a. 清空_readFdsCopy、_writeFdsCopy,
b. 把_readFds、_writeFds賦值給_readFdsCopy、_writeFdsCopy
c. 再呼叫select(MAP_KEY(--_fdState.end()) + 1, &_readFdsCopy, &_writeFdsCopy, NULL, &_timeout)進行socket輪詢
d. 遍歷FOR_MAP(_activeIOHandlers, uint32_t, IOHandler *, i),檢查IOHandler所對應的socket是否在其中, FD_ISSET(MAP_VAL(i)->GetInboundFd(), &_readFdsCopy) 或者 FD_ISSET(MAP_VAL(i)->GetOutboundFd(), &_writeFdsCopy)
e. 如果存在fdSets中,就呼叫相應IOHandler的OnEvent
e.1. accept(_inboundFd, &address, &len)
e.2. setFdOptions(fd, false)
e.3. BaseProtocol *pProtocol = ProtocolFactoryManager::CreateProtocolChain(_protocolChain, _parameters)
e.4. 根據accept返回的fd建立TCPCarrier()例項物件,建構函式裡面就把設定為可讀資料狀態、註冊到IOHandlerManager中
e.5. pTCPCarrier->SetProtocol(pProtocol->GetFarEndpoint());設定pTCPCarrier的協議為遠點協議TCPProtocol
e.6. pProtocol->GetFarEndpoint()->SetIOHandler(pTCPCarrier); 設定遠點協議的IOHandler為TCPCarrier
e.7. pProtocol->GetNearEndpoint()->SetApplication(_pApplication); 設定近點協議的應用為本身應用物件(AdminApplication)
e.8. pProtocol->GetNearEndpoint()->GetOutputBuffer()不為NULL,則pProtocol->GetNearEndpoint()->EnqueueForOutbound();
舉例:GetNearEndpoint返回InboundRTMPProtocol物件,GetOutputBuffer()實際是呼叫BaseRTMPProtocol物件中的IOBuffer _outputBuffer;EnqueueForOutbound()實際呼叫_pFarProtocol->EnqueueForOutbound(),_pFarProtocol為TCPProtocol(),呼叫TCPProtocol::EnqueueForOutbound(),即是_pCarrier->SignalOutputData(),此函式只是ENABLE_WRITE_DATA
f. 如果在OnEvent()中處理失敗就呼叫EnqueueForDelete()進入狀態清理工作,主要是清理_fdState中關於此socket的狀態(相應的去設定_readFds、_writeFds的狀態),並把IOHandler加入到_deadIOHandlers MAP中
然後在TCPCarrier中做資料的收發動作!根據讀寫進行相應的操作:
讀:
TCPCarrier的_pProtocol為TCPProtocol a. IOBuffer *pInputBuffer = _pProtocol->GetInputBuffer(); //近點協議帶有關於輸入的IOBuffer _inputBuffer
以上實際呼叫TCPProtocol::GetInputBuffer()介面,然後返回TCPProtocol物件的成員 _inputBuffer b. if (!pInputBuffer->ReadFromTCPFd(_inboundFd, _recvBufferSize, _ioAmount)) c. _pProtocol->SignalInputData(_ioAmount)
在函式內會呼叫_pNearProtocol->SignalInputData(_inputBuffer),對應呼叫BaseRTMPProtocol::SignalInputData()介面,在此函式內部進行輸入資料的解析。
寫:
a. pOutputBuffer = _pProtocol->GetOutputBuffer()
在函式內部呼叫_pNearProtocol->GetOutputBuffer(),實際呼叫BaseRTMPProtocol::GetOutputBuffer(),返回
BaseRTMPProtocol的成員IOBuffer _outputBuffer b. pOutputBuffer->WriteToTCPFd(_outboundFd, _sendBufferSize, _ioAmount)