From 56e767bb5bf5de2430aa1fc24f260c1fcb380505 Mon Sep 17 00:00:00 2001 From: mdiluzio Date: Fri, 2 Jan 2015 17:14:16 +0000 Subject: [PATCH] Clients now connect and recieve gamestate information --- source/client/client.cpp | 13 ++++- source/client/filesystem.cpp | 28 +++++---- source/client/filesystem.h | 2 + source/client/server.cpp | 109 +++++++++++++++++++++++++---------- source/gen/gen.cpp | 2 +- 5 files changed, 109 insertions(+), 45 deletions(-) diff --git a/source/client/client.cpp b/source/client/client.cpp index ab20334..9401df2 100644 --- a/source/client/client.cpp +++ b/source/client/client.cpp @@ -71,10 +71,17 @@ int runClient(int argc, char* argv[]) while ( n >= 0 ) { - // Get the message to send - printf("Please enter the message: "); memset(buffer,0,sizeof(buffer)); - fgets(buffer,sizeof(buffer)-1,stdin); + + // Receive gamestate + if (read(sockfd,buffer,sizeof(buffer)-1) < 0) + error("ERROR reading from client"); + + std::cout< #include #include +#include #include #include @@ -14,35 +15,35 @@ #include #include +#include +#include #include "net.h" +#include "filesystem.h" -std::vector gClientThreads; - -int clientHandler( sockaddr_in cli_addr, socklen_t clilen, int clientsockfd) +// Struct for net client info +struct ClientInfo { - std::cout<<"Client connected from "<= 0 ) - { - // empty the buffer - memset(buffer,0,sizeof(buffer)); + // Read in the new socket + // read will block until the client has called write + // up to the full size of the buffer + if (read(info.clientsockfd,buffer,sizeof(buffer)-1) < 0) + error("ERROR reading from client"); - // Read in the new socket - // read will block until the client has called write - // up to the full size of the buffer - n = read(clientsockfd,buffer,sizeof(buffer)-1); - if (n < 0) - error("ERROR reading from socket"); + std::cout<<"Recieved orders from "< myClients; + std::cout<<"Waiting for clients"< clientThreads; + for(auto client : myClients) + { + std::thread clientThread(waitForOrdersFromClient,std::ref(client), std::ref(gameMutex), std::ref(game)); + clientThreads.push_back(std::move(clientThread)); + } + + // Join up all the threads + for ( std::thread& thread : clientThreads ) + { + thread.join(); + } + + // Step to the next turn + gameMutex.lock(); + game.SimulateToNextTurn(); + gameMutex.unlock(); + } + + + // end game and disconnect clients + // Return return 0; } \ No newline at end of file diff --git a/source/gen/gen.cpp b/source/gen/gen.cpp index 8265a53..0f782b8 100644 --- a/source/gen/gen.cpp +++ b/source/gen/gen.cpp @@ -33,7 +33,7 @@ int main() //------ { CTTRTSGame game(6, 6); - game.SetName("Tiny2player"); + game.SetName("Tiny2Player"); AddUnitToGame( player_t::Red, '<', uvector2(4, 2), game); AddUnitToGame( player_t::Red, '<', uvector2(4, 4), game);