Some more refactoring with additional comments
Server now performs handshake instantly
This commit is contained in:
		
							parent
							
								
									5c8666d4fb
								
							
						
					
					
						commit
						b141314434
					
				
					 3 changed files with 32 additions and 23 deletions
				
			
		| 
						 | 
					@ -8,7 +8,7 @@
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <unistd.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
 | 
					    char buffer[1028]; // buffer for orders
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,13 +37,13 @@ void WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame
 | 
				
			||||||
    mut.unlock();
 | 
					    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
 | 
					    // Spawn threads
 | 
				
			||||||
    std::vector<std::thread> clientThreads;
 | 
					    std::vector<std::thread> clientThreads;
 | 
				
			||||||
    for(auto client : myClients)
 | 
					    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));
 | 
					        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();
 | 
					    gameMutex.lock();
 | 
				
			||||||
    std::string gamestate_string = GetStringFromGame(game);
 | 
					    std::string gamestate_string = GetStringFromGame(game);
 | 
				
			||||||
| 
						 | 
					@ -90,7 +90,7 @@ void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameN
 | 
				
			||||||
    gameNameString = gameName;
 | 
					    gameNameString = gameName;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void PerformServerHandshakeWithClient(const ClientInfo &client, const CTTRTSGame &game)
 | 
					void PerformServerHandshake(const ClientInfo &client, const CTTRTSGame &game)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    char handshake[64];
 | 
					    char handshake[64];
 | 
				
			||||||
    snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.GetName().c_str());
 | 
					    snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.GetName().c_str());
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,24 +26,33 @@ struct ClientInfo
 | 
				
			||||||
    player_t player;
 | 
					    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 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);
 | 
					void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameNameString);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// For local fatal errors
 | 
				
			||||||
inline void fatal_error(const char *msg)
 | 
					inline void fatal_error(const char *msg)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    std::cerr<<msg<<std::endl;
 | 
					    std::cerr<<msg<<std::endl;
 | 
				
			||||||
    exit(1);
 | 
					    exit(1);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// For system fatal errors (ie. functions that set errno)
 | 
				
			||||||
inline void fatal_perror(const char *msg)
 | 
					inline void fatal_perror(const char *msg)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    perror(msg);
 | 
					    perror(msg);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -67,8 +67,8 @@ void RunServerForGame(CTTRTSGame &game)
 | 
				
			||||||
    while ( myClients.size() < numClients )
 | 
					    while ( myClients.size() < numClients )
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // information for each client
 | 
					        // information for each client
 | 
				
			||||||
        sockaddr_in cli_addr;   // Client address
 | 
					        sockaddr_in cli_addr;           // Client address
 | 
				
			||||||
        int clientsockfd; 	    // new socket File descriptor
 | 
					        int         clientsockfd; 	    // new socket File descriptor
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        socklen_t clilen = sizeof(sockaddr_in);
 | 
					        socklen_t clilen = sizeof(sockaddr_in);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -81,20 +81,20 @@ void RunServerForGame(CTTRTSGame &game)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<< std::endl;
 | 
					        std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<< std::endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Fill out client info struct
 | 
				
			||||||
        ClientInfo clientInfo;
 | 
					        ClientInfo clientInfo;
 | 
				
			||||||
        clientInfo.cli_addr = cli_addr;
 | 
					        clientInfo.cli_addr     = cli_addr;
 | 
				
			||||||
        clientInfo.clientsockfd = clientsockfd;
 | 
					        clientInfo.clientsockfd = clientsockfd;
 | 
				
			||||||
        clientInfo.player = *player_iterator;
 | 
					        clientInfo.player       = *player_iterator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        player_iterator++;
 | 
					        // Handshake with client
 | 
				
			||||||
 | 
					        PerformServerHandshake(clientInfo, game);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Add out client info to our list
 | 
				
			||||||
        myClients.push_back(clientInfo);
 | 
					        myClients.push_back(clientInfo);
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Perform the initial handshake with clients
 | 
					        // Iterate to next player
 | 
				
			||||||
    for( auto client : myClients )
 | 
					        player_iterator++;
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        // Handshake currently just player
 | 
					 | 
				
			||||||
        PerformServerHandshakeWithClient(client, game);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::cout<<"All clients connected"<< std::endl;
 | 
					    std::cout<<"All clients connected"<< std::endl;
 | 
				
			||||||
| 
						 | 
					@ -107,11 +107,11 @@ void RunServerForGame(CTTRTSGame &game)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // Send data to clients
 | 
					        // Send data to clients
 | 
				
			||||||
        std::cout<<"Sending clients gamedata"<< std::endl;
 | 
					        std::cout<<"Sending clients gamedata"<< std::endl;
 | 
				
			||||||
        SendGameInfoToClients(myClients, game, gameMutex);
 | 
					        SendGamestateToClients(myClients, game, gameMutex);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // 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);
 | 
					        WaitForOrdersFromClients(myClients, game, gameMutex);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::cout<<"Orders recieved, simulating turn"<< std::endl;
 | 
					        std::cout<<"Orders recieved, simulating turn"<< std::endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -122,7 +122,7 @@ void RunServerForGame(CTTRTSGame &game)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Send final state to all the clients
 | 
					    // Send final state to all the clients
 | 
				
			||||||
    SendGameInfoToClients(myClients, game, gameMutex);
 | 
					    SendGamestateToClients(myClients, game, gameMutex);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int runServer(int argc, char* argv[])
 | 
					int runServer(int argc, char* argv[])
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue