From b34b933dcdf3b098d712daf2793ae71a00492ed7 Mon Sep 17 00:00:00 2001 From: mdiluzio Date: Fri, 2 Jan 2015 19:51:14 +0000 Subject: [PATCH] Pull base netcode functions out into net files --- source/client/net.cpp | 64 +++++++++++++++++++++++++++++++++++- source/client/net.h | 25 ++++++++++++++ source/client/server.cpp | 70 ---------------------------------------- 3 files changed, 88 insertions(+), 71 deletions(-) diff --git a/source/client/net.cpp b/source/client/net.cpp index 35b5308..967aa73 100644 --- a/source/client/net.cpp +++ b/source/client/net.cpp @@ -1 +1,63 @@ -#include "net.h" \ No newline at end of file +#include "net.h" + +#include +#include + +#include +#include +#include +#include + +int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game) +{ + char buffer[1028]; // buffer for orders + memset(buffer,0,sizeof(buffer)); + + std::cout<<"Waiting for "< &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"); + } +} diff --git a/source/client/net.h b/source/client/net.h index 613486f..fdab7d1 100644 --- a/source/client/net.h +++ b/source/client/net.h @@ -1,9 +1,34 @@ #ifndef _TTRTS_NET_H_ #define _TTRTS_NET_H_ +#include +#include + +#include +#include + +#include +#include +#include +#include + #include #include +// Struct for net client info +struct ClientInfo +{ + sockaddr_in cli_addr; + int clientsockfd; + player_t player; +}; + +int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game); + +void GetOrdersFromClients(std::vector &myClients, CTTRTSGame &game, std::mutex &gameMutex); + +void SendGameInfoToClients(std::vector &myClients, const CTTRTSGame &game, std::mutex &gameMutex); + inline void error(const char *msg) { perror(msg); diff --git a/source/client/server.cpp b/source/client/server.cpp index b0b2c82..f9238b8 100644 --- a/source/client/server.cpp +++ b/source/client/server.cpp @@ -10,79 +10,9 @@ #include #include -#include -#include - -#include -#include -#include -#include - #include "net.h" #include "filesystem.h" -// Struct for net client info -struct ClientInfo -{ - sockaddr_in cli_addr; - int clientsockfd; - player_t player; -}; - -int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game) -{ - char buffer[1028]; // buffer for orders - memset(buffer,0,sizeof(buffer)); - - std::cout<<"Waiting for "< &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 "<