Major cleanup of client code, extracting various functionality

This commit is contained in:
mdiluzio 2015-01-04 13:09:37 +00:00
parent 3ed25cd37f
commit 1c97177956
5 changed files with 161 additions and 100 deletions

View file

@ -2,117 +2,69 @@
#include <iostream> #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "net.h" #include "net.h"
#include "game.h" #include "game.h"
#include "filesystem.h" #include "filesystem.h"
int runClient(int argc, char* argv[]) int runClient(int argc, char* argv[])
{ {
player_t myPlayer = player_t::NUM_INVALID; // My player
int sockfd; // socket File descriptor
int portno; // Port number
int n = 0; // return value for read and write calls
struct sockaddr_in serv_addr; // Server address
struct hostent *server; // pointer to host information
char buffer[1028]; // buffer for socket read
memset(buffer,0,sizeof(buffer));
// must provide information // must provide information
if (argc < 2) if (argc < 2)
{ fatal_error("Usage: ttrts client HOST");
fprintf(stderr,"usage %s hostname\n", argv[0]);
exit(0);
}
// Get port number std::string hostname = argv[1];
portno = TTRTS_PORT;
// Create a new socket sockaddr_in serv_addr; // Server address
// AF_INET is general internetsocket domain
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
// 0 is for default protocol
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
fatal_perror("ERROR opening socket");
// Get the hostent information for the host by name
server = gethostbyname(argv[1]);
if (server == NULL)
fatal_error("ERROR, no such host");
std::cout<<"TTRTS: Connecting to "<<argv[1]<<std::endl;
// Empty the server address struct
memset(&serv_addr,0, sizeof(serv_addr)); memset(&serv_addr,0, sizeof(serv_addr));
// Set the server to AF_INET // Set the server to AF_INET
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
// copy the server address into our server_addr struct
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
// Set our server address port to the port number provided // Set our server address port to the port number provided
serv_addr.sin_port = htons(portno); serv_addr.sin_port = htons(TTRTS_PORT);
// Attempt to connect to the server using the socket and server address info std::cout<<"TTRTS: Connecting to "<<hostname<<std::endl;
if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) int sockfd = ConnectToHostServer(hostname, serv_addr);
fatal_perror("ERROR connecting");
unsigned int player; unsigned int player;
std::string gameNameString; std::string gameNameString;
// Handshake with server to fetch player and gamestring
PerformClientHandshake(sockfd, player, gameNameString); PerformClientHandshake(sockfd, player, gameNameString);
myPlayer = (player_t)player; // output our information
player_t myPlayer = (player_t)player;
std::cout<<"TTRTS: I am player "<<std::to_string((int)myPlayer)<<std::endl; std::cout<<"TTRTS: I am player "<<std::to_string((int)myPlayer)<<std::endl;
std::cout<<"TTRTS: Game is "<<gameNameString<<std::endl; std::cout<<"TTRTS: Game is "<<gameNameString<<std::endl;
// Clean out the games dir // Clean out the games dir
CreateAndCleanGameDir(gameNameString); CreateAndCleanGameDir(gameNameString);
// Buffer for messages
char buffer[1028];
memset(buffer,0,sizeof(buffer));
int n = 0; // return value for read and write calls
while ( n >= 0 ) while ( n >= 0 )
{ {
std::cout<<"TTRTS: Waiting for gamestate"<<std::endl; std::cout<<"TTRTS: Waiting for gamestate"<<std::endl;
std::string gamestate = WaitForGamestateMessage(sockfd);
std::string gamestate;
while( gamestate.find("END") == std::string::npos )
{
// Receive gamestate
memset(buffer,0,sizeof(buffer));
if (read(sockfd,buffer,sizeof(buffer)-1) < 0)
fatal_perror("ERROR reading from client");
gamestate+=buffer;
}
// Output the gamestate file for this game // Output the gamestate file for this game
CTTRTSGame thisGame = GetGameFromString(gamestate); CTTRTSGame thisGame = GetGameFromString(gamestate);
OutputGameStateFile(thisGame); OutputGameStateFile(thisGame);
// Get the order file for this turn // If game over, exit with out winning player and message
if(thisGame.GameOver())
exit( OutputGameEnd(thisGame) );
// Get the order file for this turn`
std::string orders = GetOrdersFromPlayerFile(thisGame,myPlayer); std::string orders = GetOrdersFromPlayerFile(thisGame,myPlayer);
std::cout<<"TTRTS: Sending orders"<<std::endl; std::cout<<"TTRTS: Sending orders"<<std::endl;
std::cout<<orders<<std::endl; std::cout<<orders<<std::endl;
// Write to the socket with the buffer // Write to the socket with the buffer
n = write(sockfd,orders.c_str(),orders.length()); n = SendOrdersToServer(sockfd, orders);
if (0 < n)
fatal_perror("ERROR writing to socket");
} }
return 0; return 0;

View file

@ -208,6 +208,44 @@ int CreateAndCleanGameDir(const std::string& gameName)
return 0; return 0;
} }
int OutputGameEnd(const CTTRTSGame &game) {
std::cout<<"TTRTS: Game Over!"<< std::endl;
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
{
std::cout<<"TTRTS: Winner:"<<(int) winningPlayer <<std::endl;
}
else
{
std::cout<<"TTRTS: It was a draw!"<<std::endl;
}
return (int)winningPlayer;
}
int OutputgameEnd(const CTTRTSGame &game) {
std::cout<<"TTRTS: Game Over!"<< std::endl;
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
{
std::cout<<"TTRTS: Winner:"<<(int) winningPlayer <<std::endl;
}
else
{
std::cout<<"TTRTS: It was a draw!"<<std::endl;
}
return (int)winningPlayer;
}
// ===================================================================================================================== // =====================================================================================================================
int runFromFilesystem(int argc, char* argv[]) int runFromFilesystem(int argc, char* argv[])
{ {
@ -256,20 +294,5 @@ int runFromFilesystem(int argc, char* argv[])
// Output final gamestate // Output final gamestate
OutputGameStateFile(game); OutputGameStateFile(game);
std::cout<<"TTRTS: Game Over!"<<std::endl; return OutputGameEnd( game );
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
{
std::cout<<"TTRTS: Winner:"<<(int) winningPlayer <<std::endl;
}
else
{
std::cout<<"TTRTS: It was a draw!"<<std::endl;
}
return (int)winningPlayer;
} }

