Skip to content

Commit

Permalink
🐛 Fixed error handling when running multithreaded
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherFoxGuy committed Mar 17, 2022
1 parent 789c95d commit fab14d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 44 deletions.
2 changes: 1 addition & 1 deletion conanfile.txt
@@ -1,5 +1,5 @@
[requires]
openssl/1.1.1g
openssl/1.1.1m

[generators]
cmake_find_package
44 changes: 10 additions & 34 deletions src/sw_base.cxx
Expand Up @@ -94,28 +94,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 @@ -176,7 +165,6 @@ SWBaseSocket::SWBaseSocket()
recv_close = false;

//init values
error_string = "";
block_mode = blocking;
fsend_ready = true;
frecv_ready = true;
Expand Down Expand Up @@ -290,7 +278,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 @@ -631,12 +621,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 @@ -743,24 +727,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 src/sw_base.h
Expand Up @@ -31,9 +31,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 @@ -152,10 +150,6 @@ class 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 @@ -179,9 +173,6 @@ class SWBaseSocket

// our socket descriptor
int myfd;

// last error
std::string error_string;

// data for fsend
bool fsend_ready;
Expand Down

0 comments on commit fab14d4

Please sign in to comment.