diff --git a/client/client_application.cpp b/client/client_application.cpp index d50dcd2..16ad96c 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -38,7 +38,7 @@ #include "options_menu.hpp" #include "lobby_menu.hpp" #include "in_world.hpp" -#include "in_combat.hpp" +//#include "in_combat.hpp" #include "clean_up.hpp" //------------------------- @@ -66,7 +66,7 @@ void ClientApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { throw(std::runtime_error("Failed to initialize SDL_net")); } - network.Open(0); + UDPNetworkUtility::GetSingleton().Open(0); std::cout << "Initialized SDL_net" << std::endl; //------------------------- @@ -151,7 +151,7 @@ void ClientApplication::Proc() { void ClientApplication::Quit() { std::cout << "Shutting down" << std::endl; - network.Close(); + UDPNetworkUtility::GetSingleton().Close(); SDLNet_Quit(); SDL_Quit(); std::cout << "Clean exit" << std::endl; @@ -176,16 +176,16 @@ void ClientApplication::LoadScene(SceneList sceneIndex) { activeScene = new OptionsMenu(); break; case SceneList::LOBBYMENU: - activeScene = new LobbyMenu(&network, &clientIndex, &accountIndex); + activeScene = new LobbyMenu(&clientIndex, &accountIndex); break; case SceneList::INWORLD: - activeScene = new InWorld(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); - break; - case SceneList::INCOMBAT: - activeScene = new InCombat(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new InWorld(&clientIndex, &accountIndex, &characterIndex, &characterMap); break; +// case SceneList::INCOMBAT: +// activeScene = new InCombat(&clientIndex, &accountIndex, &characterIndex, &characterMap); +// break; case SceneList::CLEANUP: - activeScene = new CleanUp(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new CleanUp(&clientIndex, &accountIndex, &characterIndex, &characterMap); break; default: throw(std::logic_error("Failed to recognize the scene index")); diff --git a/client/client_application.hpp b/client/client_application.hpp index eea274f..773243f 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -52,7 +52,6 @@ private: BaseScene* activeScene = nullptr; //shared parameters - UDPNetworkUtility network; int clientIndex = -1; int accountIndex = -1; int characterIndex = -1; diff --git a/client/main.cpp b/client/main.cpp index 57154b6..0903053 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -23,6 +23,7 @@ //singletons #include "config_utility.hpp" +#include "udp_network_utility.hpp" #include #include @@ -32,18 +33,22 @@ using namespace std; int main(int argc, char** argv) { try { //create the singletons - ClientApplication::Create(); ConfigUtility::Create(); + UDPNetworkUtility::Create(); //call the server's routines + ClientApplication::Create(); ClientApplication& app = ClientApplication::GetSingleton(); + app.Init(argc, argv); app.Proc(); app.Quit(); + ClientApplication::Delete(); + //delete the singletons ConfigUtility::Delete(); - ClientApplication::Delete(); + UDPNetworkUtility::Delete(); } catch(exception& e) { cerr << "Fatal exception thrown: " << e.what() << endl; diff --git a/client/scenes/clean_up.cpp b/client/scenes/clean_up.cpp index 87846d5..c5b5d0f 100644 --- a/client/scenes/clean_up.cpp +++ b/client/scenes/clean_up.cpp @@ -31,13 +31,11 @@ //------------------------- CleanUp::CleanUp( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, CharacterMap* argCharacterMap ): - network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), characterIndex(*argCharacterIndex), diff --git a/client/scenes/clean_up.hpp b/client/scenes/clean_up.hpp index 3ba76ce..ac39b44 100644 --- a/client/scenes/clean_up.hpp +++ b/client/scenes/clean_up.hpp @@ -45,7 +45,6 @@ class CleanUp : public BaseScene { public: //Public access members CleanUp( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, @@ -68,7 +67,7 @@ protected: void KeyUp(SDL_KeyboardEvent const&); //shared parameters - UDPNetworkUtility& network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); int& clientIndex; int& accountIndex; int& characterIndex; diff --git a/client/scenes/in_combat.cpp b/client/scenes/in_combat.cpp.txt similarity index 100% rename from client/scenes/in_combat.cpp rename to client/scenes/in_combat.cpp.txt diff --git a/client/scenes/in_combat.hpp b/client/scenes/in_combat.hpp.txt similarity index 100% rename from client/scenes/in_combat.hpp rename to client/scenes/in_combat.hpp.txt diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index dac3bce..8dbd0e6 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -35,13 +35,11 @@ //------------------------- InWorld::InWorld( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, CharacterMap* argCharacterMap ): - network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), characterIndex(*argCharacterIndex), diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 8dddfdf..c72ff73 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -49,7 +49,6 @@ class InWorld : public BaseScene { public: //Public access members InWorld( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, @@ -92,7 +91,7 @@ protected: void UpdateMap(); //shared parameters - UDPNetworkUtility& network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); int& clientIndex; int& accountIndex; int& characterIndex; diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 2e71a89..4b7b356 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -30,12 +30,7 @@ //Public access members //------------------------- -LobbyMenu::LobbyMenu( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex - ): - network(*argNetwork), +LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): clientIndex(*argClientIndex), accountIndex(*argAccountIndex) { diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index 96afaa0..43f2c99 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -40,11 +40,7 @@ class LobbyMenu : public BaseScene { public: //Public access members - LobbyMenu( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex - ); + LobbyMenu(int* const argClientIndex, int* const argAccountIndex); ~LobbyMenu(); protected: @@ -68,7 +64,7 @@ protected: //shared parameters ConfigUtility& config = ConfigUtility::GetSingleton(); - UDPNetworkUtility& network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); int& clientIndex; int& accountIndex; diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index 2b90df8..ad97a1c 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -25,7 +25,7 @@ #include -//NOTE: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network +//BUGFIX: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network //NOTE: don't confuse SerialPacket with UDPpacket void UDPNetworkUtility::Open(int port) { diff --git a/common/network/udp_network_utility.hpp b/common/network/udp_network_utility.hpp index 4ebdd98..d812ad7 100644 --- a/common/network/udp_network_utility.hpp +++ b/common/network/udp_network_utility.hpp @@ -22,15 +22,15 @@ #ifndef UDPNETWORKUTILITY_HPP_ #define UDPNETWORKUTILITY_HPP_ +//common +#include "serial_packet.hpp" +#include "singleton.hpp" + +//APIs #include "SDL/SDL_net.h" -#include "serial_packet.hpp" - -class UDPNetworkUtility { +class UDPNetworkUtility : public Singleton { public: - UDPNetworkUtility() = default; - ~UDPNetworkUtility() = default; - void Open(int port); void Close(); @@ -65,6 +65,11 @@ public: return socket; } private: + friend Singleton; + + UDPNetworkUtility() = default; + ~UDPNetworkUtility() = default; + UDPsocket socket = nullptr; UDPpacket* packet = nullptr; }; diff --git a/server/main.cpp b/server/main.cpp index 4fbc24a..71ebb7c 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -23,6 +23,7 @@ //singletons #include "config_utility.hpp" +#include "udp_network_utility.hpp" #include #include @@ -33,17 +34,21 @@ int main(int argc, char** argv) { try { //create the singletons ConfigUtility::Create(); - ServerApplication::Create(); + UDPNetworkUtility::Create(); //call the server's routines + ServerApplication::Create(); ServerApplication& app = ServerApplication::GetSingleton(); + app.Init(argc, argv); app.Proc(); app.Quit(); + ServerApplication::Delete(); + //delete the singletons ConfigUtility::Delete(); - ServerApplication::Delete(); + UDPNetworkUtility::Delete(); } catch(exception& e) { cerr << "Fatal exception thrown: " << e.what() << endl; diff --git a/server/server_application.hpp b/server/server_application.hpp index 6c99d2d..17b6e41 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -85,7 +85,7 @@ private: //APIs and utilities sqlite3* database = nullptr; lua_State* luaState = nullptr; - UDPNetworkUtility network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton(); //simple tables