| 30 |
ashish |
1 |
/*
|
|
|
2 |
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
3 |
* or more contributor license agreements. See the NOTICE file
|
|
|
4 |
* distributed with this work for additional information
|
|
|
5 |
* regarding copyright ownership. The ASF licenses this file
|
|
|
6 |
* to you under the Apache License, Version 2.0 (the
|
|
|
7 |
* "License"); you may not use this file except in compliance
|
|
|
8 |
* with the License. You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing,
|
|
|
13 |
* software distributed under the License is distributed on an
|
|
|
14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
15 |
* KIND, either express or implied. See the License for the
|
|
|
16 |
* specific language governing permissions and limitations
|
|
|
17 |
* under the License.
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
#import "TNSStreamTransport.h"
|
|
|
21 |
#import "TTransportException.h"
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
@implementation TNSStreamTransport
|
|
|
25 |
|
|
|
26 |
- (id) initWithInputStream: (NSInputStream *) input
|
|
|
27 |
outputStream: (NSOutputStream *) output
|
|
|
28 |
{
|
|
|
29 |
[super init];
|
|
|
30 |
mInput = [input retain];
|
|
|
31 |
mOutput = [output retain];
|
|
|
32 |
return self;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
- (id) initWithInputStream: (NSInputStream *) input
|
|
|
36 |
{
|
|
|
37 |
return [self initWithInputStream: input outputStream: nil];
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
- (id) initWithOutputStream: (NSOutputStream *) output
|
|
|
41 |
{
|
|
|
42 |
return [self initWithInputStream: nil outputStream: output];
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
- (void) dealloc
|
|
|
46 |
{
|
|
|
47 |
[mInput release];
|
|
|
48 |
[mOutput release];
|
|
|
49 |
[super dealloc];
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
- (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
|
|
|
54 |
{
|
|
|
55 |
int got = 0;
|
|
|
56 |
int ret = 0;
|
|
|
57 |
while (got < len) {
|
|
|
58 |
ret = [mInput read: buf+off+got maxLength: len-got];
|
|
|
59 |
if (ret <= 0) {
|
|
|
60 |
@throw [TTransportException exceptionWithReason: @"Cannot read. Remote side has closed."];
|
|
|
61 |
}
|
|
|
62 |
got += ret;
|
|
|
63 |
}
|
|
|
64 |
return got;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
- (void) write: (uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
|
|
|
69 |
{
|
|
|
70 |
int got = 0;
|
|
|
71 |
int result = 0;
|
|
|
72 |
while (got < length) {
|
|
|
73 |
result = [mOutput write: data+offset+got maxLength: length-got];
|
|
|
74 |
if (result == -1) {
|
|
|
75 |
@throw [TTransportException exceptionWithReason: @"Error writing to transport output stream."
|
|
|
76 |
error: [mOutput streamError]];
|
|
|
77 |
} else if (result == 0) {
|
|
|
78 |
@throw [TTransportException exceptionWithReason: @"End of output stream."];
|
|
|
79 |
}
|
|
|
80 |
got += result;
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
- (void) flush
|
|
|
85 |
{
|
|
|
86 |
// no flush for you!
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
@end
|