bool NetServer::SendPlayerAcceptedPacket(ENetPeer * pPeer) { //Send to client requesting connection PackedBuffer msg; try { msg.pack_ubyte(PLAYERACCEPTED_PACKET); } catch (PackedBufferException &e) { GfLogFatal("SendPlayerAcceptedPacket: packed buffer error\n"); } GfLogTrace("SendPlayerAcceptedPacket: packed data length=%d\n", msg.length()); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); if (enet_peer_send (pPeer, RELIABLECHANNEL, pPacket)==0) return true; return false; }
void NetClient::SendServerTimeRequest() { m_packetsendtime = GfTimeClock(); PackedBuffer msg; try { msg.pack_ubyte(SERVER_TIME_REQUEST_PACKET); } // catch (PackedBufferException &e) catch (PackedBufferException) { GfLogFatal("SendServerTimeRequest: packed buffer error\n"); } GfLogTrace("SendServerTimeRequest: packed data length=%d\n", msg.length()); ENetPacket *pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_UNSEQUENCED); if (enet_peer_send (m_pServer, UNRELIABLECHANNEL, pPacket)) GfLogError("SendServerTimeRequest : enet_peer_send failed\n"); }
//Here you are Xavier a dynamic weather packet void NetServer::SendWeatherPacket() { GfLogTrace("Sending Weather Packet\n"); PackedBuffer msg; try { msg.pack_ubyte(WEATHERCHANGE_PACKET); //TODO add weather data here } catch (PackedBufferException &e) { GfLogFatal("SendWeatherPacket: packed buffer error\n"); } GfLogTrace("SendWeatherPacket: packed data length=%d\n", msg.length()); ENetPacket *pWeatherPacket = enet_packet_create(msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); BroadcastPacket(pWeatherPacket,RELIABLECHANNEL); }
void NetClient::SetDriverReady(bool bReady) { // Get local driver index in the race driver list int idx = GetDriverIdx(); NetMutexData *pNData = LockNetworkData(); pNData->m_vecReadyStatus[idx-1] = bReady; UnlockNetworkData(); PackedBuffer msg; try { msg.pack_ubyte(DRIVERREADY_PACKET); msg.pack_int(idx); msg.pack_int(bReady); } // catch (PackedBufferException &e) catch (PackedBufferException) { GfLogFatal("SetDriverReady: packed buffer error\n"); } GfLogTrace("SetDriverReady: packed data length=%d\n", msg.length()); ENetPacket *pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); if (enet_peer_send (m_pServer, RELIABLECHANNEL, pPacket)==0) return; }
void NetServer::SendDriversReadyPacket() { NetMutexData *pNData = LockNetworkData(); int rsize = pNData->m_vecReadyStatus.size(); PackedBuffer msg; try { msg.pack_ubyte(ALLDRIVERREADY_PACKET); msg.pack_int(rsize); for (int i = 0; i < rsize; i++) { msg.pack_int(pNData->m_vecReadyStatus[i]); } } catch (PackedBufferException &e) { GfLogFatal("SendDriversReadyPacket: packed buffer error\n"); } GfLogTrace("SendDriversReadyPacket: packed data length=%d\n", msg.length()); UnlockNetworkData(); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); BroadcastPacket(pPacket,RELIABLECHANNEL); m_bRefreshDisplay = true; }
void NetClient::SendReadyToStartPacket() { std::string strDName = GetDriverName(); GfLogTrace("Sending ready to start packet\n"); PackedBuffer msg; try { msg.pack_ubyte(CLIENTREADYTOSTART_PACKET); msg.pack_stdstring(strDName); } // catch (PackedBufferException &e) catch (PackedBufferException) { GfLogFatal("SendReadyToStartPacket: packed buffer error\n"); } GfLogTrace("SendReadyToStartPacket: packed data length=%d\n", msg.length()); ENetPacket *pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); if (enet_peer_send (m_pServer, RELIABLECHANNEL, pPacket)) GfLogError("SendReadyToStartPacket : enet_peer_send failed\n"); }
void NetServer::SendTimePacket(ENetPacket *pPacketRec, ENetPeer * pPeer) { GfLogTrace("Sending Time Packet\n"); double time = GfTimeClock(); GfLogTrace("\nServer time is %lf",time); PackedBuffer msg; try { msg.pack_ubyte(SERVER_TIME_SYNC_PACKET); msg.pack_double(time); } catch (PackedBufferException &e) { GfLogFatal("SendTimePacket: packed buffer error\n"); } GfLogTrace("SendTimePacket: packed data length=%d\n", msg.length()); //TODO change to peer send ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_UNSEQUENCED); enet_peer_send (pPeer, UNRELIABLECHANNEL, pPacket); }
void NetServer::SendStartTimePacket(int &startTime) { //Wait RACESTARTDELEAY seconds to start race m_racestarttime = GfTimeClock()+RACESTARTDELEAY; PackedBuffer msg; try { msg.pack_ubyte(RACESTARTTIME_PACKET); msg.pack_double(m_racestarttime); } catch (PackedBufferException &e) { GfLogFatal("SendStartTimePacket: packed buffer error\n"); } GfLogTrace("SendStartTimePacket: packed data length=%d\n", msg.length()); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); BroadcastPacket(pPacket,RELIABLECHANNEL); GfLogInfo("Server Start time is %lf\n",m_racestarttime); }
void NetServer::SendFinishTimePacket() { GfLogTrace("Sending finish Time Packet\n"); NetMutexData *pNData = LockNetworkData(); double time = pNData->m_finishTime; UnlockNetworkData(); GfLogInfo("Server finish time is %lf\n",time); PackedBuffer msg; try { msg.pack_ubyte(FINISHTIME_PACKET); msg.pack_double(time); } catch (PackedBufferException &e) { GfLogFatal("SendFinishTimePacket: packed buffer error\n"); } GfLogTrace("SendFinishTimePacket: packed data length=%d\n", msg.length()); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); BroadcastPacket(pPacket,RELIABLECHANNEL); }
void NetServer::SendPrepareToRacePacket() { //Add all players to list except the server player NetServerMutexData *pSData = LockServerData(); for (int i=0;i<(int)pSData->m_vecNetworkPlayers.size();i++) { if (pSData->m_vecNetworkPlayers[i].client) { m_vecWaitForPlayers.push_back(pSData->m_vecNetworkPlayers[i]); } } UnlockServerData(); if (m_vecWaitForPlayers.size()==0) m_bBeginRace = true; ////TODO send needed xml files to race PackedBuffer msg; try { msg.pack_ubyte(PREPARETORACE_PACKET); } catch (PackedBufferException &e) { GfLogFatal("SendPrepareToRacePacket: packed buffer error\n"); } GfLogTrace("SendPrepareToRacePacket: packed data length=%d\n", msg.length()); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); BroadcastPacket(pPacket,RELIABLECHANNEL); }
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); }
bool NetClient::SendDriverInfoPacket(NetDriver *pDriver) { SetDriverName(pDriver->name); pDriver->address.port = m_pHost->address.port; GfLogTrace("SendDriverInfoPacket: pDriver\n"); GfLogTrace("->host=%d\n", pDriver->address.host); GfLogTrace("->port=%d\n", pDriver->address.port); GfLogTrace("->idx=%d\n", pDriver->idx); GfLogTrace("->name=%s\n", pDriver->name); GfLogTrace("->sname=%s\n", pDriver->sname); GfLogTrace("->cname=%s\n", pDriver->cname); GfLogTrace("->car=%s\n", pDriver->car); GfLogTrace("->team=%s\n", pDriver->team); GfLogTrace("->author=%s\n", pDriver->author); GfLogTrace("->racenumber=%d\n", pDriver->racenumber); GfLogTrace("->skilllevel=%s\n", pDriver->skilllevel); GfLogTrace("->red=%.1f\n", pDriver->red); GfLogTrace("->green=%.1f\n", pDriver->green); GfLogTrace("->blue=%.1f\n", pDriver->blue); GfLogTrace("->module=%s\n", pDriver->module); GfLogTrace("->type=%s\n", pDriver->type); GfLogTrace("->client=%d\n", pDriver->client); PackedBuffer msg; try { msg.pack_ubyte(PLAYERINFO_PACKET); msg.pack_int(pDriver->idx); msg.pack_string(pDriver->name, sizeof pDriver->name); msg.pack_string(pDriver->sname, sizeof pDriver->sname); msg.pack_string(pDriver->cname, sizeof pDriver->cname); msg.pack_string(pDriver->car, sizeof pDriver->car); msg.pack_string(pDriver->team, sizeof pDriver->team); msg.pack_string(pDriver->author, sizeof pDriver->author); msg.pack_int(pDriver->racenumber); msg.pack_string(pDriver->skilllevel, sizeof pDriver->skilllevel); msg.pack_float(pDriver->red); msg.pack_float(pDriver->green); msg.pack_float(pDriver->blue); msg.pack_string(pDriver->module, sizeof pDriver->module); msg.pack_string(pDriver->type, sizeof pDriver->type); msg.pack_int(pDriver->client); } // catch (PackedBufferException &e) catch (PackedBufferException) { GfLogFatal("SendDriverInfoPacket: packed buffer error\n"); } GfLogTrace("SendDriverInfoPacket: packed data length=%d\n", msg.length()); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); if (enet_peer_send (m_pServer, RELIABLECHANNEL, pPacket)==0) return true; return false; }
//Remove disconnected player from race track void NetServer::RemovePlayerFromRace(unsigned int idx) { GfLogTrace("Removing disconnected player\n"); std::vector<CarStatus> vecCarStatus; double time = 0.0; int startRank = GetDriverStartRank(idx); CarStatus cstatus; cstatus.topSpeed = -1.0; cstatus.fuel = -1.0; cstatus.startRank = startRank; cstatus.dammage = -1; cstatus.state = RM_CAR_STATE_ELIMINATED; cstatus.time = m_currentTime; NetMutexData *pNData = LockNetworkData(); pNData->m_vecCarStatus.push_back(cstatus); UnlockNetworkData(); vecCarStatus.push_back(cstatus); time = m_currentTime; int iNumCars = vecCarStatus.size(); PackedBuffer msg; try { msg.pack_ubyte(CARSTATUS_PACKET); msg.pack_double(time); msg.pack_int(iNumCars); for (int i=0;i<iNumCars;i++) { msg.pack_float(vecCarStatus[i].topSpeed); msg.pack_int(vecCarStatus[i].state); msg.pack_int(vecCarStatus[i].startRank); msg.pack_int(vecCarStatus[i].dammage); msg.pack_float(vecCarStatus[i].fuel); } } catch (PackedBufferException &e) { GfLogFatal("RemovePlayerFromRace: packed buffer error\n"); } GfLogTrace("RemovePlayerFromRace: packed data length=%d\n", msg.length()); ENetPacket * pPacket = enet_packet_create (msg.buffer(), msg.length(), ENET_PACKET_FLAG_RELIABLE); BroadcastPacket(pPacket,RELIABLECHANNEL); }