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 // 0 is for default protocol
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0)
error("ERROR opening socket"); fatal_perror("ERROR opening socket");
std::cout<<"Opened socket on "<<sockfd<<std::endl; std::cout<<"Opened socket on "<<sockfd<<std::endl;
// Get the hostent information for the host by name // Get the hostent information for the host by name
server = gethostbyname(argv[1]); server = gethostbyname(argv[1]);
if (server == NULL) if (server == NULL)
error("ERROR, no such host"); fatal_error("ERROR, no such host");
std::cout<<"Connecting to "<<argv[1]<<std::endl; 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 // Attempt to connect to the server using the socket and server address info
if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) 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; std::cout<<"Waiting for handshake"<<std::endl;
memset(buffer,0,sizeof(buffer)); memset(buffer,0,sizeof(buffer));
if (read(sockfd,buffer,sizeof(buffer)-1) < 0) 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::string handshake(buffer);
std::cout<<"Handshake:"<<handshake<<std::endl; std::cout<<"Handshake:"<<handshake<<std::endl;
if ( write( sockfd, handshake.c_str(), handshake.length()+1 ) < 0 ) 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; unsigned int player;
char gameName[64]; char gameName[64];
if ( sscanf(handshake.c_str(),TTRTS_HANDSHAKE_FORMAT,&player,gameName) < 2 ) if ( sscanf(handshake.c_str(),TTRTS_HANDSHAKE_FORMAT,&player,gameName) < 2 )
error("Handshake failed"); fatal_error("Handshake failed");
myPlayer = (player_t)player; myPlayer = (player_t)player;
std::cout<<"I am player "<<std::to_string((int)myPlayer)<<std::endl; std::cout<<"I am player "<<std::to_string((int)myPlayer)<<std::endl;
@ -109,7 +109,7 @@ int runClient(int argc, char* argv[])
// Receive gamestate // Receive gamestate
memset(buffer,0,sizeof(buffer)); memset(buffer,0,sizeof(buffer));
if (read(sockfd,buffer,sizeof(buffer)-1) < 0) if (read(sockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR reading from client"); fatal_perror("ERROR reading from client");
gamestate+=buffer; gamestate+=buffer;
} }
@ -125,8 +125,8 @@ int runClient(int argc, char* argv[])
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 = write(sockfd,orders.c_str(),orders.length());
if (n < 0) if (0 < n)
error("ERROR writing to socket"); fatal_perror("ERROR writing to socket");
std::cout<<"Order Sent"<<std::endl; 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 // read will block until the client has called write
// up to the full size of the buffer // up to the full size of the buffer
if (read(info.clientsockfd,buffer,sizeof(buffer)-1) < 0) if (read(info.clientsockfd,buffer,sizeof(buffer)-1) < 0)
error("ERROR reading from client"); fatal_perror("ERROR reading from client");
// Append the received orders // Append the received orders
orders+=buffer; orders+=buffer;
@ -66,6 +66,6 @@ void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame
{ {
// Write to the socket with the buffer // Write to the socket with the buffer
if ( write( client.clientsockfd, gamestate_string.c_str(), gamestate_string.length() ) < 0 ) 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 <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <iostream>
#define TTRTS_HANDSHAKE_FORMAT "player %u name %s" #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); 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); perror(msg);
exit(1); exit(1);

View file

@ -38,7 +38,7 @@ int runServer(int argc, char* argv[])
// 0 is for default protocol // 0 is for default protocol
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) if (sockfd < 0)
error("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;
@ -47,7 +47,7 @@ int runServer(int argc, char* argv[])
{ {
if(retry > 10) 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) 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 // client information will be stored in cli_addr
clientsockfd = accept(sockfd, (sockaddr *) &cli_addr, &clilen); clientsockfd = accept(sockfd, (sockaddr *) &cli_addr, &clilen);
if (clientsockfd < 0) 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; 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 // Send handshake
if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 ) if ( write( client.clientsockfd,handshake,sizeof(handshake) ) < 0 )
error("ERROR sending to client"); fatal_perror("ERROR sending to client");
// Recieve handshake // Recieve handshake
char buffer[64]; char buffer[64];
if (read(client.clientsockfd,buffer,sizeof(buffer)-1) < 0) 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; std::cout<<"Received:"<<buffer<<std::endl;
// Verify handshake // Verify handshake
if ( std::string(buffer) != std::string(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; std::cout<<"Success on handshake with "<<handshake<<std::endl;
} }