diff --git a/source/client/net.cpp b/source/client/net.cpp index ed171f6..0fc85ba 100644 --- a/source/client/net.cpp +++ b/source/client/net.cpp @@ -8,7 +8,7 @@ #include #include -void WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game) +void WaitForOrdersFromClient(const ClientInfo info, CTTRTSGame &game, std::mutex &mut) { char buffer[1028]; // buffer for orders @@ -37,13 +37,13 @@ void WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame mut.unlock(); } -void GetOrdersFromClients(std::vector &myClients, CTTRTSGame &game, std::mutex &gameMutex) +void WaitForOrdersFromClients(std::vector &myClients, CTTRTSGame &game, std::mutex &gameMutex) { // Spawn threads std::vector clientThreads; for(auto client : myClients) { - std::thread clientThread(WaitForOrdersFromClient, client, ref(gameMutex), std::ref(game)); + std::thread clientThread(WaitForOrdersFromClient, client, std::ref(game), ref(gameMutex)); clientThreads.push_back(move(clientThread)); } @@ -54,7 +54,7 @@ void GetOrdersFromClients(std::vector &myClients, CTTRTSGame &game, } } -void SendGameInfoToClients(std::vector &myClients, const CTTRTSGame &game, std::mutex &gameMutex) +void SendGamestateToClients(std::vector &myClients, const CTTRTSGame &game, std::mutex &gameMutex) { gameMutex.lock(); std::string gamestate_string = GetStringFromGame(game); @@ -90,7 +90,7 @@ void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameN gameNameString = gameName; } -void PerformServerHandshakeWithClient(const ClientInfo &client, const CTTRTSGame &game) +void PerformServerHandshake(const ClientInfo &client, const CTTRTSGame &game) { char handshake[64]; snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.GetName().c_str()); diff --git a/source/client/net.h b/source/client/net.h index eaa8737..e80c091 100644 --- a/source/client/net.h +++ b/source/client/net.h @@ -26,24 +26,33 @@ struct ClientInfo player_t player; }; -void WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game); +// Wait for orders from a client, will not return until client has send valid orders +// Will automatically add orders to the game +void WaitForOrdersFromClient(const ClientInfo info, CTTRTSGame &game, std::mutex &mut); -void GetOrdersFromClients(std::vector &myClients, CTTRTSGame &game, std::mutex &gameMutex); +// Iterates through a list of clients calling WaitForOrdersFromClient +void WaitForOrdersFromClients(std::vector &myClients, CTTRTSGame &game, std::mutex &gameMutex); -void SendGameInfoToClients(std::vector &myClients, const CTTRTSGame &game, std::mutex &gameMutex); +// Sends current gamestate to each client +void SendGamestateToClients(std::vector &myClients, const CTTRTSGame &game, std::mutex &gameMutex); +// Tries to bind to a socket, will attempt 10 times with longer waits between void TryBindSocket(int sockfd, sockaddr_in &serv_addr); -void PerformServerHandshakeWithClient(const ClientInfo &client, const CTTRTSGame &game); +// Perform the server side handshake with a client +void PerformServerHandshake(const ClientInfo &client, const CTTRTSGame &game); +// Perform the client side handshake with the server void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameNameString); +// For local fatal errors inline void fatal_error(const char *msg) { std::cerr<