2015-01-02 15:03:29 +00:00
|
|
|
#include "server.h"
|
|
|
|
|
2015-01-02 16:18:18 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2015-01-02 16:09:24 +00:00
|
|
|
#include <iostream>
|
2015-01-02 17:14:16 +00:00
|
|
|
#include <mutex>
|
2015-01-02 16:09:24 +00:00
|
|
|
|
2015-01-02 15:13:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2015-01-03 22:43:39 +00:00
|
|
|
#include <sys/socket.h>
|
2015-01-02 15:13:05 +00:00
|
|
|
|
|
|
|
#include "net.h"
|
2015-01-02 17:14:16 +00:00
|
|
|
#include "filesystem.h"
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-04 11:08:35 +00:00
|
|
|
void RunServerForGame(CTTRTSGame &game)
|
2015-01-02 16:18:18 +00:00
|
|
|
{
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"Setting up server on port "<<TTRTS_PORT<< std::endl;
|
2015-01-02 16:18:18 +00:00
|
|
|
|
|
|
|
// Server side information
|
2015-01-03 22:43:39 +00:00
|
|
|
int sockfd; // socket File descriptor
|
2015-01-02 16:18:18 +00:00
|
|
|
sockaddr_in serv_addr; // Server address
|
|
|
|
int portno = TTRTS_PORT;
|
2015-01-02 15:13:05 +00:00
|
|
|
|
|
|
|
// empty the server address
|
|
|
|
memset(&serv_addr,0, sizeof(serv_addr));
|
|
|
|
// Set the server address family to AF_INET
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
// htons swaps from host byte order to network byte order
|
|
|
|
serv_addr.sin_port = htons(portno);
|
2015-01-02 16:18:18 +00:00
|
|
|
// The host for this address is this current machine's IP, INADDR_ANY fetches this
|
2015-01-02 15:13:05 +00:00
|
|
|
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"Opening socket"<< std::endl;
|
2015-01-02 16:18:18 +00:00
|
|
|
// Create a new socket
|
|
|
|
// AF_INET is general internet socket 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)
|
2015-01-03 22:30:52 +00:00
|
|
|
fatal_perror("ERROR opening socket");
|
2015-01-02 16:18:18 +00:00
|
|
|
|
|
|
|
// bind our socket to this server address
|
2015-01-04 11:08:35 +00:00
|
|
|
TryBindSocket(sockfd, serv_addr);
|
2015-01-02 15:13:05 +00:00
|
|
|
|
|
|
|
// Listen on the socket for messages
|
|
|
|
// Second param is length of backlog queue, the maximum number of connections
|
|
|
|
// that can be waiting while the process is handling a single connection
|
|
|
|
// max is usually set to 5
|
|
|
|
listen(sockfd,5);
|
|
|
|
|
2015-01-04 11:08:35 +00:00
|
|
|
// Get information about the game
|
2015-01-02 19:31:14 +00:00
|
|
|
std::vector<player_t> players = game.GetPlayers();
|
2015-01-03 19:27:08 +00:00
|
|
|
unsigned int numClients = players.size();
|
2015-01-02 19:31:14 +00:00
|
|
|
auto player_iterator = players.begin();
|
2015-01-02 17:14:16 +00:00
|
|
|
|
|
|
|
// game mutex
|
|
|
|
std::mutex gameMutex;
|
|
|
|
|
|
|
|
// Set of clients
|
|
|
|
std::vector<ClientInfo> myClients;
|
|
|
|
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"Waiting for clients"<< std::endl;
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-02 16:18:18 +00:00
|
|
|
// Loop while we're connecting the clients
|
2015-01-02 17:14:16 +00:00
|
|
|
while ( myClients.size() < numClients )
|
2015-01-02 15:13:05 +00:00
|
|
|
{
|
2015-01-02 16:18:18 +00:00
|
|
|
// information for each client
|
|
|
|
sockaddr_in cli_addr; // Client address
|
|
|
|
int clientsockfd; // new socket File descriptor
|
2015-01-02 15:13:05 +00:00
|
|
|
|
2015-01-02 17:14:16 +00:00
|
|
|
socklen_t clilen = sizeof(sockaddr_in);
|
2015-01-02 16:18:18 +00:00
|
|
|
|
|
|
|
// accept waits for a connection from a client
|
|
|
|
// it returns a new socket file descriptor for this connection
|
|
|
|
// client information will be stored in cli_addr
|
|
|
|
clientsockfd = accept(sockfd, (sockaddr *) &cli_addr, &clilen);
|
|
|
|
if (clientsockfd < 0)
|
2015-01-03 22:30:52 +00:00
|
|
|
fatal_perror("ERROR on accept");
|
2015-01-02 16:18:18 +00:00
|
|
|
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<< std::endl;
|
2015-01-02 19:31:14 +00:00
|
|
|
|
|
|
|
ClientInfo clientInfo;
|
|
|
|
clientInfo.cli_addr = cli_addr;
|
|
|
|
clientInfo.clientsockfd = clientsockfd;
|
|
|
|
clientInfo.player = *player_iterator;
|
|
|
|
|
|
|
|
player_iterator++;
|
2015-01-03 19:27:08 +00:00
|
|
|
myClients.push_back(clientInfo);
|
2015-01-02 17:14:16 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 19:31:14 +00:00
|
|
|
// Perform the initial handshake with clients
|
|
|
|
for( auto client : myClients )
|
|
|
|
{
|
|
|
|
// Handshake currently just player
|
2015-01-04 11:08:35 +00:00
|
|
|
PerformServerHandshakeWithClient(client, game);
|
2015-01-02 19:31:14 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"All clients connected"<< std::endl;
|
2015-01-02 19:46:09 +00:00
|
|
|
|
|
|
|
std::cout<<"Hit enter to begin...";
|
|
|
|
std::cin.ignore();
|
2015-01-02 16:18:18 +00:00
|
|
|
|
2015-01-02 17:14:16 +00:00
|
|
|
// Loop for each turn
|
|
|
|
while ( !game.GameOver() )
|
|
|
|
{
|
|
|
|
// Send data to clients
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"Sending clients gamedata"<< std::endl;
|
2015-01-02 19:05:58 +00:00
|
|
|
SendGameInfoToClients(myClients, game, gameMutex);
|
2015-01-02 17:14:16 +00:00
|
|
|
|
|
|
|
// Wait for orders from clients
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"Waiting for client orders"<< std::endl;
|
2015-01-02 19:05:58 +00:00
|
|
|
GetOrdersFromClients(myClients, game, gameMutex);
|
2015-01-02 17:14:16 +00:00
|
|
|
|
2015-01-03 22:43:39 +00:00
|
|
|
std::cout<<"Orders recieved, simulating turn"<< std::endl;
|
2015-01-02 19:00:28 +00:00
|
|
|
|
2015-01-02 17:14:16 +00:00
|
|
|
// Step to the next turn
|
|
|
|
gameMutex.lock();
|
|
|
|
game.SimulateToNextTurn();
|
|
|
|
gameMutex.unlock();
|
2015-01-02 15:13:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 19:00:28 +00:00
|
|
|
// Send final state to all the clients
|
|
|
|
SendGameInfoToClients(myClients, game, gameMutex);
|
2015-01-03 22:43:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-01-02 19:00:28 +00:00
|
|
|
|
|
|
|
// Get the winning player
|
|
|
|
player_t winningPlayer = game.GetWinningPlayer();
|
2015-01-02 17:14:16 +00:00
|
|
|
|
2015-01-02 19:00:28 +00:00
|
|
|
// Print the winner!
|
|
|
|
if ( winningPlayer != player_t::NUM_INVALID )
|
|
|
|
std::cout<<"Game over! Winner:"<<(int) winningPlayer <<std::endl;
|
|
|
|
else
|
|
|
|
std::cout<<"Game over! It was a draw!"<<std::endl;
|
2015-01-02 17:14:16 +00:00
|
|
|
|
2015-01-02 15:13:05 +00:00
|
|
|
// Return
|
2015-01-03 22:43:39 +00:00
|
|
|
return (int)winningPlayer;
|
2015-01-02 15:03:29 +00:00
|
|
|
}
|