From d50cf5b91e98bce38632e25b330bb9368d141cb8 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 3 Sep 2014 00:45:08 +1000 Subject: [PATCH 1/5] Fixed shoddy packet typing --- client/scenes/in_world.cpp | 5 +++-- client/scenes/in_world.hpp | 2 +- common/network/serial_utility.cpp | 2 ++ server/server_application.hpp | 4 ++-- server/server_logic.cpp | 4 ++-- server/server_methods.cpp | 6 +++--- todo.txt | 13 +++++-------- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index f78103f..ac7b255 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -271,7 +271,7 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) { void InWorld::HandlePacket(SerialPacket* const argPacket) { switch(argPacket->type) { case SerialPacketType::DISCONNECT: - HandleDisconnect(argPacket); + HandleDisconnect(static_cast(argPacket)); break; case SerialPacketType::CHARACTER_NEW: HandleCharacterNew(static_cast(argPacket)); @@ -292,7 +292,8 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) { } } -void InWorld::HandleDisconnect(SerialPacket* const argPacket) { +void InWorld::HandleDisconnect(ClientPacket* const argPacket) { + //TODO: More needed in the disconnection SetNextScene(SceneList::CLEANUP); } diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 517b86a..633ffc0 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -75,7 +75,7 @@ protected: //Network handlers void HandlePacket(SerialPacket* const); - void HandleDisconnect(SerialPacket* const); + void HandleDisconnect(ClientPacket* const); void HandleCharacterNew(CharacterPacket* const); void HandleCharacterDelete(CharacterPacket* const); void HandleCharacterUpdate(CharacterPacket* const); diff --git a/common/network/serial_utility.cpp b/common/network/serial_utility.cpp index f43d312..6f70999 100644 --- a/common/network/serial_utility.cpp +++ b/common/network/serial_utility.cpp @@ -40,6 +40,8 @@ void deserialCopy(void** buffer, void* data, int size) { *buffer = reinterpret_cast(*buffer) + size; } +//DOCS: The server and client MUST use the correct packet types + //main switch functions void serializePacket(void* buffer, SerialPacketBase* packet) { switch(packet->type) { diff --git a/server/server_application.hpp b/server/server_application.hpp index ecd08ab..acccb5c 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -61,10 +61,10 @@ private: void HandlePacket(SerialPacket* const); //basic connections - void HandleBroadcastRequest(SerialPacket* const); + void HandleBroadcastRequest(ServerPacket* const); void HandleJoinRequest(ClientPacket* const); void HandleDisconnect(ClientPacket* const); - void HandleShutdown(SerialPacket* const); + void HandleShutdown(ClientPacket* const); //map management void HandleRegionRequest(RegionPacket* const); diff --git a/server/server_logic.cpp b/server/server_logic.cpp index 94cc258..8fa450f 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -179,7 +179,7 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) { switch(argPacket->type) { //basic connections case SerialPacketType::BROADCAST_REQUEST: - HandleBroadcastRequest(static_cast(argPacket)); + HandleBroadcastRequest(static_cast(argPacket)); break; case SerialPacketType::JOIN_REQUEST: HandleJoinRequest(static_cast(argPacket)); @@ -188,7 +188,7 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) { HandleDisconnect(static_cast(argPacket)); break; case SerialPacketType::SHUTDOWN: - HandleShutdown(static_cast(argPacket)); + HandleShutdown(static_cast(argPacket)); break; //map management diff --git a/server/server_methods.cpp b/server/server_methods.cpp index d993b83..9a6beba 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -27,7 +27,7 @@ //basic connections //------------------------- -void ServerApplication::HandleBroadcastRequest(SerialPacket* const argPacket) { +void ServerApplication::HandleBroadcastRequest(ServerPacket* const argPacket) { //send the server's data ServerPacket newPacket; @@ -102,7 +102,7 @@ void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) { std::cout << "Disconnection, " << clientMap.size() << " clients and " << accountMgr.GetContainer()->size() << " accounts total" << std::endl; } -void ServerApplication::HandleShutdown(SerialPacket* const argPacket) { +void ServerApplication::HandleShutdown(ClientPacket* const argPacket) { //TODO: authenticate who is shutting the server down /*Pseudocode: if sender's account -> admin is not true then @@ -115,7 +115,7 @@ void ServerApplication::HandleShutdown(SerialPacket* const argPacket) { running = false; //disconnect all clients - SerialPacket newPacket; + ClientPacket newPacket; newPacket.type = SerialPacketType::DISCONNECT; PumpPacket(&newPacket); diff --git a/todo.txt b/todo.txt index 74cae7e..8e455b3 100644 --- a/todo.txt +++ b/todo.txt @@ -1,16 +1,13 @@ -TODO: Sort out the *_cast<> stuff -TODO: encapsulate the data structures -TODO: Ping-pong and keep alive system +TODO: Heartbeat systems +TODO: Rejection messages TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times TODO: Get the rooms working, even if only via hotkeys -TODO: Rejection messages -TODO: Move the map system into it's own namespace -TODO: The TileSheet class should implement the surface itself - TODO: Fix shoddy movement + +TODO: Move the map system into it's own namespace? +TODO: The TileSheet class should implement the surface itself TODO: make the whole thing more fault tolerant TODO: Authentication -TODO: server is slaved to the client TODO: Time delay for requesting region packets TODO: command line parameters overriding config.cfg settings From 5577387d61b387f1396469b88dcb0af53f16ed02 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 3 Sep 2014 00:53:55 +1000 Subject: [PATCH 2/5] Added basic hearbeat framework --- client/scenes/in_world.cpp | 17 +++++++++++++++++ client/scenes/in_world.hpp | 2 ++ server/server_application.hpp | 2 ++ server/server_logic.cpp | 6 ++++++ server/server_methods.cpp | 10 ++++++++++ 5 files changed, 37 insertions(+) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index ac7b255..3c20ad6 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -270,6 +270,12 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) { void InWorld::HandlePacket(SerialPacket* const argPacket) { switch(argPacket->type) { + case SerialPacketType::PING: + HandlePing(static_cast(argPacket)); + break; + case SerialPacketType::PONG: + HandlePong(static_cast(argPacket)); + break; case SerialPacketType::DISCONNECT: HandleDisconnect(static_cast(argPacket)); break; @@ -292,6 +298,17 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) { } } +void InWorld::HandlePing(ServerPacket* const argPacket) { + ServerPacket newPacket; + newPacket.type = SerialPacketType::PONG; + network.SendTo(&argPacket->srcAddress, &newPacket); +} + +void InWorld::HandlePong(ServerPacket* const argPacket) { + //TODO: InWorld::HandlePong() +} + + void InWorld::HandleDisconnect(ClientPacket* const argPacket) { //TODO: More needed in the disconnection SetNextScene(SceneList::CLEANUP); diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 633ffc0..a2479b7 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -75,6 +75,8 @@ protected: //Network handlers void HandlePacket(SerialPacket* const); + void HandlePing(ServerPacket* const); + void HandlePong(ServerPacket* const); void HandleDisconnect(ClientPacket* const); void HandleCharacterNew(CharacterPacket* const); void HandleCharacterDelete(CharacterPacket* const); diff --git a/server/server_application.hpp b/server/server_application.hpp index acccb5c..df68357 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -61,6 +61,8 @@ private: void HandlePacket(SerialPacket* const); //basic connections + void HandlePing(ServerPacket* const); + void HandlePong(ServerPacket* const); void HandleBroadcastRequest(ServerPacket* const); void HandleJoinRequest(ClientPacket* const); void HandleDisconnect(ClientPacket* const); diff --git a/server/server_logic.cpp b/server/server_logic.cpp index 8fa450f..49dc68b 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -178,6 +178,12 @@ void ServerApplication::Quit() { void ServerApplication::HandlePacket(SerialPacket* const argPacket) { switch(argPacket->type) { //basic connections + case SerialPacketType::PING: + HandlePing(static_cast(argPacket)); + break; + case SerialPacketType::PONG: + HandlePong(static_cast(argPacket)); + break; case SerialPacketType::BROADCAST_REQUEST: HandleBroadcastRequest(static_cast(argPacket)); break; diff --git a/server/server_methods.cpp b/server/server_methods.cpp index 9a6beba..4358e15 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -27,6 +27,16 @@ //basic connections //------------------------- +void ServerApplication::HandlePing(ServerPacket* const argPacket) { + ServerPacket newPacket; + newPacket.type = SerialPacketType::PONG; + network.SendTo(&argPacket->srcAddress, &newPacket); +} + +void ServerApplication::HandlePong(ServerPacket* const argPacket) { + //TODO: ServerApplications::HandlePong() +} + void ServerApplication::HandleBroadcastRequest(ServerPacket* const argPacket) { //send the server's data ServerPacket newPacket; From 06922dc8209e523d1cc5aec103a0353b26bb2530 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 3 Sep 2014 01:21:17 +1000 Subject: [PATCH 3/5] Client-side heartbeat is working --- client/scenes/in_world.cpp | 22 ++++++++++++++++++++-- client/scenes/in_world.hpp | 7 +++++++ todo.txt | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 3c20ad6..5cd2dc6 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -140,6 +140,20 @@ void InWorld::Update() { //update the camera camera.x = localCharacter->GetOrigin().x - camera.marginX; camera.y = localCharacter->GetOrigin().y - camera.marginY; + + //check the connection + if (Clock::now() - lastBeat > std::chrono::seconds(5)) { + if (attemptedBeats > 2) { + throw(std::runtime_error("Connection lost")); + } + + ServerPacket newPacket; + newPacket.type = SerialPacketType::PING; + network.SendTo(Channels::SERVER, &newPacket); + + attemptedBeats++; + lastBeat = Clock::now(); + } } void InWorld::FrameEnd() { @@ -305,9 +319,13 @@ void InWorld::HandlePing(ServerPacket* const argPacket) { } void InWorld::HandlePong(ServerPacket* const argPacket) { - //TODO: InWorld::HandlePong() -} + if (network.GetIPAddress(Channels::SERVER)->host != argPacket->srcAddress.host) { + throw(std::runtime_error("Heartbeat message received from unknown source")); + } + attemptedBeats = 0; + lastBeat = Clock::now(); +} void InWorld::HandleDisconnect(ClientPacket* const argPacket) { //TODO: More needed in the disconnection diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index a2479b7..15fcdce 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -46,6 +46,8 @@ //STL #include +#include + class InWorld : public BaseScene { public: //Public access members @@ -121,6 +123,11 @@ protected: //game Character* localCharacter = nullptr; + + //connections + typedef std::chrono::steady_clock Clock; + Clock::time_point lastBeat = Clock::now(); + int attemptedBeats = 0; }; #endif diff --git a/todo.txt b/todo.txt index 8e455b3..aec3faf 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,6 @@ TODO: Heartbeat systems TODO: Rejection messages +TODO: The error handling is terrible TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times TODO: Get the rooms working, even if only via hotkeys TODO: Fix shoddy movement From 79c7e48139115e40fec09ede025534045afb6063 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 3 Sep 2014 01:59:53 +1000 Subject: [PATCH 4/5] UDPNetworkUtility accepts addresses by value, encapsulated ClientData I started encapsulating ClientData, and I added the internals for the heartbeat ssytem. However, when I took a look inside UDPNetworkUtility, I realized that I didn't have to pass the IPaddresses by reference anymore. Therefore, I've changed it to accept the addresses by value, and I'm committing that change right away before I finish the heartbeat system. This engine is really shaping up, I think. --- client/scenes/in_world.cpp | 2 +- client/scenes/lobby_menu.cpp | 4 ++-- common/network/udp_network_utility.cpp | 18 +++++++-------- common/network/udp_network_utility.hpp | 6 ++--- server/client_data.cpp | 32 ++++++++++++++++++++++++++ server/client_data.hpp | 26 +++++++++++++++++++-- server/server_methods.cpp | 16 ++++++------- 7 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 server/client_data.cpp diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 5cd2dc6..d8b0d35 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -315,7 +315,7 @@ void InWorld::HandlePacket(SerialPacket* const argPacket) { void InWorld::HandlePing(ServerPacket* const argPacket) { ServerPacket newPacket; newPacket.type = SerialPacketType::PONG; - network.SendTo(&argPacket->srcAddress, &newPacket); + network.SendTo(argPacket->srcAddress, &newPacket); } void InWorld::HandlePong(ServerPacket* const argPacket) { diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index e04259e..88d09e6 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -217,7 +217,7 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) { void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { clientIndex = argPacket->clientIndex; accountIndex = argPacket->accountIndex; - network.Bind(&argPacket->srcAddress, Channels::SERVER); + network.Bind(argPacket->srcAddress, Channels::SERVER); SetNextScene(SceneList::INWORLD); //send this player's character info @@ -251,6 +251,6 @@ void LobbyMenu::SendJoinRequest() { strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE); //join the selected server - network.SendTo(&selection->address, &packet); + network.SendTo(selection->address, &packet); selection = nullptr; } diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index bfcec52..6a2d243 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -55,11 +55,11 @@ int UDPNetworkUtility::Bind(const char* ip, int port, int channel) { throw(std::runtime_error("Failed to resolve a host")); } - return Bind(&add, channel); + return Bind(add, channel); } -int UDPNetworkUtility::Bind(IPaddress* add, int channel) { - int ret = SDLNet_UDP_Bind(socket, channel, add); +int UDPNetworkUtility::Bind(IPaddress add, int channel) { + int ret = SDLNet_UDP_Bind(socket, channel, &add); if (ret < 0) { throw(std::runtime_error("Failed to bind to a channel")); @@ -82,17 +82,17 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, void* data, int len) { throw(std::runtime_error("Failed to resolve a host")); } - SendTo(&add, data, len); + SendTo(add, data, len); } -int UDPNetworkUtility::SendTo(IPaddress* add, void* data, int len) { +int UDPNetworkUtility::SendTo(IPaddress add, void* data, int len) { if (len > packet->maxlen) { throw(std::runtime_error("The buffer is to large for the UDPpacket")); } memset(packet->data, 0, packet->maxlen); memcpy(packet->data, data, len); packet->len = len; - packet->address = *add; + packet->address = add; int ret = SDLNet_UDP_Send(socket, -1, packet); @@ -162,14 +162,14 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacketBase* serial throw(std::runtime_error("Failed to resolve a host")); } - SendTo(&add, serialPacket); + SendTo(add, serialPacket); } -int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacketBase* serialPacket) { +int UDPNetworkUtility::SendTo(IPaddress add, SerialPacketBase* serialPacket) { memset(packet->data, 0, packet->maxlen); serializePacket(packet->data, serialPacket); packet->len = PACKET_BUFFER_SIZE; - packet->address = *add; + packet->address = add; int ret = SDLNet_UDP_Send(socket, -1, packet); diff --git a/common/network/udp_network_utility.hpp b/common/network/udp_network_utility.hpp index 3928ff5..eef3de3 100644 --- a/common/network/udp_network_utility.hpp +++ b/common/network/udp_network_utility.hpp @@ -36,7 +36,7 @@ public: //bind to a channel int Bind(const char* ip, int port, int channel = -1); - int Bind(IPaddress* add, int channel = -1); + int Bind(IPaddress add, int channel = -1); void Unbind(int channel); IPaddress* GetIPAddress(int channel) { @@ -45,14 +45,14 @@ public: //send a buffer int SendTo(const char* ip, int port, void* data, int len); - int SendTo(IPaddress* add, void* data, int len); + int SendTo(IPaddress add, void* data, int len); int SendTo(int channel, void* data, int len); int SendToAllChannels(void* data, int len); int Receive(); //send a SerialPacketBase int SendTo(const char* ip, int port, SerialPacketBase* serialPacket); - int SendTo(IPaddress* add, SerialPacketBase* serialPacket); + int SendTo(IPaddress add, SerialPacketBase* serialPacket); int SendTo(int channel, SerialPacketBase* serialPacket); int SendToAllChannels(SerialPacketBase* serialPacket); int Receive(SerialPacketBase* serialPacket); diff --git a/server/client_data.cpp b/server/client_data.cpp new file mode 100644 index 0000000..56eddc6 --- /dev/null +++ b/server/client_data.cpp @@ -0,0 +1,32 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "client_data.hpp" + +int ClientData::IncrementAttempts() { + lastBeat = Clock::now(); + return attemptedBeats++; +} + +int ClientData::ResetAttempts() { + lastBeat = Clock::now(); + return attemptedBeats = 0; +} \ No newline at end of file diff --git a/server/client_data.hpp b/server/client_data.hpp index 4a9c5bc..0af85ed 100644 --- a/server/client_data.hpp +++ b/server/client_data.hpp @@ -24,9 +24,31 @@ #include "SDL/SDL_net.h" -struct ClientData { +#include + +//TODO: ClientManager? +class ClientData { +public: + typedef std::chrono::steady_clock Clock; + + ClientData() = default; + ClientData(IPaddress add): address(add) {} + ~ClientData() = default; + + IPaddress SetAddress(IPaddress add) { return address = add; } + IPaddress GetAddress() { return address; } + + Clock::time_point GetLastBeat() { return lastBeat; } + + int GetAttempts() { return attemptedBeats; } + int IncrementAttempts(); + int ResetAttempts(); + +private: IPaddress address = {0,0}; - //TODO: ping system? + + Clock::time_point lastBeat = Clock::now(); + int attemptedBeats = 0; }; #endif diff --git a/server/server_methods.cpp b/server/server_methods.cpp index 4358e15..390064f 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -30,7 +30,7 @@ void ServerApplication::HandlePing(ServerPacket* const argPacket) { ServerPacket newPacket; newPacket.type = SerialPacketType::PONG; - network.SendTo(&argPacket->srcAddress, &newPacket); + network.SendTo(argPacket->srcAddress, &newPacket); } void ServerApplication::HandlePong(ServerPacket* const argPacket) { @@ -46,13 +46,13 @@ void ServerApplication::HandleBroadcastRequest(ServerPacket* const argPacket) { newPacket.playerCount = characterMgr.GetContainer()->size(); newPacket.version = NETWORK_VERSION; - network.SendTo(&argPacket->srcAddress, static_cast(&newPacket)); + network.SendTo(argPacket->srcAddress, static_cast(&newPacket)); } void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) { //create the new client ClientData newClient; - newClient.address = argPacket->srcAddress; + newClient.SetAddress(argPacket->srcAddress); //load the user account //TODO: handle passwords @@ -69,7 +69,7 @@ void ServerApplication::HandleJoinRequest(ClientPacket* const argPacket) { newPacket.clientIndex = clientIndex; newPacket.accountIndex = accountIndex; - network.SendTo(&newClient.address, static_cast(&newPacket)); + network.SendTo(newClient.GetAddress(), static_cast(&newPacket)); //finished this routine clientMap[clientIndex++] = newClient; @@ -90,7 +90,7 @@ void ServerApplication::HandleDisconnect(ClientPacket* const argPacket) { //forward to the specified client network.SendTo( - &clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex() ].address, + clientMap[ accountMgr.GetAccount(argPacket->accountIndex)->GetClientIndex() ].GetAddress(), static_cast(argPacket) ); @@ -148,7 +148,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) { newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y); //send the content - network.SendTo(&argPacket->srcAddress, static_cast(&newPacket)); + network.SendTo(argPacket->srcAddress, static_cast(&newPacket)); } //------------------------- @@ -247,7 +247,7 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) { for (auto& it : *characterMgr.GetContainer()) { newPacket.characterIndex = it.first; CopyCharacterToPacket(&newPacket, it.first); - network.SendTo(&client.address, static_cast(&newPacket)); + network.SendTo(client.GetAddress(), static_cast(&newPacket)); } //TODO: more in HandleSynchronize() @@ -261,7 +261,7 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) { void ServerApplication::PumpPacket(SerialPacket* const argPacket) { for (auto& it : clientMap) { - network.SendTo(&it.second.address, argPacket); + network.SendTo(it.second.GetAddress(), argPacket); } } From 246a5ee541dbc4038a523614d96ab7e880701f57 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 3 Sep 2014 03:23:56 +1000 Subject: [PATCH 5/5] The server-side heartbeat is working and stable --- server/server_application.hpp | 1 + server/server_logic.cpp | 19 ++++++++++++ server/server_methods.cpp | 55 ++++++++++++++++++++++++++++++++++- todo.txt | 1 - 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/server/server_application.hpp b/server/server_application.hpp index df68357..105cffe 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -81,6 +81,7 @@ private: //utility methods //TODO: a function that only sends to characters in a certain proximity + void CleanupLostConnection(int index); void PumpPacket(SerialPacket* const); void PumpCharacterUnload(int uid); void CopyCharacterToPacket(CharacterPacket* const packet, int characterIndex); diff --git a/server/server_logic.cpp b/server/server_logic.cpp index 49dc68b..281c5b2 100644 --- a/server/server_logic.cpp +++ b/server/server_logic.cpp @@ -26,6 +26,7 @@ #include "utility.hpp" #include +#include #include #include @@ -144,6 +145,24 @@ void ServerApplication::Proc() { //update the internals //BUG: #30 Update the internals i.e. player positions + //TODO: This could be checked only every few seconds + //Check connections + for (auto& it : clientMap) { + if (std::chrono::steady_clock::now() - it.second.GetLastBeat() > std::chrono::seconds(5)) { + ServerPacket newPacket; + newPacket.type = SerialPacketType::PING; + network.SendTo(it.second.GetAddress(), &newPacket); + it.second.IncrementAttempts(); + } + + if (it.second.GetAttempts() > 2) { + CleanupLostConnection(it.first); + + //all iterators are invalid, so we can't continue + break; + } + } + //give the computer a break SDL_Delay(10); } diff --git a/server/server_methods.cpp b/server/server_methods.cpp index 390064f..bb3be96 100644 --- a/server/server_methods.cpp +++ b/server/server_methods.cpp @@ -34,7 +34,17 @@ void ServerApplication::HandlePing(ServerPacket* const argPacket) { } void ServerApplication::HandlePong(ServerPacket* const argPacket) { - //TODO: ServerApplications::HandlePong() + //find and update the specified client + + //BUGFIX: running multiple clients on one computer will result in matching host values; check the ports too + for (auto& it : clientMap) { + if (it.second.GetAddress().host == argPacket->srcAddress.host && + it.second.GetAddress().port == argPacket->srcAddress.port + ) { + it.second.ResetAttempts(); + break; + } + } } void ServerApplication::HandleBroadcastRequest(ServerPacket* const argPacket) { @@ -172,6 +182,8 @@ void ServerApplication::HandleCharacterNew(CharacterPacket* const argPacket) { return; } + //TODO: Make sure that a character's owner's account is loaded before continuing + //send this new character to all clients CharacterPacket newPacket; newPacket.type = SerialPacketType::CHARACTER_NEW; @@ -257,6 +269,47 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) { //utility methods //------------------------- +void ServerApplication::CleanupLostConnection(int clientIndex) { + //NOTE: This assumes each player has only one account and character at a time + + //find the account + int accountIndex = -1; + for (auto& it : *accountMgr.GetContainer()) { + if (it.second.GetClientIndex() == clientIndex) { + accountIndex = it.first; + break; + } + } + + //find the character + int characterIndex = -1; + for (auto& it : *characterMgr.GetContainer()) { + if (it.second.GetOwner() == accountIndex) { + characterIndex = it.first; + break; + } + } + + //send a dissconnection message just in case + ClientPacket newPacket; + newPacket.type = SerialPacketType::DISCONNECT; + network.SendTo(clientMap[clientIndex].GetAddress(), &newPacket); + + //clean up this mess + characterMgr.UnloadCharacter(characterIndex); + accountMgr.UnloadAccount(accountIndex); + clientMap.erase(clientIndex); + + PumpCharacterUnload(characterIndex); + + //output a message + std::cerr << "Connection lost: " << std::endl; + std::cerr << "\tClient: " << clientIndex << std::endl; + std::cerr << "\tAccount: " << accountIndex << std::endl; + std::cerr << "\tCharacter: " << characterIndex << std::endl; + std::cout << clientMap.size() << " clients and " << accountMgr.GetContainer()->size() << " accounts total" << std::endl; +} + //TODO: a function that only sends to characters in a certain proximity void ServerApplication::PumpPacket(SerialPacket* const argPacket) { diff --git a/todo.txt b/todo.txt index aec3faf..e8af752 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,3 @@ -TODO: Heartbeat systems TODO: Rejection messages TODO: The error handling is terrible TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times