diff --git a/client/in_game.cpp b/client/in_game.cpp index 2c40566..070d832 100644 --- a/client/in_game.cpp +++ b/client/in_game.cpp @@ -20,6 +20,7 @@ InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nU } InGame::~InGame() { + //placeholder disconnect PacketData p; p.type = PacketList::DISCONNECT; p.disconnect.playerID = *playerID; @@ -44,7 +45,46 @@ void InGame::FrameEnd() { } void InGame::Update() { - // + Receive(); +} + +void InGame::Receive() { + PacketData packet; + while(netUtil->Receive()) { + memcpy(&packet, netUtil->GetInData(), sizeof(PacketData)); + switch(packet.type) { +// case PacketList::NONE: +// // +// break; +// case PacketList::PING: +// // +// break; +// case PacketList::PONG: +// // +// break; +// case PacketList::JOINREQUEST: +// // +// break; +// case PacketList::JOINCONFIRM: +// // +// break; +// case PacketList::DISCONNECT: +// // +// break; +// case PacketList::SYNCHRONIZE: +// // +// break; + case PacketList::NEWPLAYER: + cout << "NEWPLAYER triggered" << endl; + break; +// case PacketList::DELETEPLAYER: +// // +// break; +// case PacketList::MOVEMENT: +// // +// break; + } + } } void InGame::Render(SDL_Surface* const screen) { diff --git a/client/in_game.hpp b/client/in_game.hpp index 3ab55fd..ec69d41 100644 --- a/client/in_game.hpp +++ b/client/in_game.hpp @@ -19,6 +19,7 @@ protected: virtual void FrameStart(); virtual void FrameEnd(); virtual void Update(); + virtual void Receive(); virtual void Render(SDL_Surface* const); //Event handlers diff --git a/client/lobby.cpp b/client/lobby.cpp index 462f6de..aa1dc39 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -63,7 +63,6 @@ void Lobby::Update() { } void Lobby::Receive() { - //dump to the console PacketData packet; while(netUtil->Receive()) { memcpy(&packet, netUtil->GetInData(), sizeof(PacketData)); @@ -80,12 +79,13 @@ void Lobby::Receive() { // case PacketList::JOINREQUEST: // // // break; - case PacketList::JOINCONFIRM: + case PacketList::JOINCONFIRM: { PacketData p; memcpy(&p, netUtil->GetInData(), sizeof(PacketData)); *playerID = p.joinConfirm.playerID; netUtil->Bind(&netUtil->GetInPacket()->address, 0); SetNextScene(SceneList::INGAME); + } break; // case PacketList::DISCONNECT: // // @@ -93,9 +93,12 @@ void Lobby::Receive() { // case PacketList::SYNCHRONIZE: // // // break; -// case PacketList::NEWPLAYER: -// // -// break; +#ifdef DEBUG + //this might not be the end of the world; it only happens when the game ping is too low + case PacketList::NEWPLAYER: + cout << "WARNING: new player triggered unexpectedly" << endl; + break; +#endif // case PacketList::DELETEPLAYER: // // // break; @@ -195,6 +198,7 @@ void Lobby::PushServer(PacketData* packet) { void Lobby::JoinRequest(ServerData* server) { if (!server) { + //CAN receive null return; } PacketData p; diff --git a/common/packet_list.hpp b/common/packet_list.hpp index b1c749f..7e5bd38 100644 --- a/common/packet_list.hpp +++ b/common/packet_list.hpp @@ -69,6 +69,7 @@ struct NewPlayer { char handle[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE]; Vector2 position; + Vector2 motion; }; struct DeletePlayer { diff --git a/docs/pseudocode.txt b/docs/pseudocode.txt index 8a8af18..40f1ba4 100644 --- a/docs/pseudocode.txt +++ b/docs/pseudocode.txt @@ -50,3 +50,25 @@ Receive: player update: PlayerManager.Update(message) end + +------------------------- + +--send info about the specified client to all clients +--use this for new connections, movement, etc. +SendClientData(int playerID): + for (clientMap): + Send(it.channel, clientMap[playerID]) + end +end + +NewClientData := SendClientData + + +--send all info about the server to the specified client +--send this to new connections +SynchronizeClient(int playerID): + for (clientMap): + Send(clientMap[playerID].channel, it) + end +end + diff --git a/server/client_data.hpp b/server/client_data.hpp index 97cfb1e..d6e81b6 100644 --- a/server/client_data.hpp +++ b/server/client_data.hpp @@ -25,8 +25,7 @@ struct ClientData { std::string avatar; enum class Command { NORMAL, - SYNCHRONIZE, - PING, + CHANGED, }command = Command::NORMAL; }; diff --git a/server/server_application.cpp b/server/server_application.cpp index 3364d68..aef3f31 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -78,7 +78,7 @@ void ServerApplication::Quit() { void ServerApplication::Ping(PacketData* packet) { //respond to pings with the server name if (!packet) { - return; + throw(runtime_error("Ping() received null")); } packet->type = PacketList::PONG; snprintf(packet->pong.metadata,PACKET_STRING_SIZE, "%s",configUtil.CString("servername")); @@ -114,7 +114,8 @@ void ServerApplication::JoinRequest(PacketData* packet) { p.joinConfirm.playerID = clientMap[playerID].playerID; netUtil.Send(clientMap[playerID].channel, &p, sizeof(PacketData)); - //TODO: NewPlayer() + //send it out to the clients + NewClientData(playerID); #ifdef DEBUG cout << "current players: " << clientMap.size() << endl; @@ -138,12 +139,40 @@ void ServerApplication::Disconnect(int playerID) { void ServerApplication::Movement(PacketData* packet) { if (!packet) { - return; + throw(runtime_error("Movement() received null")); } clientMap[packet->movement.playerID].position = packet->movement.position; clientMap[packet->movement.playerID].motion = packet->movement.motion; //simple relay + //TODO: SendClientData(packet->movement.playerID); for (auto it : clientMap) { netUtil.Send(it.second.channel, packet, sizeof(PacketData)); } } + +void ServerApplication::NewClientData(int playerID) { + if (clientMap.find(playerID) == clientMap.end()) { + throw(runtime_error("NewClientData() Failed to find the client")); + } + //create the packet to send + PacketData p; + p.type = PacketList::NEWPLAYER; + p.newPlayer.playerID = clientMap[playerID].playerID; + snprintf(p.newPlayer.handle, PACKET_STRING_SIZE, "%s", clientMap[playerID].handle.c_str()); + snprintf(p.newPlayer.avatar, PACKET_STRING_SIZE, "%s", clientMap[playerID].avatar.c_str()); + p.newPlayer.position = clientMap[playerID].position; + p.newPlayer.motion = clientMap[playerID].motion; + + //send the packet + for (auto it : clientMap) { + netUtil.Send(it.second.channel, &p, sizeof(PacketData)); + } +} + +void ServerApplication::SendClientData(int playerID) { + //TODO +} + +void ServerApplication::SynchronizeClient(int playerID) { + //TODO +} diff --git a/server/server_application.hpp b/server/server_application.hpp index 2a1d2d8..e13080f 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -27,14 +27,17 @@ private: void Disconnect(int playerID); void Movement(PacketData*); - bool running = false; - Delta delta; + void NewClientData(int playerID); + void SendClientData(int playerID); + void SynchronizeClient(int playerID); //globals ConfigUtility configUtil; UDPNetworkUtility netUtil; //members + bool running = false; + Delta delta; std::map clientMap; int maxClients = SDLNET_MAX_UDPCHANNELS; int uniqueIndex = 0;