From 5c7fcc51eb92f12cec4de30c70e7936ab6b9a6fd Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sat, 4 Apr 2020 12:25:01 +0200 Subject: [PATCH] Improved socket io error handling --- .../src/connection/ServerConnection.cpp | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/native/serverconnection/src/connection/ServerConnection.cpp b/native/serverconnection/src/connection/ServerConnection.cpp index 39a4cc2..d8b025f 100644 --- a/native/serverconnection/src/connection/ServerConnection.cpp +++ b/native/serverconnection/src/connection/ServerConnection.cpp @@ -200,6 +200,26 @@ void ServerConnection::schedule_resend(const std::chrono::system_clock::time_poi 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) { if(!this->protocol_handler) { 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_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())) this->protocol_handler->server_type = server_type::TEAMSPEAK;