Some more refactoring with additional comments

Server now performs handshake instantly
This commit is contained in:
mdiluzio 2015-01-04 11:22:54 +00:00
parent 5c8666d4fb
commit b141314434
3 changed files with 32 additions and 23 deletions

View file

@ -8,7 +8,7 @@
#include <string.h>
#include <unistd.h>
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<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex)
void WaitForOrdersFromClients(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));
std::thread clientThread(WaitForOrdersFromClient, client, std::ref(game), ref(gameMutex));
clientThreads.push_back(move(clientThread));
}
@ -54,7 +54,7 @@ void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game,
}
}
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex)
void SendGamestateToClients(std::vector<ClientInfo> &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());

View file

@ -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<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex);
// Iterates through a list of clients calling WaitForOrdersFromClient
void WaitForOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex);
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex);
// Sends current gamestate to each client
void SendGamestateToClients(std::vector<ClientInfo> &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<<msg<<std::endl;
exit(1);
}
// For system fatal errors (ie. functions that set errno)
inline void fatal_perror(const char *msg)
{
perror(msg);

View file

@ -81,20 +81,20 @@ void RunServerForGame(CTTRTSGame &game)
std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<< std::endl;
// Fill out client info struct
ClientInfo clientInfo;
clientInfo.cli_addr = cli_addr;
clientInfo.clientsockfd = clientsockfd;
clientInfo.player = *player_iterator;
player_iterator++;
myClients.push_back(clientInfo);
}
// Handshake with client
PerformServerHandshake(clientInfo, game);
// Perform the initial handshake with clients
for( auto client : myClients )
{
// Handshake currently just player
PerformServerHandshakeWithClient(client, game);
// Add out client info to our list
myClients.push_back(clientInfo);
// Iterate to next player
player_iterator++;
}
std::cout<<"All clients connected"<< std::endl;
@ -107,11 +107,11 @@ void RunServerForGame(CTTRTSGame &game)
{
// Send data to clients
std::cout<<"Sending clients gamedata"<< std::endl;
SendGameInfoToClients(myClients, game, gameMutex);
SendGamestateToClients(myClients, game, gameMutex);
// Wait for orders from clients
std::cout<<"Waiting for client orders"<< std::endl;
GetOrdersFromClients(myClients, game, gameMutex);
WaitForOrdersFromClients(myClients, game, gameMutex);
std::cout<<"Orders recieved, simulating turn"<< std::endl;
@ -122,7 +122,7 @@ void RunServerForGame(CTTRTSGame &game)
}
// Send final state to all the clients
SendGameInfoToClients(myClients, game, gameMutex);
SendGamestateToClients(myClients, game, gameMutex);
}
int runServer(int argc, char* argv[])