/// \fn NetConnection::update() /// \brief Updates the message queues etc. void NetConnection::update() { ASSERT(IS_SET(mFlags, eConnected)); Timer& timer = TIMER; // receive pending messages receive(); // resend messages if necessary mClients.rewind(); while ( mClients.hasNext() ) { NetAddress& client = mClients.getNext(); #ifdef JENGINE_CONNTIMEOUT if ((timer.getTick() - client.lastTimeRecv) > MAX_TIME_BETWEEN_RECV) { // the client has been time out, remove the bastard Log::getInstance().error("Player %d has been timed out!", index); } else #endif { process(client); sendAck(client); sendAlive(client, timer.getTick()); if ( client.pstatistics ) { client.pstatistics->update(timer.getTick()); } } } }
void Server::listen() { _aliveThread = new std::thread([] () { int delay = 10000; while (true) { sleep(delay); printf("SENDING ALIVE\n"); sendAlive(); delay = (delay == 10000 ? 20000 : 180000); } }); _listenerThread = new std::thread([] () { bool bindErrorReported = false; while (true) { NPT_UdpMulticastSocket socket; NPT_IpAddress upnpAddress = getUPNPAddress(); // TODO: Setup local port and network interface socket.SetTimeToLive(4); socket.JoinGroup(upnpAddress); while (true) { char* buf = new char[1024]; NPT_DataBuffer receivePacket; NPT_SocketAddress address; receivePacket.SetBuffer((NPT_Byte*)buf, 1024); socket.Receive(receivePacket, &address); std::string s((char*)receivePacket.GetData(), receivePacket.GetDataSize()); if (s.compare(0, 8, "M-SEARCH") == 0) { NPT_String remoteAddr = address.GetIpAddress().ToString(); int remotePort = address.GetPort(); // if (gMS->isAddressAllowed(address)) { if (s.find_first_of("urn:schemas-upnp-org:service:ContentDirectory:1") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "urn:schemas-upnp-org:service:ContentDirectory:1"); } if (s.find_first_of("upnp:rootdevice") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "upnp:rootdevice"); } if (s.find_first_of("urn:schemas-upnp-org:device:MediaServer:1") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "urn:schemas-upnp-org:device:MediaServer:1"); } if (s.find_first_of("ssdp:all") > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, "urn:schemas-upnp-org:device:MediaServer:1"); } if (s.find_first_of(gMS->udnString()) > 0) { sendDiscover(remoteAddr.GetChars(), remotePort, gMS->udnString()); } } } else if (s.compare(0, 6, "NOTIFY") == 0) { /* String remoteAddr = address.getHostAddress(); int remotePort = receivePacket.getPort(); logger.trace("Receiving a NOTIFY from [" + remoteAddr + ":" + remotePort + "]"); */ } } } }); }