From 1ab963099dc02e484bbdeb006a14e4c3f792733c Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 24 May 2013 15:39:42 +1000 Subject: [PATCH] Connections and disconnections between client and server are functioning --- client/in_game.cpp | 10 +++++++++- client/in_game.hpp | 3 ++- client/lobby.cpp | 11 ++++++++++- client/lobby.hpp | 6 ++++-- client/scene_manager.cpp | 4 ++-- client/scene_manager.hpp | 1 + common/packet_list.hpp | 1 + common/udp_network_utility.cpp | 1 + server/client_data.hpp | 5 +++++ server/server_application.cpp | 36 ++++++++++++++++++++++++++-------- 10 files changed, 63 insertions(+), 15 deletions(-) diff --git a/client/in_game.cpp b/client/in_game.cpp index a37e857..2c40566 100644 --- a/client/in_game.cpp +++ b/client/in_game.cpp @@ -8,16 +8,24 @@ using namespace std; //Public access members //------------------------- -InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) { +InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil, int* ID) { #ifdef DEBUG cout << "entering InGame" << endl; #endif configUtil = cUtil; surfaceMgr = sMgr; netUtil = nUtil; + playerID = ID; + cout << "playerID: " << *playerID << endl; } InGame::~InGame() { + PacketData p; + p.type = PacketList::DISCONNECT; + p.disconnect.playerID = *playerID; + *playerID = -1; + netUtil->Send(0, &p, sizeof(PacketData)); + netUtil->Unbind(0); #ifdef DEBUG cout << "leaving InGame" << endl; #endif diff --git a/client/in_game.hpp b/client/in_game.hpp index 9431b6f..3ab55fd 100644 --- a/client/in_game.hpp +++ b/client/in_game.hpp @@ -11,7 +11,7 @@ class InGame : public BaseScene { public: //Public access members - InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*); + InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID); virtual ~InGame(); protected: @@ -32,6 +32,7 @@ protected: ConfigUtility* configUtil = nullptr; SurfaceManager* surfaceMgr = nullptr; UDPNetworkUtility* netUtil = nullptr; + int* playerID; }; #endif diff --git a/client/lobby.cpp b/client/lobby.cpp index eece608..311ce14 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -9,7 +9,7 @@ using namespace std; //Public access members //------------------------- -Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil) { +Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUtil, int* ID) { #ifdef DEBUG cout << "entering Lobby" << endl; #endif @@ -17,6 +17,7 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti configUtil = cUtil; surfaceMgr = sMgr; netUtil = nUtil; + playerID = ID; //members font.SetSurface(surfaceMgr->Get("font")); @@ -72,6 +73,11 @@ void Lobby::Receive() { break; case PacketList::JOINCONFIRM: //TODO: enter the game + PacketData jc; + memcpy(&jc, netUtil->GetInData(), sizeof(PacketData)); + *playerID = jc.joinConfirm.playerID; + netUtil->Bind(&netUtil->GetInPacket()->address, 0); + SetNextScene(SceneList::INGAME); break; } } @@ -164,6 +170,9 @@ void Lobby::PushServer(PacketData* packet) { } 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")); diff --git a/client/lobby.hpp b/client/lobby.hpp index 1fc14ae..5d1a0fa 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -18,7 +18,7 @@ class Lobby : public BaseScene { public: //Public access members - Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*); + Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID); virtual ~Lobby(); protected: @@ -46,11 +46,13 @@ protected: void PushServer(PacketData*); void JoinRequest(ServerData*); - //members + //globals ConfigUtility* configUtil = nullptr; SurfaceManager* surfaceMgr = nullptr; UDPNetworkUtility* netUtil = nullptr; + int* playerID; + //members RasterFont font; std::map buttonMap; diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index d56eb58..8465cdc 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -104,10 +104,10 @@ void SceneManager::LoadScene(SceneList sceneIndex) { activeScene = new MainMenu(&configUtil, &surfaceMgr); break; case SceneList::LOBBY: - activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil); + activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil, &playerID); break; case SceneList::INGAME: - activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil); + activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil, &playerID); break; #ifdef DEBUG diff --git a/client/scene_manager.hpp b/client/scene_manager.hpp index 1583f0c..73cf554 100644 --- a/client/scene_manager.hpp +++ b/client/scene_manager.hpp @@ -32,6 +32,7 @@ private: ConfigUtility configUtil; SurfaceManager surfaceMgr; UDPNetworkUtility netUtil; + int playerID = -1; }; #endif diff --git a/common/packet_list.hpp b/common/packet_list.hpp index b6cebc8..52f48b0 100644 --- a/common/packet_list.hpp +++ b/common/packet_list.hpp @@ -51,6 +51,7 @@ struct JoinConfirm { struct Disconnect { PacketList type = PacketList::DISCONNECT; + int playerID; }; //------------------------- diff --git a/common/udp_network_utility.cpp b/common/udp_network_utility.cpp index 4209634..a7b5e08 100644 --- a/common/udp_network_utility.cpp +++ b/common/udp_network_utility.cpp @@ -96,6 +96,7 @@ int UDPNetworkUtility::Send(int channel, void* data, int len) { } int UDPNetworkUtility::Receive() { + memset(packIn->data, 0, packIn->maxlen); int ret = SDLNet_UDP_Recv(socket, packIn); if (ret < 0) { diff --git a/server/client_data.hpp b/server/client_data.hpp index 994e60b..97cfb1e 100644 --- a/server/client_data.hpp +++ b/server/client_data.hpp @@ -23,6 +23,11 @@ struct ClientData { Vector2 motion; std::string handle; std::string avatar; + enum class Command { + NORMAL, + SYNCHRONIZE, + PING, + }command = Command::NORMAL; }; #endif diff --git a/server/server_application.cpp b/server/server_application.cpp index 6e5f8fc..365b54d 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -84,16 +84,36 @@ void ServerApplication::Ping(PacketData* packet) { void ServerApplication::JoinRequest(PacketData* packet) { //TODO cout << "Join request..." << endl; -// if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) { -// //rejection -// return; -// } -// int ch = netUtil.Bind(&netUtil.GetInPacket()->address, -1); -// cout << ch << endl; + if (clientMap.size() >= maxClients) { + //rejection + return; + } + int playerID = uniqueIndex++; + clientMap[playerID].playerID = playerID; + clientMap[playerID].channel = netUtil.Bind(&netUtil.GetInPacket()->address, -1); + clientMap[playerID].handle = packet->joinRequest.handle; + clientMap[playerID].avatar = packet->joinRequest.avatar; + + //debug + cout << "playerID: " << playerID << ", " << clientMap[playerID].playerID << endl; + cout << "channel: " << clientMap[playerID].channel << endl; + cout << "handle: " << clientMap[playerID].handle << endl; + cout << "avatar: " << clientMap[playerID].avatar << endl; + + //join confirm + PacketData jc; + jc.type = PacketList::JOINCONFIRM; + jc.joinConfirm.playerID = clientMap[playerID].playerID; + netUtil.Send(clientMap[playerID].channel, &jc, sizeof(PacketData)); } -void ServerApplication::Disconnect(PacketData* packet) { - //TODO +void ServerApplication::Disconnect(PacketData* packet) { //TODO: use playerID here + //TODO: Delete player + int playerID = packet->disconnect.playerID; + cout << "disconnecting: " << playerID << endl; + netUtil.Unbind(clientMap[playerID].channel); + clientMap.erase(playerID); + cout << "current players: " << clientMap.size() << endl; } void ServerApplication::Movement(PacketData* packet) {