32 @property(strong, NS_NONATOMIC_IOSONLY) NSMutableData *requestBuffer;
33 @property(strong, NS_NONATOMIC_IOSONLY) NSMutableData *responseBuffer;
46 @synthesize requestBuffer = _requestBuffer;
47 @synthesize responseBuffer = _responseBuffer;
52 if ((
self = [super
init]))
62 [
self setRequestBuffer:[NSMutableData data]];
63 [
self setResponseBuffer:[NSMutableData data]];
74 - (id)initWithRequestStream:(NSInputStream *)requestStream responseStream:(NSOutputStream *)responseStream
79 [
self setRequestStream:requestStream];
80 [
self setResponseStream:responseStream];
85 - (id)initWithSocketNativeHandle:(NSSocketNativeHandle)socketNativeHandle
87 if ((
self = [
self init]))
89 CFReadStreamRef readStream = NULL;
90 CFWriteStreamRef writeStream = NULL;
91 CFStreamCreatePairWithSocket(kCFAllocatorDefault, socketNativeHandle, &readStream, &writeStream);
92 if (readStream && writeStream)
94 CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
95 CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
96 [
self setRequestStream:CFBridgingRelease(readStream)];
97 [
self setResponseStream:CFBridgingRelease(writeStream)];
101 if (readStream) CFRelease(readStream);
102 if (writeStream) CFRelease(writeStream);
117 [stream setDelegate:self];
119 [stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
134 [stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
145 - (NSString *)receiveLineUsingEncoding:(NSStringEncoding)encoding
156 NSString *requestString = [[NSString alloc] initWithData:[
self requestBuffer] encoding:encoding];
157 NSRange lineRange = [requestString lineRangeForRange:NSMakeRange(0, 0)];
158 if (lineRange.length)
160 [[
self requestBuffer] setData:[[requestString substringFromIndex:lineRange.length] dataUsingEncoding:encoding]];
161 result = [requestString substringToIndex:lineRange.length];
183 #pragma mark Overrides
190 if (delegate && [delegate respondsToSelector:
@selector(streamPair:
hasBytesAvailable:)])
192 [delegate streamPair:self hasBytesAvailable:[[
self requestBuffer] length]];
213 [
responseBuffer replaceBytesInRange:NSMakeRange(0, bytesSent) withBytes:NULL length:0];
217 - (void)handleRequestEvent:(NSStreamEvent)eventCode
221 case NSStreamEventHasBytesAvailable:
224 NSInteger bytesAvailable = [[
self requestStream] read:bytes maxLength:sizeof(bytes)];
235 while (bytesAvailable > 0)
237 [[
self requestBuffer] appendBytes:bytes length:bytesAvailable];
240 bytesAvailable = [[
self requestStream] read:bytes maxLength:sizeof(bytes)];
250 if (bytesAvailable < 0)
261 if (
delegate && [
delegate respondsToSelector:
@selector(streamPair:handleRequestEvent:)])
263 [
delegate streamPair:self handleRequestEvent:eventCode];
267 - (void)handleResponseEvent:(NSStreamEvent)eventCode
271 case NSStreamEventHasSpaceAvailable:
281 if (
delegate && [
delegate respondsToSelector:
@selector(streamPair:handleResponseEvent:)])
283 [
delegate streamPair:self handleResponseEvent:eventCode];
288 #pragma mark Stream Delegate
290 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode