Improved socket io error handling
This commit is contained in:
		
							parent
							
								
									94c0208d45
								
							
						
					
					
						commit
						5c7fcc51eb
					
				| @ -200,6 +200,26 @@ void ServerConnection::schedule_resend(const std::chrono::system_clock::time_poi | |||||||
| 		this->event_condition.notify_one(); | 		this->event_condition.notify_one(); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | inline std::string wsa_error_str(int code) { | ||||||
|  |     int err; | ||||||
|  |     char msgbuf[256];   // for a message up to 255 bytes.
 | ||||||
|  |     msgbuf[0] = '\0';    // Microsoft doesn't guarantee this on man page.
 | ||||||
|  | 
 | ||||||
|  |     err = WSAGetLastError(); | ||||||
|  |     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,   // flags
 | ||||||
|  |                   NULL,                // lpsource
 | ||||||
|  |                   err,                 // message id
 | ||||||
|  |                   MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),    // languageid
 | ||||||
|  |                   msgbuf,              // output buffer
 | ||||||
|  |                   sizeof(msgbuf),     // size of msgbuf, bytes
 | ||||||
|  |                   NULL);               // va_list of arguments
 | ||||||
|  | 
 | ||||||
|  |     if (!*msgbuf) | ||||||
|  |         sprintf(msgbuf, "%d", err);  // provide error # if no string available
 | ||||||
|  |     return std::string{msgbuf}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| NAN_METHOD(ServerConnection::connect) { | NAN_METHOD(ServerConnection::connect) { | ||||||
| 	if(!this->protocol_handler) { | 	if(!this->protocol_handler) { | ||||||
| 		Nan::ThrowError("ServerConnection not initialized"); | 		Nan::ThrowError("ServerConnection not initialized"); | ||||||
| @ -304,7 +324,15 @@ NAN_METHOD(ServerConnection::connect) { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	this->socket->on_data = [&](const pipes::buffer_view& buffer) { this->protocol_handler->progress_packet(buffer); }; | 	this->socket->on_data = [&](const pipes::buffer_view& buffer) { this->protocol_handler->progress_packet(buffer); }; | ||||||
|     this->socket->on_fatal_error = [&](int, int) { this->close_connection(); }; |     this->socket->on_fatal_error = [&](int code, int detail) { | ||||||
|  | #if WIN32 | ||||||
|  |         auto message = wsa_error_str(detail); | ||||||
|  | #else | ||||||
|  |         auto message = strerror(detail); | ||||||
|  | #endif | ||||||
|  |         this->execute_callback_disconnect.call((code == 1 ? tr("Failed to received data: ") : tr("Failed to send data: ")) + message, false); | ||||||
|  |         this->close_connection(); | ||||||
|  |     }; | ||||||
| 
 | 
 | ||||||
| 	if(teamspeak->IsBoolean() && teamspeak->BooleanValue(info.GetIsolate())) | 	if(teamspeak->IsBoolean() && teamspeak->BooleanValue(info.GetIsolate())) | ||||||
| 		this->protocol_handler->server_type = server_type::TEAMSPEAK; | 		this->protocol_handler->server_type = server_type::TEAMSPEAK; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user