Refactor running game server into own function
This commit is contained in:
parent
4055d85d99
commit
43d688a728
1 changed files with 28 additions and 23 deletions
|
@ -9,16 +9,17 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "filesystem.h"
|
#include "filesystem.h"
|
||||||
|
|
||||||
int runServer(int argc, char* argv[])
|
CTTRTSGame &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;
|
||||||
|
|
||||||
// Server side information
|
// Server side information
|
||||||
int sockfd; // socket File descriptor
|
int sockfd; // socket File descriptor
|
||||||
sockaddr_in serv_addr; // Server address
|
sockaddr_in serv_addr; // Server address
|
||||||
int portno = TTRTS_PORT;
|
int portno = TTRTS_PORT;
|
||||||
|
|
||||||
|
@ -31,7 +32,7 @@ int runServer(int argc, char* argv[])
|
||||||
// The host for this address is this current machine's IP, INADDR_ANY fetches this
|
// The host for this address is this current machine's IP, INADDR_ANY fetches this
|
||||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
std::cout<<"Opening socket"<<std::endl;
|
std::cout<<"Opening socket"<< std::endl;
|
||||||
// Create a new socket
|
// Create a new socket
|
||||||
// AF_INET is general internet socket domain
|
// AF_INET is general internet socket domain
|
||||||
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
|
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
|
||||||
|
@ -41,7 +42,7 @@ int runServer(int argc, char* argv[])
|
||||||
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;
|
std::cout<<"Binding socket"<< std::endl;
|
||||||
int retry = 1;
|
int retry = 1;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
@ -53,7 +54,7 @@ int runServer(int argc, char* argv[])
|
||||||
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) >= 0)
|
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) >= 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
std::cout<<"Binding failed on try "<<retry<<std::endl;
|
std::cout<<"Binding failed on try "<<retry<< std::endl;
|
||||||
sleep(retry);
|
sleep(retry);
|
||||||
retry++;
|
retry++;
|
||||||
}
|
}
|
||||||
|
@ -64,9 +65,6 @@ int runServer(int argc, char* argv[])
|
||||||
// max is usually set to 5
|
// max is usually set to 5
|
||||||
listen(sockfd,5);
|
listen(sockfd,5);
|
||||||
|
|
||||||
// Set up game
|
|
||||||
CTTRTSGame game = GetGameFromFile("Tiny2Player.txt");
|
|
||||||
|
|
||||||
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();
|
||||||
|
@ -77,7 +75,7 @@ int runServer(int argc, char* argv[])
|
||||||
// Set of clients
|
// Set of clients
|
||||||
std::vector<ClientInfo> myClients;
|
std::vector<ClientInfo> myClients;
|
||||||
|
|
||||||
std::cout<<"Waiting for clients"<<std::endl;
|
std::cout<<"Waiting for clients"<< std::endl;
|
||||||
|
|
||||||
// Loop while we're connecting the clients
|
// Loop while we're connecting the clients
|
||||||
while ( myClients.size() < numClients )
|
while ( myClients.size() < numClients )
|
||||||
|
@ -95,7 +93,7 @@ int runServer(int argc, char* argv[])
|
||||||
if (clientsockfd < 0)
|
if (clientsockfd < 0)
|
||||||
fatal_perror("ERROR on accept");
|
fatal_perror("ERROR on accept");
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
ClientInfo clientInfo;
|
ClientInfo clientInfo;
|
||||||
clientInfo.cli_addr = cli_addr;
|
clientInfo.cli_addr = cli_addr;
|
||||||
|
@ -114,7 +112,7 @@ int runServer(int argc, char* argv[])
|
||||||
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());
|
||||||
|
|
||||||
// Output the handshake
|
// Output the handshake
|
||||||
std::cout<<"Handshaking:"<<handshake<<std::endl;
|
std::cout<<"Handshaking:"<<handshake<< std::endl;
|
||||||
|
|
||||||
// Send handshake
|
// Send handshake
|
||||||
if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
|
if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
|
||||||
|
@ -125,16 +123,16 @@ int runServer(int argc, char* argv[])
|
||||||
if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0)
|
if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0)
|
||||||
fatal_perror("ERROR reading from client");
|
fatal_perror("ERROR reading from client");
|
||||||
|
|
||||||
std::cout<<"Received:"<<buffer<<std::endl;
|
std::cout<<"Received:"<<buffer<< std::endl;
|
||||||
|
|
||||||
// Verify handshake
|
// Verify handshake
|
||||||
if ( std::string(buffer) != std::string(handshake) )
|
if ( std::string(buffer) != std::string(handshake) )
|
||||||
fatal_error("Error in client handshake");
|
fatal_error("Error in client handshake");
|
||||||
|
|
||||||
std::cout<<"Success on handshake with "<<handshake<<std::endl;
|
std::cout<<"Success on handshake with "<<handshake<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout<<"All clients connected"<<std::endl;
|
std::cout<<"All clients connected"<< std::endl;
|
||||||
|
|
||||||
std::cout<<"Hit enter to begin...";
|
std::cout<<"Hit enter to begin...";
|
||||||
std::cin.ignore();
|
std::cin.ignore();
|
||||||
|
@ -143,14 +141,14 @@ int runServer(int argc, char* argv[])
|
||||||
while ( !game.GameOver() )
|
while ( !game.GameOver() )
|
||||||
{
|
{
|
||||||
// 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);
|
SendGameInfoToClients(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);
|
GetOrdersFromClients(myClients, game, gameMutex);
|
||||||
|
|
||||||
std::cout<<"Orders recieved, simulating turn"<<std::endl;
|
std::cout<<"Orders recieved, simulating turn"<< std::endl;
|
||||||
|
|
||||||
// Step to the next turn
|
// Step to the next turn
|
||||||
gameMutex.lock();
|
gameMutex.lock();
|
||||||
|
@ -160,20 +158,27 @@ int runServer(int argc, char* argv[])
|
||||||
|
|
||||||
// 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[])
|
||||||
|
{
|
||||||
|
// Set up game
|
||||||
|
CTTRTSGame game = GetGameFromFile(argv[1]);
|
||||||
|
if(game.GetNumUnits() == 0)
|
||||||
|
fatal_error("game not valid");
|
||||||
|
|
||||||
|
RunServerForGame(game);
|
||||||
|
|
||||||
// Get the winning player
|
// Get the winning player
|
||||||
player_t winningPlayer = game.GetWinningPlayer();
|
player_t winningPlayer = game.GetWinningPlayer();
|
||||||
|
|
||||||
// Print the winner!
|
// Print the winner!
|
||||||
if ( winningPlayer != player_t::NUM_INVALID )
|
if ( winningPlayer != player_t::NUM_INVALID )
|
||||||
{
|
|
||||||
std::cout<<"Game over! Winner:"<<(int) winningPlayer <<std::endl;
|
std::cout<<"Game over! Winner:"<<(int) winningPlayer <<std::endl;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
std::cout<<"Game over! It was a draw!"<<std::endl;
|
std::cout<<"Game over! It was a draw!"<<std::endl;
|
||||||
}
|
|
||||||
|
|
||||||
// Return
|
// Return
|
||||||
return 0;
|
return (int)winningPlayer;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue