SocketRocket研究二

socket的调用周期


  • socket在打开和接收发数据时一直会调用一个回调方法
    - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
    这里通过的code的状态和aStream是in还是out的判断进行处理,code总共有如下几类
typedef NS_OPTIONS(NSUInteger, NSStreamEvent) {
    NSStreamEventNone = 0,
    NSStreamEventOpenCompleted = 1UL << 0,
    NSStreamEventHasBytesAvailable = 1UL << 1,
    NSStreamEventHasSpaceAvailable = 1UL << 2,
    NSStreamEventErrorOccurred = 1UL << 3,
    NSStreamEventEndEncountered = 1UL << 4
};
  1. NSStreamEventOpenCompleted和in、outStream表示连接打开了
  2. 如果是inStream,会接着调用didConnect,在这里会创建一个包含一些请求信息的CFHTTPMessageRef request,然后把一些参数和验证信息放到这里
  3. 把创建好的request转换成NSData发送给服务器,也就是接着调用- (void)_writeData:(NSData *)data- (void)_pumpWriting;_scheduleCleanup这个发送数据的流程
  4. 同时调用_readHTTPHeader_HTTPHeadersDidFinishwebSocketDidOpen通知代理连接已经开启
  • NSStreamEventEndEncountered 和in、outStream表示结束连接,但调用了close并没回调这个,在上时间没发心跳包时会被后台剔除然后就会回调这个,好做断开重连
  • NSStreamEventErrorOccurred和in、outStream表示连接出错
  • NSStreamEventHasBytesAvailable和InputStream表示接收到了数据
    1. 把inputStream的数据读入_readBuffer中
                 const int bufferSize = 2048;
                 uint8_t buffer[bufferSize];
                 
                 while (_inputStream.hasBytesAvailable) {
                     NSInteger bytes_read = [_inputStream read:buffer maxLength:bufferSize];
                     
                     if (bytes_read > 0) {
                         [_readBuffer appendBytes:buffer length:bytes_read];
                     } else if (bytes_read < 0) {
                         [self _failWithError:_inputStream.streamError];
                     }
                     
                     if (bytes_read != bufferSize) {
                         break;
                     }
                 };
                 [self _pumpScanner];
    
    紧接着调用_pumpScanner_innerPumpScanner、使用在首次连接成功后读header时设置的处理数据handler
-(void)_readHTTPHeader;
{
    if (_receivedHTTPHeaders == NULL) {
        _receivedHTTPHeaders = CFHTTPMessageCreateEmpty(NULL, NO);
    }
    // 处理之后接收到的数据                    
    [self _readUntilHeaderCompleteWithCallback:^(SRWebSocket *self,  NSData *data) {
        CFHTTPMessageAppendBytes(_receivedHTTPHeaders, (const UInt8 *)data.bytes, data.length);
        
        if (CFHTTPMessageIsHeaderComplete(_receivedHTTPHeaders)) {
            SRFastLog(@"Finished reading headers %@", CFBridgingRelease(CFHTTPMessageCopyAllHeaderFields(_receivedHTTPHeaders)));
            [self _HTTPHeadersDidFinish];
        } else {
            [self _readHTTPHeader];
        }
    }];
}
  • NSStreamEventHasSpaceAvailable和OutputStream 表示发送数据,只在启动连接后和close时和调用send会回调这个方法(send/open/close方法后的回调),本质上这几个地方都调用了- (void)_writeData:(NSData *)data;- (void)_pumpWriting;,所以会调用这个回调

读取内容的方法是在第一次
didConnect->_readFrameNew->_readFrameContinue->_handleFrameHeader:curData:->|_handleFrameWithData:opCode:->|_readFrameNew
------------------------------------------------|_readFrameContinue
-------------------------------------------------|__handleMessage
|_readFrameContinue

你可能感兴趣的:(SocketRocket研究二)