More refactoring by pulling code out of client and server files
This commit is contained in:
parent
43d688a728
commit
5c8666d4fb
4 changed files with 86 additions and 61 deletions
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
int runClient(int argc, char* argv[])
|
int runClient(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
player_t myPlayer;
|
player_t myPlayer = player_t::NUM_INVALID; // My player
|
||||||
|
|
||||||
int sockfd; // socket File descriptor
|
int sockfd; // socket File descriptor
|
||||||
int portno; // Port number
|
int portno; // Port number
|
||||||
|
@ -29,7 +29,7 @@ int runClient(int argc, char* argv[])
|
||||||
|
|
||||||
struct hostent *server; // pointer to host information
|
struct hostent *server; // pointer to host information
|
||||||
|
|
||||||
char buffer[1028]; // buffer for socked read
|
char buffer[1028]; // buffer for socket read
|
||||||
memset(buffer,0,sizeof(buffer));
|
memset(buffer,0,sizeof(buffer));
|
||||||
|
|
||||||
// must provide information
|
// must provide information
|
||||||
|
@ -77,27 +77,17 @@ int runClient(int argc, char* argv[])
|
||||||
|
|
||||||
std::cout<<"Waiting for handshake"<<std::endl;
|
std::cout<<"Waiting for handshake"<<std::endl;
|
||||||
|
|
||||||
memset(buffer,0,sizeof(buffer));
|
|
||||||
if (read(sockfd,buffer,sizeof(buffer)-1) < 0)
|
|
||||||
fatal_perror("ERROR recieving handshake from server");
|
|
||||||
|
|
||||||
std::string handshake(buffer);
|
|
||||||
std::cout<<"Handshake:"<<handshake<<std::endl;
|
|
||||||
|
|
||||||
if ( write( sockfd, handshake.c_str(), handshake.length()+1 ) < 0 )
|
|
||||||
fatal_perror("ERROR sending handshake to server");
|
|
||||||
|
|
||||||
unsigned int player;
|
unsigned int player;
|
||||||
char gameName[64];
|
std::string gameNameString;
|
||||||
if ( sscanf(handshake.c_str(),TTRTS_HANDSHAKE_FORMAT,&player,gameName) < 2 )
|
|
||||||
fatal_error("Handshake failed");
|
PerformClientHandshake(sockfd, player, gameNameString);
|
||||||
|
|
||||||
myPlayer = (player_t)player;
|
myPlayer = (player_t)player;
|
||||||
std::cout<<"I am player "<<std::to_string((int)myPlayer)<<std::endl;
|
std::cout<<"I am player "<<std::to_string((int)myPlayer)<<std::endl;
|
||||||
std::cout<<"Game is "<<gameName<<std::endl;
|
std::cout<<"Game is "<<gameNameString<<std::endl;
|
||||||
|
|
||||||
// Clean out the games dir
|
// Clean out the games dir
|
||||||
CreateAndCleanGameDir(gameName);
|
CreateAndCleanGameDir(gameNameString);
|
||||||
|
|
||||||
while ( n >= 0 )
|
while ( n >= 0 )
|
||||||
{
|
{
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game)
|
void WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game)
|
||||||
{
|
{
|
||||||
char buffer[1028]; // buffer for orders
|
char buffer[1028]; // buffer for orders
|
||||||
|
|
||||||
|
@ -35,8 +35,6 @@ int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &
|
||||||
mut.lock();
|
mut.lock();
|
||||||
game.IssueOrders(info.player , orders);
|
game.IssueOrders(info.player , orders);
|
||||||
mut.unlock();
|
mut.unlock();
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex)
|
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex)
|
||||||
|
@ -69,3 +67,70 @@ void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame
|
||||||
fatal_perror("ERROR sending to client");
|
fatal_perror("ERROR sending to client");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameNameString)
|
||||||
|
{
|
||||||
|
char handshakeBuffer[128];
|
||||||
|
memset(handshakeBuffer,0,sizeof(handshakeBuffer));
|
||||||
|
|
||||||
|
if (read(sockfd, handshakeBuffer,sizeof(handshakeBuffer)-1) < 0)
|
||||||
|
fatal_perror("ERROR recieving handshake from server");
|
||||||
|
|
||||||
|
std::string handshake(handshakeBuffer);
|
||||||
|
std::cout<<"Handshake:"<<handshake<< std::endl;
|
||||||
|
|
||||||
|
if ( write( sockfd, handshake.c_str(), handshake.length()+1 ) < 0 )
|
||||||
|
fatal_perror("ERROR sending handshake to server");
|
||||||
|
|
||||||
|
char gameName[64];
|
||||||
|
if ( sscanf(handshake.c_str(),TTRTS_HANDSHAKE_FORMAT,&player,gameName) < 2 )
|
||||||
|
fatal_error("Handshake failed");
|
||||||
|
|
||||||
|
gameNameString = gameName;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PerformServerHandshakeWithClient(const ClientInfo &client, const CTTRTSGame &game)
|
||||||
|
{
|
||||||
|
char handshake[64];
|
||||||
|
snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.GetName().c_str());
|
||||||
|
|
||||||
|
// Output the handshake
|
||||||
|
std::cout<<"Handshaking:"<<handshake<< std::endl;
|
||||||
|
|
||||||
|
// Send handshake
|
||||||
|
if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
|
||||||
|
fatal_perror("ERROR sending to client");
|
||||||
|
|
||||||
|
// Receive handshake
|
||||||
|
char buffer[64];
|
||||||
|
if ( read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0 )
|
||||||
|
fatal_perror("ERROR reading from client");
|
||||||
|
|
||||||
|
std::cout<<"Received:"<<buffer<< std::endl;
|
||||||
|
|
||||||
|
// Verify handshake
|
||||||
|
if ( std::string(buffer) != std::string(handshake) )
|
||||||
|
fatal_error("Error in client handshake");
|
||||||
|
|
||||||
|
std::cout<<"Success on handshake with "<<handshake<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TryBindSocket(int sockfd, sockaddr_in &serv_addr)
|
||||||
|
{
|
||||||
|
std::cout<<"Binding socket"<< std::endl;
|
||||||
|
int retry = 1;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if(retry > 10)
|
||||||
|
fatal_error("Binding failed");
|
||||||
|
|
||||||
|
// Attempt to bind our listening socket
|
||||||
|
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) >= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
std::cout<<"Binding failed on try "<<retry<< std::endl;
|
||||||
|
sleep(retry);
|
||||||
|
retry++;
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,12 +26,18 @@ struct ClientInfo
|
||||||
player_t player;
|
player_t player;
|
||||||
};
|
};
|
||||||
|
|
||||||
int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game);
|
void WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game);
|
||||||
|
|
||||||
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex);
|
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex);
|
||||||
|
|
||||||
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex);
|
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex);
|
||||||
|
|
||||||
|
void TryBindSocket(int sockfd, sockaddr_in &serv_addr);
|
||||||
|
|
||||||
|
void PerformServerHandshakeWithClient(const ClientInfo &client, const CTTRTSGame &game);
|
||||||
|
|
||||||
|
void PerformClientHandshake(int sockfd, unsigned int &player, std::string &gameNameString);
|
||||||
|
|
||||||
inline void fatal_error(const char *msg)
|
inline void fatal_error(const char *msg)
|
||||||
{
|
{
|
||||||
std::cerr<<msg<<std::endl;
|
std::cerr<<msg<<std::endl;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
|
|
||||||
CTTRTSGame &RunServerForGame(CTTRTSGame &game)
|
void RunServerForGame(CTTRTSGame &game)
|
||||||
{
|
{
|
||||||
std::cout<<"Setting up server on port "<<TTRTS_PORT<< std::endl;
|
std::cout<<"Setting up server on port "<<TTRTS_PORT<< std::endl;
|
||||||
|
|
||||||
|
@ -42,22 +42,7 @@ CTTRTSGame &RunServerForGame(CTTRTSGame &game)
|
||||||
fatal_perror("ERROR opening socket");
|
fatal_perror("ERROR opening socket");
|
||||||
|
|
||||||
// bind our socket to this server address
|
// bind our socket to this server address
|
||||||
std::cout<<"Binding socket"<< std::endl;
|
TryBindSocket(sockfd, serv_addr);
|
||||||
int retry = 1;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
if(retry > 10)
|
|
||||||
{
|
|
||||||
fatal_error("Binding failed after retries");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) >= 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
std::cout<<"Binding failed on try "<<retry<< std::endl;
|
|
||||||
sleep(retry);
|
|
||||||
retry++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Listen on the socket for messages
|
// Listen on the socket for messages
|
||||||
// Second param is length of backlog queue, the maximum number of connections
|
// Second param is length of backlog queue, the maximum number of connections
|
||||||
|
@ -65,6 +50,7 @@ CTTRTSGame &RunServerForGame(CTTRTSGame &game)
|
||||||
// max is usually set to 5
|
// max is usually set to 5
|
||||||
listen(sockfd,5);
|
listen(sockfd,5);
|
||||||
|
|
||||||
|
// Get information about the game
|
||||||
std::vector<player_t> players = game.GetPlayers();
|
std::vector<player_t> players = game.GetPlayers();
|
||||||
unsigned int numClients = players.size();
|
unsigned int numClients = players.size();
|
||||||
auto player_iterator = players.begin();
|
auto player_iterator = players.begin();
|
||||||
|
@ -108,28 +94,7 @@ CTTRTSGame &RunServerForGame(CTTRTSGame &game)
|
||||||
for( auto client : myClients )
|
for( auto client : myClients )
|
||||||
{
|
{
|
||||||
// Handshake currently just player
|
// Handshake currently just player
|
||||||
char handshake[64];
|
PerformServerHandshakeWithClient(client, game);
|
||||||
snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.GetName().c_str());
|
|
||||||
|
|
||||||
// Output the handshake
|
|
||||||
std::cout<<"Handshaking:"<<handshake<< std::endl;
|
|
||||||
|
|
||||||
// Send handshake
|
|
||||||
if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
|
|
||||||
fatal_perror("ERROR sending to client");
|
|
||||||
|
|
||||||
// Recieve handshake
|
|
||||||
char buffer[64];
|
|
||||||
if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0)
|
|
||||||
fatal_perror("ERROR reading from client");
|
|
||||||
|
|
||||||
std::cout<<"Received:"<<buffer<< std::endl;
|
|
||||||
|
|
||||||
// Verify handshake
|
|
||||||
if ( std::string(buffer) != std::string(handshake) )
|
|
||||||
fatal_error("Error in client handshake");
|
|
||||||
|
|
||||||
std::cout<<"Success on handshake with "<<handshake<< std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout<<"All clients connected"<< std::endl;
|
std::cout<<"All clients connected"<< std::endl;
|
||||||
|
@ -158,7 +123,6 @@ CTTRTSGame &RunServerForGame(CTTRTSGame &game)
|
||||||
|
|
||||||
// Send final state to all the clients
|
// Send final state to all the clients
|
||||||
SendGameInfoToClients(myClients, game, gameMutex);
|
SendGameInfoToClients(myClients, game, gameMutex);
|
||||||
return game;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int runServer(int argc, char* argv[])
|
int runServer(int argc, char* argv[])
|
||||||
|
|
Loading…
Add table
Reference in a new issue