该主循环在RTMFPServer::run(const volatile bool& terminate)
函数中。RTMFPServer
覆盖Startable
的run(const volatile bool &terminate)
方法。
void RTMFPServer::run(const volatile bool& terminate) {
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;
CumulusServer
接收数据:CumulusServer
的 socket 有数据可读:
if (_socket.available() > 0) {
try {
从 socket 读取:
把所读长度返回给 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;
}
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();
CumulusServer
和CumulusEdge
的 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;
}
if (isBanned(sender.host())) {
INFO("Data rejected because client %s is banned",
sender.host().toString().c_str());
continue;
}
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
-