blob: 8130a8b580df25f4a13dff5e83da054edaff9013 [file] [log] [blame]
Mark Slee7e9eea42007-09-10 21:00:23 +00001#import "TNSStreamTransport.h"
2
3@implementation TNSStreamTransport
4
5- (id) initWithInputStream: (NSInputStream *) input
6 outputStream: (NSOutputStream *) output
7{
8 [super init];
9 mInput = [input retain];
10 mOutput = [output retain];
11 return self;
12}
13
14- (id) initWithInputStream: (NSInputStream *) input
15{
16 return [self initWithInputStream: input outputStream: nil];
17}
18
19- (id) initWithOutputStream: (NSOutputStream *) output
20{
21 return [self initWithInputStream: nil outputStream: output];
22}
23
24
25- (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len
26{
27 int got = 0;
28 int ret = 0;
29 while (got < len) {
30 ret = [mInput read: buf+off+got maxLength: len-got];
31 if (ret <= 0) {
32 @throw [NSException exceptionWithName: @"TTransportException"
33 reason: @"Cannot read. Remote side has closed."
34 userInfo: nil];
35 }
36 got += ret;
37 }
38 return got;
39}
40
41
42- (void) write: (uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
43{
44 int result = [mOutput write: data+offset maxLength: length];
45 if (result == -1) {
46 NSDictionary * errorInfo = [NSDictionary dictionaryWithObject: [mOutput streamError]
47 forKey: @"error"];
48 @throw [NSException exceptionWithName: @"TTransportException"
49 reason: [NSString stringWithFormat: @"Error writing to transport output stream (%@).", [mOutput streamError]]
50 userInfo: errorInfo];
51 } else if (result == 0) {
52 @throw [NSException exceptionWithName: @"TTransportException"
53 reason: @"End of output stream."
54 userInfo: nil];
55 } else if (result != length) {
56 @throw [NSException exceptionWithName: @"TTransportException"
57 reason: @"Output stream did not write all of our data."
58 userInfo: nil];
59 }
60}
61
62- (void) flush
63{
64 // no flush for you!
65}
66
67@end