Skip to content

Commit

Permalink
SocketW: Error handling is now thread safe and never exit()s.
Browse files Browse the repository at this point in the history
Removed variable `SWBaseSocket::error_string` which was written on any error and read on error in `disconnect()`, all without synchronization. This is a problem in Rigs of Rods server which uses each client socket from 2 threads: receiver and broadcaster.

Function `SWBaseSocket::set_error()` was modified to only fill the supplied SWBaseError object, without touching anything in the SWBaseSocket object. Also, the function won't shut down the whole program with `exit()` anymore.

"verbose mode" was removed completely.
  • Loading branch information
ohlidalp authored and tritonas00 committed Sep 23, 2021
1 parent 0aaa288 commit b455cec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 43 deletions.
44 changes: 10 additions & 34 deletions dependencies/socketw/sw_base.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,17 @@ void WSA_exit(void)
//== Error handling mode
//====================================================================
bool sw_DoThrow = false;
bool sw_Verbose = true;

void sw_setThrowMode(bool throw_errors)
{
sw_DoThrow = throw_errors;
}

void sw_setVerboseMode(bool verbose)
{
sw_Verbose = verbose;
}

bool sw_getThrowMode(void)
{
return sw_DoThrow;
}

bool sw_getVerboseMode(void)
{
return sw_Verbose;
}


//====================================================================
//== Base error class
Expand Down Expand Up @@ -178,7 +167,6 @@ SWBaseSocket::SWBaseSocket()
recv_close = false;

//init values
error_string = "";
block_mode = blocking;
fsend_ready = true;
frecv_ready = true;
Expand Down Expand Up @@ -292,7 +280,9 @@ bool SWBaseSocket::disconnect(SWBaseError *error)
}

if( n != 0 ){
set_error(error, err, error_string);
char err_str[100];
snprintf(err_str, 100, "SWBaseSocket::disconnect() - recv() failed with code %d", n);
set_error(error, err, err_str);
return false; //error
}

Expand Down Expand Up @@ -633,12 +623,6 @@ bool SWBaseSocket::waitWrite(SWBaseError *error)
return waitIO(tmp, error);
}

void SWBaseSocket::print_error()
{
if( error_string.size() > 0 )
fprintf(stderr, "%s!\n", error_string.c_str());
}

void SWBaseSocket::handle_errno(SWBaseError *error, string msg)
{
#ifndef _WIN32
Expand Down Expand Up @@ -745,24 +729,16 @@ void SWBaseSocket::no_error(SWBaseError *error)

void SWBaseSocket::set_error(SWBaseError *error, SWBaseError name, string msg)
{
error_string = msg;

if(error != NULL){
if( sw_DoThrow ){
SWBaseError e;
e = name;
e.error_string = msg;
e.failed_class = this;
throw e;
} else if(error != NULL){
*error = name;
error->error_string = msg;
error->failed_class = this;
}else{
if( sw_Verbose )
print_error();

if( sw_DoThrow ){
SWBaseError e;
e = name;
e.error_string = msg;
e.failed_class = this;
throw e;
}else
exit(-1);
}
}

9 changes: 0 additions & 9 deletions dependencies/socketw/sw_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
//
// Default is throw_errors == false and verbose == true
void sw_setThrowMode(bool throw_errors);
void sw_setVerboseMode(bool verbose);
bool sw_getThrowMode(void);
bool sw_getVerboseMode(void);


// Abstract base class for streaming sockets
Expand Down Expand Up @@ -151,10 +149,6 @@ class DECLSPEC SWBaseSocket
// and others that use those, i.e. all frecvmsg().
void set_timeout(Uint32 sec, Uint32 usec){ tsec = sec, tusec = usec; }

// Error handling
virtual void print_error(); //prints the last error if any to stderr
virtual std::string get_error(){return error_string;} //returns a human readable error string

protected:
// get a new socket if myfd < 0
virtual void get_socket()=0;
Expand All @@ -178,9 +172,6 @@ class DECLSPEC SWBaseSocket

// our socket descriptor
int myfd;

// last error
std::string error_string;

// data for fsend
bool fsend_ready;
Expand Down

0 comments on commit b455cec

Please # to comment.