Make client and server perform a handshake to agree on player IDs

This commit is contained in:
mdiluzio 2015-01-02 19:31:14 +00:00
parent bccd043d2c
commit 770502184c
2 changed files with 67 additions and 1 deletions

View file

@ -14,9 +14,12 @@
#include <netdb.h>
#include "net.h"
#include "game.h"
int runClient(int argc, char* argv[])
{
player_t myPlayer;
int sockfd; // socket File descriptor
int portno; // Port number
int n = 0; // return value for read and write calls
@ -71,6 +74,30 @@ int runClient(int argc, char* argv[])
if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
std::cout<<"Waiting for handshake"<<std::endl;
memset(buffer,0,sizeof(buffer));
if (read(sockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR recieving handshake from server");
std::string handshake(buffer);
if ( write(sockfd,handshake.c_str(),handshake.length()) < 0)
error("ERROR sending handshake to server");
size_t pos;
if( (pos = handshake.find("player")) != std::string::npos )
{
std::string player = handshake.substr(pos, handshake.length());
myPlayer = (player_t)atoi(player.c_str());
}
else
{
error("Handshake failed");
}
std::cout<<"I am player "<< std::to_string((int)myPlayer) << std::endl;
while ( n >= 0 )
{
memset(buffer,0,sizeof(buffer));

View file

@ -26,6 +26,7 @@ struct ClientInfo
{
sockaddr_in cli_addr;
int clientsockfd;
player_t player;
};
int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game)
@ -45,7 +46,7 @@ int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &
std::cout<<buffer<<std::endl;
mut.lock();
game.IssueOrders(player_t::Red , buffer);
game.IssueOrders(info.player , buffer);
mut.unlock();
return 0;
@ -135,7 +136,10 @@ int runServer(int argc, char* argv[])
// Set up game
CTTRTSGame game = GetGameFromFile("Tiny2Player.txt");
std::vector<player_t> players = game.GetPlayers();
unsigned int numClients = game.GetPlayers().size();
auto player_iterator = players.begin();
// game mutex
std::mutex gameMutex;
@ -162,9 +166,44 @@ int runServer(int argc, char* argv[])
error("ERROR on accept");
std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<<std::endl;
ClientInfo clientInfo;
clientInfo.cli_addr = cli_addr;
clientInfo.clientsockfd = clientsockfd;
clientInfo.player = *player_iterator;
player_iterator++;
// Could verify if player is valid here
myClients.push_back({cli_addr,clientsockfd});
}
// Perform the initial handshake with clients
for( auto client : myClients )
{
// Handshake currently just player
std::string handshake = std::string("player ")+std::to_string((int)client.player);
// Output the handshake
std::cout<<"Handshaking with "<<handshake<<std::endl;
// Send handshake
if ( write( client.clientsockfd,handshake.c_str(),handshake.length() ) < 0 )
error("ERROR sending to client");
// Recieve handshake
char buffer[64];
if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR reading from client");
// Verify handshake
if ( std::string(buffer) != handshake )
error("Error in client handshake");
std::cout<<"Success on handshake with "<<handshake<<std::endl;
}
std::cout<<"All clients connected"<<std::endl;
// Loop for each turn