OpenRTMFP/Cumulus Primer(8)CumulusServer主程式主迴圈分析

鍾超發表於2012-04-15

OpenRTMFP/Cumulus Primer(8)CumulusServer主程式主迴圈分析

  • 作者:柳大·Poechant(鍾超)
  • 部落格:Blog.CSDN.net/Poechant
  • 郵箱:zhongchao.ustc#gmail.com (# -> @)
  • 日期:April 14th, 2012

該主迴圈在RTMFPServer::run(const volatile bool& terminate)函式中。RTMFPServer覆蓋Startablerun(const volatile bool &terminate)方法。

void RTMFPServer::run(const volatile bool& terminate) {

1 繫結地址

CumulusServer的 IP 地址和埠:

    SocketAddress address("0.0.0.0",_port);
    _socket.bind(address,true);

繫結CumulusEdge的 IP 地址和埠:

    SocketAddress edgesAddress("0.0.0.0",_edgesPort);
    if (_edgesPort>0)
        _edgesSocket.bind(edgesAddress,true);

傳送者(Client)的 IP 地址和埠:

    SocketAddress sender;
    UInt8 buff[PACKETRECV_SIZE];
    int size = 0;

    while (!terminate) {

        bool stop=false;
        bool idle = realTime(stop);
        if(stop)
            break;

        _handshake.isEdges=false;

2CumulusServer接收資料:

CumulusServer的 socket 有資料可讀:

        if (_socket.available() > 0) {
            try {

從 socket 讀取:

  1. 把資料存到 buff,
  2. 把傳送者地址賦給 sender,
  3. 把所讀長度返回給 size */

                 size = _socket.receiveFrom(buff,sizeof(buff),sender);
    

處理CumulusServer的 socket 產生的異常:

            } catch(Exception& ex) {
                DEBUG("Main socket reception : %s",ex.displayText().c_str());
                _socket.close();
                _socket.bind(address,true);
                continue;
            }

2 如果CumulusEdge埠存在且 edge socket 可用。

CumulusEdge的 socket 有資料可讀:

        } else if (_edgesPort > 0 && _edgesSocket.available() > 0) {
            try {
                size = _edgesSocket.receiveFrom(buff, sizeof(buff), sender);
                _handshake.isEdges = true;
            } catch(Exception& ex) {
                DEBUG("Main socket reception : %s", ex.displayText().c_str());
                _edgesSocket.close();
                _edgesSocket.bind(edgesAddress, true);
                continue;
            }
            Edge* pEdge = edges(sender);
            if (pEdge)
                pEdge->update();

3CumulusServerCumulusEdge的 socket 都沒有資料:

        } else {

CumulusServer空閒:

            if (idle) {

主執行緒等待一秒。

                Thread::sleep(1);
                if (!_timeLastManage.isElapsed(_freqManage)) {

Just middle session

                    if (_middle) {
                        Sessions::Iterator it;
                        for (it = _sessions.begin(); it != _sessions.end(); ++it) {
                            Middle* pMiddle = dynamic_cast<Middle*>(it->second);
                            if (pMiddle)
                                pMiddle->manage();
                        }
                    }
                } else {
                    _timeLastManage.update();
                    manage();
                }
            }
            continue;
        }

4 傳送方的 ip 被禁:

        if (isBanned(sender.host())) {
            INFO("Data rejected because client %s is banned",
                sender.host().toString().c_str());
            continue;
        }

5 資料包長度小於可能的最小值(12)

        if (size < RTMFP_MIN_PACKET_SIZE) {
            ERROR("Invalid packet");
            continue;
        }

        PacketReader packet(buff,size);
        Session* pSession = findSession(RTMFP::Unpack(packet));

        if (!pSession)
            continue;

        if (!pSession->checked)
            _handshake.commitCookie(*pSession);

CumulusEdge或者自己(CumulusServer)的 socket:

        pSession->setEndPoint(_handshake.isEdges ? _edgesSocket : _socket,sender);
        pSession->receive(packet);
    }

    _handshake.clear();
    _sessions.clear();
    _socket.close();
    if (_edgesPort>0)
        _edgesSocket.close();

    if(_pCirrus) {
        delete _pCirrus;
        _pCirrus = NULL;
    }
}

-

轉載請註明來自柳大的CSDN部落格:Blog.CSDN.net/Poechant

-

相關文章