Use new fatal_error and fatal_perror functions for errors

This commit is contained in:
mdiluzio 2015-01-03 22:30:52 +00:00
parent 1785ce2fc0
commit 4055d85d99
4 changed files with 25 additions and 18 deletions

View file

@ -48,14 +48,14 @@ int runClient(int argc, char* argv[])
// 0 is for default protocol
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
fatal_perror("ERROR opening socket");
std::cout<<"Opened socket on "<<sockfd<<std::endl;
// Get the hostent information for the host by name
server = gethostbyname(argv[1]);
if (server == NULL)
error("ERROR, no such host");
fatal_error("ERROR, no such host");
std::cout<<"Connecting to "<<argv[1]<<std::endl;
@ -73,24 +73,24 @@ int runClient(int argc, char* argv[])
// Attempt to connect to the server using the socket and server address info
if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
fatal_perror("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");
fatal_perror("ERROR recieving handshake from server");
std::string handshake(buffer);
std::cout<<"Handshake:"<<handshake<<std::endl;
if ( write( sockfd, handshake.c_str(), handshake.length()+1 ) < 0 )
error("ERROR sending handshake to server");
fatal_perror("ERROR sending handshake to server");
unsigned int player;
char gameName[64];
if ( sscanf(handshake.c_str(),TTRTS_HANDSHAKE_FORMAT,&player,gameName) < 2 )
error("Handshake failed");
fatal_error("Handshake failed");
myPlayer = (player_t)player;
std::cout<<"I am player "<<std::to_string((int)myPlayer)<<std::endl;
@ -109,7 +109,7 @@ int runClient(int argc, char* argv[])
// Receive gamestate
memset(buffer,0,sizeof(buffer));
if (read(sockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR reading from client");
fatal_perror("ERROR reading from client");
gamestate+=buffer;
}
@ -125,8 +125,8 @@ int runClient(int argc, char* argv[])
std::cout<<orders<<std::endl;
// Write to the socket with the buffer
n = write(sockfd,orders.c_str(),orders.length());
if (n < 0)
error("ERROR writing to socket");
if (0 < n)
fatal_perror("ERROR writing to socket");
std::cout<<"Order Sent"<<std::endl;
}

View file

@ -24,7 +24,7 @@ int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &
// read will block until the client has called write
// up to the full size of the buffer
if (read(info.clientsockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR reading from client");
fatal_perror("ERROR reading from client");
// Append the received orders
orders+=buffer;
@ -66,6 +66,6 @@ void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame
{
// Write to the socket with the buffer
if ( write( client.clientsockfd, gamestate_string.c_str(), gamestate_string.length() ) < 0 )
error("ERROR sending to client");
fatal_perror("ERROR sending to client");
}
}

View file

@ -14,6 +14,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define TTRTS_HANDSHAKE_FORMAT "player %u name %s"
@ -31,7 +32,13 @@ void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game,
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex);
inline void error(const char *msg)
inline void fatal_error(const char *msg)
{
std::cerr<<msg<<std::endl;
exit(1);
}
inline void fatal_perror(const char *msg)
{
perror(msg);
exit(1);

View file

@ -38,7 +38,7 @@ int runServer(int argc, char* argv[])
// 0 is for default protocol
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
fatal_perror("ERROR opening socket");
// bind our socket to this server address
std::cout<<"Binding socket"<<std::endl;
@ -47,7 +47,7 @@ int runServer(int argc, char* argv[])
{
if(retry > 10)
{
error("Binding failed after retries");
fatal_error("Binding failed after retries");
}
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) >= 0)
@ -93,7 +93,7 @@ int runServer(int argc, char* argv[])
// client information will be stored in cli_addr
clientsockfd = accept(sockfd, (sockaddr *) &cli_addr, &clilen);
if (clientsockfd < 0)
error("ERROR on accept");
fatal_perror("ERROR on accept");
std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<<std::endl;
@ -118,18 +118,18 @@ int runServer(int argc, char* argv[])
// Send handshake
if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
error("ERROR sending to client");
fatal_perror("ERROR sending to client");
// Recieve handshake
char buffer[64];
if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR reading from client");
fatal_perror("ERROR reading from client");
std::cout<<"Received:"<<buffer<<std::endl;
// Verify handshake
if ( std::string(buffer) != std::string(handshake) )
error("Error in client handshake");
fatal_error("Error in client handshake");
std::cout<<"Success on handshake with "<<handshake<<std::endl;
}