Add game name to handshake and formalise handshake format

This commit is contained in:
mdiluzio 2015-01-03 20:04:31 +00:00
parent 61c012370d
commit 1785ce2fc0
5 changed files with 25 additions and 20 deletions

View file

@ -82,22 +82,22 @@ int runClient(int argc, char* argv[])
error("ERROR recieving handshake from server"); error("ERROR recieving handshake from server");
std::string handshake(buffer); std::string handshake(buffer);
std::cout<<"Handshake:"<<handshake<<std::endl;
if ( write(sockfd,handshake.c_str(),handshake.length()) < 0) if ( write( sockfd, handshake.c_str(), handshake.length()+1 ) < 0 )
error("ERROR sending handshake to server"); error("ERROR sending handshake to server");
size_t pos; unsigned int player;
if( (pos = handshake.find("player")) != std::string::npos ) char gameName[64];
{ if ( sscanf(handshake.c_str(),TTRTS_HANDSHAKE_FORMAT,&player,gameName) < 2 )
std::string player = handshake.substr(pos, handshake.length());
myPlayer = (player_t)atoi(player.c_str());
}
else
{
error("Handshake failed"); error("Handshake failed");
}
std::cout<<"I am player "<< std::to_string((int)myPlayer) << std::endl; myPlayer = (player_t)player;
std::cout<<"I am player "<<std::to_string((int)myPlayer)<<std::endl;
std::cout<<"Game is "<<gameName<<std::endl;
// Clean out the games dir
CreateAndCleanGameDir(gameName);
while ( n >= 0 ) while ( n >= 0 )
{ {

View file

@ -179,8 +179,9 @@ std::string GetOrdersFromPlayerFile(const CTTRTSGame &game, player_t &player)
return orders; return orders;
} }
int CreateAndCleanGameDir(const std::string& gameDir) int CreateAndCleanGameDir(const std::string& gameName)
{ {
std::string gameDir = getGamesDir()+gameName;
struct stat info; struct stat info;
int ret = stat( gameDir.c_str(), &info ); int ret = stat( gameDir.c_str(), &info );
if( ret == 0 && info.st_mode & S_IFDIR ) if( ret == 0 && info.st_mode & S_IFDIR )
@ -236,11 +237,8 @@ int runFromFilesystem(int argc, char* argv[])
// Default for games // Default for games
std::string ttrts_games_dir = getGamesDir(); std::string ttrts_games_dir = getGamesDir();
// Current game directory
std::string gameDir = ttrts_games_dir + game.GetName();
// Empty the current game directory // Empty the current game directory
if ( CreateAndCleanGameDir(gameDir) < 0) if ( CreateAndCleanGameDir(game.GetName()) < 0)
return -1; return -1;
// While the game isn't finished // While the game isn't finished

View file

@ -24,4 +24,6 @@ std::string getGamesDir();
int runFromFilesystem(int argc, char* argv[]); int runFromFilesystem(int argc, char* argv[]);
int CreateAndCleanGameDir(const std::string& gameName);
#endif #endif

View file

@ -15,6 +15,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define TTRTS_HANDSHAKE_FORMAT "player %u name %s"
// Struct for net client info // Struct for net client info
struct ClientInfo struct ClientInfo
{ {

View file

@ -110,13 +110,14 @@ int runServer(int argc, char* argv[])
for( auto client : myClients ) for( auto client : myClients )
{ {
// Handshake currently just player // Handshake currently just player
std::string handshake = std::string("player ")+std::to_string((int)client.player); char handshake[64];
snprintf(handshake, sizeof(handshake), TTRTS_HANDSHAKE_FORMAT,(unsigned int)client.player,game.GetName().c_str());
// Output the handshake // Output the handshake
std::cout<<"Handshaking with "<<handshake<<std::endl; std::cout<<"Handshaking:"<<handshake<<std::endl;
// Send handshake // Send handshake
if ( write( client.clientsockfd,handshake.c_str(),handshake.length() ) < 0 ) if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
error("ERROR sending to client"); error("ERROR sending to client");
// Recieve handshake // Recieve handshake
@ -124,8 +125,10 @@ int runServer(int argc, char* argv[])
if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0) if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR reading from client"); error("ERROR reading from client");
std::cout<<"Received:"<<buffer<<std::endl;
// Verify handshake // Verify handshake
if ( std::string(buffer) != handshake ) if ( std::string(buffer) != std::string(handshake) )
error("Error in client handshake"); error("Error in client handshake");
std::cout<<"Success on handshake with "<<handshake<<std::endl; std::cout<<"Success on handshake with "<<handshake<<std::endl;