void NetClient::ReadAllDriverReadyPacket(ENetPacket *pPacket) { int rsize; PackedBuffer msg(pPacket->data, pPacket->dataLength); GfLogTrace("ReadAllDriverReadyPacket: packed data length=%d\n", msg.length()); try { msg.unpack_ubyte(); rsize = msg.unpack_int(); NetMutexData *pNData = LockNetworkData(); pNData->m_vecReadyStatus.clear(); pNData->m_vecReadyStatus.resize(rsize); for (int i=0;i<rsize;i++) pNData->m_vecReadyStatus[i] = msg.unpack_int() ? true : false; UnlockNetworkData(); SetRaceInfoChanged(true); } // catch (PackedBufferException &e) catch (PackedBufferException) { GfLogFatal("ReadAllDriverReadyPacket: packed buffer error\n"); } GfLogTrace("Received All Driver Ready Packet\n"); }
void NetServer::OverrideDriverReady(int idx,bool bReady) { NetMutexData *pNData = LockNetworkData(); pNData->m_vecReadyStatus[idx-1] = bReady; UnlockNetworkData(); Dump("NetServer::OverrideDriverReady"); SetRaceInfoChanged(true); }
bool NetServer::listen() { if (!m_pServer) return false; bool bHasPacket = false; ENetEvent event; char hostName[256]; while (enet_host_service (m_pServer, & event, 0) > 0) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: enet_address_get_host_ip (&event.peer -> address,hostName,256); GfLogTrace ("A new client connected from %s\n",hostName); /* Store any relevant client information here. */ event.peer -> data = (void*)"Client information"; break; case ENET_EVENT_TYPE_RECEIVE: ReadPacket(event); bHasPacket = true; break; case ENET_EVENT_TYPE_DISCONNECT: GfLogTrace("\nA client lost the connection.\n"); enet_address_get_host_ip (&event.peer -> address,hostName,256); GfLogTrace ("A new client disconnected from %s\n",hostName); RemoveDriver(event); SetRaceInfoChanged(true); GfLogTrace ("%s disconected.\n", (char*)event.peer -> data); /* Reset the peer's client information. */ event.peer -> data = NULL; break; case ENET_EVENT_TYPE_NONE: // Do nothing. break; } } if (bHasPacket) m_activeNetworkTime = GfTimeClock(); return bHasPacket; }
void NetServer::UpdateDriver(NetDriver & driver) { assert(m_strRaceXMLFile!=""); bool bNewDriver = true; NetServerMutexData *pSData = LockServerData(); // Search for the driver in m_vecNetworkPlayers, and update its car name if found. for(unsigned int i=0;i<pSData->m_vecNetworkPlayers.size();i++) { if (strcmp(driver.name,pSData->m_vecNetworkPlayers[i].name)==0) { bNewDriver = false; strncpy(pSData->m_vecNetworkPlayers[i].car,driver.car,64); break; } } // If not found, append it to m_vecNetworkPlayers if (bNewDriver) { driver.idx = pSData->m_vecNetworkPlayers.size()+1; if (!driver.client) { driver.address = m_pServer->address; } pSData->m_vecNetworkPlayers.push_back(driver); NetMutexData *pNData = LockNetworkData(); pNData->m_vecReadyStatus.push_back(false); UnlockNetworkData(); } GenerateDriversForXML(); RobotXml rXml; rXml.CreateRobotFile("networkhuman",pSData->m_vecNetworkPlayers); UnlockServerData(); Dump("NetServer::UpdateDriver"); SetRaceInfoChanged(true); }
bool NetServer::Start(int port) { SetRaceInfoChanged(true); m_bPrepareToRace = false; m_bBeginRace = false; m_timePhysics = -2.0; m_sendCarDataTime = 0.0; m_sendCtrlTime = 0.0; /* Bind the server to the default localhost. */ /* A specific host address can be specified by */ /* enet_address_set_host (& address, "x.x.x.x"); */ m_address.host = ENET_HOST_ANY; /* Bind the server to port*/ m_address.port = port; assert(m_pServer ==NULL); GfLogInfo ("Starting network server : Listening on port %d.\n", port); #if (ENET_VERSION >= 0x010300) m_pServer = enet_host_create (& m_address /* the address to bind the server host to */, MAXNETWORKPLAYERS, 2, /* assume tha maximum number of channels is allowed*/ 0 /* assume any amount of incoming bandwidth */, 0 /* assume any amount of outgoing bandwidth */); #else m_pServer = enet_host_create (& m_address /* the address to bind the server host to */, MAXNETWORKPLAYERS, 0 /* assume any amount of incoming bandwidth */, 0 /* assume any amount of outgoing bandwidth */); #endif if (m_pServer == NULL) { GfLogError ("An error occurred while trying to create an ENet server host.\n"); return false; } m_pHost = m_pServer; return true; }
void NetServer::SendRaceSetupPacket() { PackedBuffer msg; try { msg.pack_ubyte(RACEINFOCHANGE_PACKET); } catch (PackedBufferException &e) { GfLogFatal("SendRaceSetupPacket: packed buffer error\n"); } GfLogTrace("SendRaceSetupPacket: packed data length=%d\n", msg.length()); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); BroadcastPacket(pPacket,RELIABLECHANNEL); SetRaceInfoChanged(true); }
void NetClient::ReadRaceSetupPacket(ENetPacket *pPacket) { GfLogTrace("\nRecieving race setup\n"); SetRaceInfoChanged(true); }
void NetServer::RemoveDriver(ENetEvent event) { int playerStartIndex; ENetAddress address = event.peer->address; char hostName[256]; enet_address_get_host_ip (&address,hostName,256); GfLogTrace ("Client Player Info disconnect from %s\n",hostName); std::vector<NetDriver>::iterator p; if (m_vecWaitForPlayers.size()>0) { p = m_vecWaitForPlayers.begin(); while(p!=m_vecWaitForPlayers.end()) { if ((p->address.host == address.host)&&(p->hostPort == address.port)) { m_vecWaitForPlayers.erase(p); break; } p++; } if (m_vecWaitForPlayers.size()==0) m_bBeginRace = true; } //look for driver id NetServerMutexData *pSData = LockServerData(); for (p = pSData->m_vecNetworkPlayers.begin();p!=pSData->m_vecNetworkPlayers.end();p++) { if (p->client) { if ((p->address.host == address.host)&&(p->hostPort == address.port)) { if(m_bRaceActive) { playerStartIndex = p->idx-1; pSData->m_vecNetworkPlayers.erase(p); RemovePlayerFromRace(playerStartIndex); GenerateDriversForXML(); RobotXml rXml; rXml.CreateRobotFile("networkhuman",pSData->m_vecNetworkPlayers); SetRaceInfoChanged(true); } else { pSData->m_vecNetworkPlayers.erase(p); GenerateDriversForXML(); RobotXml rXml; rXml.CreateRobotFile("networkhuman",pSData->m_vecNetworkPlayers); SetRaceInfoChanged(true); } UnlockServerData(); return; } } } UnlockServerData(); }