diff --git a/source/client/client.cpp b/source/client/client.cpp index f052ad2..d5de77f 100644 --- a/source/client/client.cpp +++ b/source/client/client.cpp @@ -2,117 +2,69 @@ #include -#include -#include -#include -#include - -#include -#include -#include - -#include - #include "net.h" + #include "game.h" #include "filesystem.h" 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 if (argc < 2) - { - fprintf(stderr,"usage %s hostname\n", argv[0]); - exit(0); - } + fatal_error("Usage: ttrts client HOST"); - // Get port number - portno = TTRTS_PORT; + std::string hostname = argv[1]; - // Create a new socket - // 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 "<h_addr, server->h_length); - // 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 - if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) - fatal_perror("ERROR connecting"); + std::cout<<"TTRTS: Connecting to "<= 0 ) { std::cout<<"TTRTS: Waiting for gamestate"< + #include #include @@ -186,4 +188,73 @@ sockaddr_in GetLocalServerAddress() // The host for this address is this current machine's IP, INADDR_ANY fetches this serv_addr.sin_addr.s_addr = INADDR_ANY; return serv_addr; -} \ No newline at end of file +} + + +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!"<