diff --git a/source/client/server.cpp b/source/client/server.cpp index 89cef9e..ecfef7f 100644 --- a/source/client/server.cpp +++ b/source/client/server.cpp @@ -28,24 +28,7 @@ struct ClientInfo int clientsockfd; }; -void SendGameInfoToClients(std::vector &myClients, const CTTRTSGame &game, std::mutex &gameMutex) -{ - // Spawn threads - std::vector clientThreads; - for(auto client : myClients) - { - std::thread clientThread(waitForOrdersFromClient, client, ref(gameMutex), std::ref(game)); - clientThreads.push_back(move(clientThread)); - } - - // Join up all the threads - for ( std::thread& thread : clientThreads ) - { - thread.join(); - } -} - -int waitForOrdersFromClient(const ClientInfo info, std::mutex& mut, CTTRTSGame& game ) +int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game) { char buffer[1028]; // buffer for orders memset(buffer,0,sizeof(buffer)); @@ -68,6 +51,37 @@ int waitForOrdersFromClient(const ClientInfo info, std::mutex& mut, CTTRTSGame& return 0; } +void GetOrdersFromClients(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)); + clientThreads.push_back(move(clientThread)); + } + + // Join up all the threads + for ( std::thread& thread : clientThreads ) + { + thread.join(); + } +} + +void SendGameInfoToClients(std::vector &myClients, const CTTRTSGame &game, std::mutex &gameMutex) +{ + gameMutex.lock(); + std::string gamestate_string = GetStringFromGame(game); + gameMutex.unlock(); + + for (auto client : myClients) + { + // Write to the socket with the buffer + if ( write( client.clientsockfd, gamestate_string.c_str(), gamestate_string.length() ) < 0 ) + error("ERROR sending to client"); + } +} + int runServer(int argc, char* argv[]) { std::cout<<"Setting up server on port "<