View file

@ -1,5 +1,7 @@
#include "net.h" #include "net.h"
#include <netdb.h>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
@ -187,3 +189,72 @@ sockaddr_in GetLocalServerAddress()
serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_addr.s_addr = INADDR_ANY;
return serv_addr; return serv_addr;
} }
int ConnectToHostServer(const std::string &hostname, sockaddr_in &serv_addr)
{
// Create a new socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
fatal_perror("ERROR opening socket");
// Get the hostent information for the host by name
hostent *server = gethostbyname(hostname.c_str());
if (server == NULL)
fatal_error("ERROR, no such host");
// copy the server address into our server_addr struct
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
// Attempt to connect to the server using the socket and server address info
if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
fatal_perror("ERROR connecting");
return sockfd;
}
std::string WaitForGamestateMessage(int sockfd)
{
std::string gamestate;
char gamestateBuffer[1028];
while( gamestate.find("END") == std::string::npos )
{
memset(gamestateBuffer,0,sizeof(gamestateBuffer));
// Receive gamestate
if (read(sockfd,gamestateBuffer,sizeof(gamestateBuffer)-1) < 0)
fatal_perror("ERROR reading from client");
gamestate+=gamestateBuffer;
}
return gamestate;
}
int SendOrdersToServer(int sockfd, const std::string &orders)
{
int n = write(sockfd,orders.c_str(),orders.length());
if (0 < n)
fatal_perror("ERROR writing to socket");
return n;
}
int OutputGameEnd( CTTRTSGame& game )
{
std::cout<<"TTRTS: Game Over!"<<std::endl;
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
{
std::cout<<"TTRTS: Winner:"<<(int) winningPlayer <<std::endl;
}
else
{
std::cout<<"TTRTS: It was a draw!"<<std::endl;
}
return (int)winningPlayer;
}

View file

@ -18,6 +18,9 @@
#define TTRTS_HANDSHAKE_FORMAT "player %u name %s" #define TTRTS_HANDSHAKE_FORMAT "player %u name %s"
//======================================================================================================================
// Structs for net management
// Struct for net client info // Struct for net client info
struct ClientInfo struct ClientInfo
{ {
@ -26,6 +29,10 @@ struct ClientInfo
player_t player; player_t player;
}; };
//======================================================================================================================
// Server side function
// Get the address of a local server // Get the address of a local server
sockaddr_in GetLocalServerAddress(); sockaddr_in GetLocalServerAddress();
@ -52,9 +59,24 @@ void TryBindSocket(int sockfd, const sockaddr_in &serv_addr);
// Perform the server side handshake with a client // Perform the server side handshake with a client
void PerformServerHandshake(const ClientInfo &client, const std::string &game); void PerformServerHandshake(const ClientInfo &client, const std::string &game);
//======================================================================================================================
// Client side functions
// Connect to the host, returns new socket for communication
int ConnectToHostServer(const std::string &hostname, sockaddr_in &serv_addr);
// Perform the client side handshake with the server // 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);
// Wait for gamestate message from host
std::string WaitForGamestateMessage(int sockfd);
// Send orders to the server
int SendOrdersToServer(int sockfd, const std::string &orders);
//======================================================================================================================
// Error functions
// For local fatal errors // For local fatal errors
inline void fatal_error(const char *msg) inline void fatal_error(const char *msg)
{ {
@ -69,4 +91,8 @@ inline void fatal_perror(const char *msg)
exit(1); exit(1);
} }
//======================================================================================================================
// Other functions
int OutputGameEnd( CTTRTSGame& game );
#endif #endif

View file

@ -89,17 +89,6 @@ int runServer(int argc, char* argv[])
RunServerForGame(game); RunServerForGame(game);
std::cout<<"TTRTS: Game over!"<<std::endl; // Return winning player and output game end
return OutputGameEnd(game);
// Get the winning player
player_t winningPlayer = game.GetWinningPlayer();
// Print the winner!
if ( winningPlayer != player_t::NUM_INVALID )
std::cout<<"TTRTS: Winner is player "<<(int) winningPlayer <<std::endl;
else
std::cout<<"TTRTS: It was a draw!"<<std::endl;
// Return
return (int)winningPlayer;
} }