diff --git a/client/lobby.cpp b/client/lobby.cpp index 311ce14..462f6de 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -68,17 +68,40 @@ void Lobby::Receive() { while(netUtil->Receive()) { memcpy(&packet, netUtil->GetInData(), sizeof(PacketData)); switch(packet.type) { +// case PacketList::NONE: +// // +// break; +// case PacketList::PING: +// // +// break; case PacketList::PONG: PushServer(&packet); break; +// case PacketList::JOINREQUEST: +// // +// break; case PacketList::JOINCONFIRM: - //TODO: enter the game - PacketData jc; - memcpy(&jc, netUtil->GetInData(), sizeof(PacketData)); - *playerID = jc.joinConfirm.playerID; + 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: +// // +// break; +// case PacketList::SYNCHRONIZE: +// // +// break; +// case PacketList::NEWPLAYER: +// // +// break; +// case PacketList::DELETEPLAYER: +// // +// break; +// case PacketList::MOVEMENT: +// // +// break; } } } @@ -155,10 +178,11 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) { void Lobby::PingNetwork() { //ping the network - PacketData packet; - packet.type = PacketList::PING; - netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast(&packet), sizeof(PacketData)); + PacketData p; + p.type = PacketList::PING; + netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast(&p), sizeof(PacketData)); //reset the server list + //TODO: enable this // serverVector.clear(); } @@ -173,9 +197,9 @@ void Lobby::JoinRequest(ServerData* server) { if (!server) { return; } - PacketData packet; - packet.type = PacketList::JOINREQUEST; - snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); - snprintf(packet.joinRequest.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); - netUtil->Send(&server->address, reinterpret_cast(&packet), sizeof(PacketData)); + PacketData p; + p.type = PacketList::JOINREQUEST; + snprintf(p.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); + snprintf(p.joinRequest.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); + netUtil->Send(&server->address, reinterpret_cast(&p), sizeof(PacketData)); } diff --git a/common/packet_list.hpp b/common/packet_list.hpp index b23cf21..b1c749f 100644 --- a/common/packet_list.hpp +++ b/common/packet_list.hpp @@ -8,20 +8,20 @@ #define PACKET_STRING_SIZE 100 enum class PacketList { - NONE, + NONE = 0, //connections - PING, - PONG, - JOINREQUEST, - JOINCONFIRM, - DISCONNECT, + PING = 1, + PONG = 2, + JOINREQUEST = 3, + JOINCONFIRM = 4, + DISCONNECT = 5, //information control - SYNCHRONIZE, - NEWPLAYER, - DELETEPLAYER, - MOVEMENT, + SYNCHRONIZE = 6, + NEWPLAYER = 7, + DELETEPLAYER = 8, + MOVEMENT = 9, }; //------------------------- diff --git a/docs/TODO.txt b/docs/TODO.txt index 7be0f57..997fc62 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -1,2 +1,5 @@ +Each client needs to know about each other client now. + + Have multiple players moving around the server at the same time keep the docs up to date!!!!! \ No newline at end of file diff --git a/server/server_application.cpp b/server/server_application.cpp index d951eb6..3364d68 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -16,12 +16,52 @@ void ServerApplication::Init() { void ServerApplication::Proc() { while(running) { - HandleInput(); - UpdateWorld(); - HandleOutput(); + //handle input + while(netUtil.Receive()) { + PacketData packet; + memcpy(reinterpret_cast(&packet), netUtil.GetInData(), sizeof(PacketData)); + switch(packet.type) { +// case PacketList::NONE: +// // +// break; + case PacketList::PING: + Ping(&packet); + break; +// case PacketList::PONG: +// // +// break; + case PacketList::JOINREQUEST: + JoinRequest(&packet); + break; +// case PacketList::JOINCONFIRM: +// // +// break; + case PacketList::DISCONNECT: + Disconnect(packet.disconnect.playerID); + break; +// case PacketList::SYNCHRONIZE: +// // +// break; +// case PacketList::NEWPLAYER: +// // +// break; +// case PacketList::DELETEPLAYER: +// // +// break; + case PacketList::MOVEMENT: + Movement(&packet); + break; + } + } + //update the world + delta.Calculate(); + for (auto it : clientMap) { + it.second.Update(delta.GetDelta()); + } + + //handle output... + //TODO... - //debug -// running = false; SDL_Delay(10); } } @@ -31,56 +71,17 @@ void ServerApplication::Quit() { SDLNet_Quit(); } -void ServerApplication::HandleInput() { - //accept new connections - //accept updates from the clients - //read the updates from the clients into internal containers - PacketData packet; - while(netUtil.Receive()) { - memcpy(reinterpret_cast(&packet), netUtil.GetInData(), sizeof(PacketData)); - switch(packet.type) { - case PacketList::PING: - Ping(&packet); - break; - case PacketList::JOINREQUEST: - JoinRequest(&packet); - break; - case PacketList::DISCONNECT: - Disconnect(packet.disconnect.playerID); - break; - case PacketList::MOVEMENT: - Movement(&packet); - break; - } - } -} - -void ServerApplication::UpdateWorld() { - //update internals ie. - // ai - // loot drops - delta.Calculate(); - for (auto it : clientMap) { - it.second.Update(delta.GetDelta()); - } -} - -void ServerApplication::HandleOutput() { - //send all information to new connections - //selective updates to existing connectons -} - //------------------------- //network commands //------------------------- void ServerApplication::Ping(PacketData* packet) { + //respond to pings with the server name if (!packet) { return; } - //respond to pings with the server name packet->type = PacketList::PONG; - sprintf(packet->pong.metadata, "%s",configUtil.CString("servername")); + snprintf(packet->pong.metadata,PACKET_STRING_SIZE, "%s",configUtil.CString("servername")); netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast(packet), sizeof(PacketData)); } @@ -95,14 +96,16 @@ void ServerApplication::JoinRequest(PacketData* packet) { clientMap[playerID].channel = netUtil.Bind(&netUtil.GetInPacket()->address, -1); clientMap[playerID].handle = packet->joinRequest.handle; clientMap[playerID].avatar = packet->joinRequest.avatar; - cout << "New player: " << playerID << endl; //debug #ifdef DEBUG - cout << "playerID: " << clientMap[playerID].playerID << endl; - cout << "channel: " << clientMap[playerID].channel << endl; - cout << "handle: " << clientMap[playerID].handle << endl; - cout << "avatar: " << clientMap[playerID].avatar << endl; + cout << "New player:" << endl; + cout << "\tplayerID: " << clientMap[playerID].playerID << endl; + cout << "\tchannel: " << clientMap[playerID].channel << endl; + cout << "\thandle: " << clientMap[playerID].handle << endl; + cout << "\tavatar: " << clientMap[playerID].avatar << endl; +#else + cout << "New player: " << playerID << endl; #endif //join confirm @@ -110,16 +113,24 @@ void ServerApplication::JoinRequest(PacketData* packet) { p.type = PacketList::JOINCONFIRM; p.joinConfirm.playerID = clientMap[playerID].playerID; netUtil.Send(clientMap[playerID].channel, &p, sizeof(PacketData)); + + //TODO: NewPlayer() + +#ifdef DEBUG + cout << "current players: " << clientMap.size() << endl; +#endif } void ServerApplication::Disconnect(int playerID) { - //TODO: Delete player if (clientMap.find(playerID) == clientMap.end()) { return; } cout << "disconnecting: " << playerID << endl; netUtil.Unbind(clientMap[playerID].channel); clientMap.erase(playerID); + + //TODO: Delete player + #ifdef DEBUG cout << "current players: " << clientMap.size() << endl; #endif diff --git a/server/server_application.hpp b/server/server_application.hpp index 61011c9..2a1d2d8 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -21,10 +21,6 @@ public: void Quit(); private: - void HandleInput(); - void UpdateWorld(); - void HandleOutput(); - //network commands void Ping(PacketData*); void JoinRequest(PacketData*);