Fix server functions being used wrongly
This commit is contained in:
parent
a81b4ff8d0
commit
bccd043d2c
1 changed files with 34 additions and 31 deletions
|
@ -28,24 +28,7 @@ struct ClientInfo
|
||||||
int clientsockfd;
|
int clientsockfd;
|
||||||
};
|
};
|
||||||
|
|
||||||
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex)
|
int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game)
|
||||||
{
|
|
||||||
// Spawn threads
|
|
||||||
std::vector<std::thread> 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 )
|
|
||||||
{
|
{
|
||||||
char buffer[1028]; // buffer for orders
|
char buffer[1028]; // buffer for orders
|
||||||
memset(buffer,0,sizeof(buffer));
|
memset(buffer,0,sizeof(buffer));
|
||||||
|
@ -68,6 +51,37 @@ int waitForOrdersFromClient(const ClientInfo info, std::mutex& mut, CTTRTSGame&
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex)
|
||||||
|
{
|
||||||
|
// Spawn threads
|
||||||
|
std::vector<std::thread> 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<ClientInfo> &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[])
|
int runServer(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
std::cout<<"Setting up server on port "<<TTRTS_PORT<<std::endl;
|
std::cout<<"Setting up server on port "<<TTRTS_PORT<<std::endl;
|
||||||
|
@ -156,24 +170,13 @@ int runServer(int argc, char* argv[])
|
||||||
// Loop for each turn
|
// Loop for each turn
|
||||||
while ( !game.GameOver() )
|
while ( !game.GameOver() )
|
||||||
{
|
{
|
||||||
// Grab the current game state string
|
|
||||||
gameMutex.lock();
|
|
||||||
std::string gamestate_string = GetStringFromGame(game);
|
|
||||||
gameMutex.unlock();
|
|
||||||
|
|
||||||
// Send data to clients
|
// Send data to clients
|
||||||
std::cout<<"Sending clients gamedata"<<std::endl;
|
std::cout<<"Sending clients gamedata"<<std::endl;
|
||||||
for (auto client : myClients)
|
SendGameInfoToClients(myClients, game, gameMutex);
|
||||||
{
|
|
||||||
// Write to the socket with the buffer
|
|
||||||
if ( write( client.clientsockfd, gamestate_string.c_str(), gamestate_string.length() ) < 0 )
|
|
||||||
error("ERROR sending to client");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for orders from clients
|
// Wait for orders from clients
|
||||||
std::cout<<"Waiting for client orders"<<std::endl;
|
std::cout<<"Waiting for client orders"<<std::endl;
|
||||||
|
GetOrdersFromClients(myClients, game, gameMutex);
|
||||||
SendGameInfoToClients(myClients, game, gameMutex);
|
|
||||||
|
|
||||||
std::cout<<"Orders recieved, simulating turn"<<std::endl;
|
std::cout<<"Orders recieved, simulating turn"<<std::endl;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue