cNetwork: Added error message to error callbacks.

master
Mattes D 2015-01-20 11:27:05 +01:00
parent 00253403b3
commit 64855ed340
8 changed files with 28 additions and 21 deletions

View File

@ -53,7 +53,7 @@ void cHostnameLookup::Callback(int a_ErrCode, evutil_addrinfo * a_Addr, void * a
// If an error has occurred, notify the error callback: // If an error has occurred, notify the error callback:
if (a_ErrCode != 0) if (a_ErrCode != 0)
{ {
Self->m_Callbacks->OnError(a_ErrCode); Self->m_Callbacks->OnError(a_ErrCode, evutil_socket_error_to_string(a_ErrCode));
cNetworkSingleton::Get().RemoveHostnameLookup(Self); cNetworkSingleton::Get().RemoveHostnameLookup(Self);
return; return;
} }
@ -91,7 +91,7 @@ void cHostnameLookup::Callback(int a_ErrCode, evutil_addrinfo * a_Addr, void * a
// If only unsupported families were reported, call the Error handler: // If only unsupported families were reported, call the Error handler:
if (!HasResolved) if (!HasResolved)
{ {
Self->m_Callbacks->OnError(DNS_ERR_NODATA); Self->m_Callbacks->OnError(DNS_ERR_NODATA, "The name does not resolve to any known address.");
} }
else else
{ {

View File

@ -78,7 +78,7 @@ void cIPLookup::Callback(int a_Result, char a_Type, int a_Count, int a_Ttl, void
if ((a_Result != 0) || (a_Addresses == nullptr)) if ((a_Result != 0) || (a_Addresses == nullptr))
{ {
// An error has occurred, notify the error callback: // An error has occurred, notify the error callback:
Self->m_Callbacks->OnError(a_Result); Self->m_Callbacks->OnError(a_Result, evutil_socket_error_to_string(a_Result));
} }
else else
{ {

View File

@ -34,7 +34,7 @@ public:
virtual void OnRemoteClosed(cTCPLink & a_Link) = 0; virtual void OnRemoteClosed(cTCPLink & a_Link) = 0;
/** Called when an error is detected on the connection. */ /** Called when an error is detected on the connection. */
virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) = 0; virtual void OnError(cTCPLink & a_Link, int a_ErrorCode, const AString & a_ErrorMsg) = 0;
}; };
typedef SharedPtr<cCallbacks> cCallbacksPtr; typedef SharedPtr<cCallbacks> cCallbacksPtr;
@ -127,7 +127,7 @@ public:
virtual void OnSuccess(cTCPLink & a_Link) = 0; virtual void OnSuccess(cTCPLink & a_Link) = 0;
/** Called when the Connect call fails. */ /** Called when the Connect call fails. */
virtual void OnError(int a_ErrorCode) = 0; virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
}; };
typedef SharedPtr<cConnectCallbacks> cConnectCallbacksPtr; typedef SharedPtr<cConnectCallbacks> cConnectCallbacksPtr;
@ -163,7 +163,7 @@ public:
/** Called when an error is encountered while resolving. /** Called when an error is encountered while resolving.
If an error is reported, the OnFinished() callback is not called. */ If an error is reported, the OnFinished() callback is not called. */
virtual void OnError(int a_ErrorCode) = 0; virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) = 0;
/** Called when all the addresses resolved have been reported via the OnNameResolved() callback. /** Called when all the addresses resolved have been reported via the OnNameResolved() callback.
Only called if there was no error reported. */ Only called if there was no error reported. */

View File

@ -173,7 +173,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
if (bind(MainSock, reinterpret_cast<const sockaddr *>(&name), sizeof(name)) != 0) if (bind(MainSock, reinterpret_cast<const sockaddr *>(&name), sizeof(name)) != 0)
{ {
m_ErrorCode = EVUTIL_SOCKET_ERROR(); m_ErrorCode = EVUTIL_SOCKET_ERROR();
Printf(m_ErrorMsg, "Cannot bind IPv6 socket to port %d: %s", a_Port, evutil_socket_error_to_string(m_ErrorCode)); Printf(m_ErrorMsg, "Cannot bind IPv6 socket to port %d: %d (%s)", a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode));
evutil_closesocket(MainSock); evutil_closesocket(MainSock);
return false; return false;
} }
@ -181,14 +181,14 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
if (evutil_make_socket_nonblocking(MainSock) != 0) if (evutil_make_socket_nonblocking(MainSock) != 0)
{ {
m_ErrorCode = EVUTIL_SOCKET_ERROR(); m_ErrorCode = EVUTIL_SOCKET_ERROR();
Printf(m_ErrorMsg, "Cannot make socket on port %d non-blocking: %s", a_Port, evutil_socket_error_to_string(m_ErrorCode)); Printf(m_ErrorMsg, "Cannot make socket on port %d non-blocking: %d (%s)", a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode));
evutil_closesocket(MainSock); evutil_closesocket(MainSock);
return false; return false;
} }
if (listen(MainSock, 0) != 0) if (listen(MainSock, 0) != 0)
{ {
m_ErrorCode = EVUTIL_SOCKET_ERROR(); m_ErrorCode = EVUTIL_SOCKET_ERROR();
Printf(m_ErrorMsg, "Cannot listen on port %d: %s", a_Port, evutil_socket_error_to_string(m_ErrorCode)); Printf(m_ErrorMsg, "Cannot listen on port %d: %d (%s)", a_Port, m_ErrorCode, evutil_socket_error_to_string(m_ErrorCode));
evutil_closesocket(MainSock); evutil_closesocket(MainSock);
return false; return false;
} }
@ -215,6 +215,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();
LOGD("evutil_make_socket_nonblocking() failed for secondary socket: %d, %s", err, evutil_socket_error_to_string(err)); LOGD("evutil_make_socket_nonblocking() failed for secondary socket: %d, %s", err, evutil_socket_error_to_string(err));
evutil_closesocket(SecondSock); evutil_closesocket(SecondSock);
return true; // Report as success, the primary socket is working
} }
// Bind to all IPv4 interfaces: // Bind to all IPv4 interfaces:
@ -227,7 +228,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();
LOGD("Cannot bind secondary socket to port %d: %d (%s)", a_Port, err, evutil_socket_error_to_string(err)); LOGD("Cannot bind secondary socket to port %d: %d (%s)", a_Port, err, evutil_socket_error_to_string(err));
evutil_closesocket(SecondSock); evutil_closesocket(SecondSock);
return true; return true; // Report as success, the primary socket is working
} }
if (listen(SecondSock, 0) != 0) if (listen(SecondSock, 0) != 0)
@ -235,7 +236,7 @@ bool cServerHandleImpl::Listen(UInt16 a_Port)
err = EVUTIL_SOCKET_ERROR(); err = EVUTIL_SOCKET_ERROR();
LOGD("Cannot listen on on secondary socket on port %d: %d (%s)", a_Port, err, evutil_socket_error_to_string(err)); LOGD("Cannot listen on on secondary socket on port %d: %d (%s)", a_Port, err, evutil_socket_error_to_string(err));
evutil_closesocket(SecondSock); evutil_closesocket(SecondSock);
return false; return true; // Report as success, the primary socket is working
} }
m_SecondaryConnListener = evconnlistener_new(cNetworkSingleton::Get().GetEventBase(), Callback, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 0, SecondSock); m_SecondaryConnListener = evconnlistener_new(cNetworkSingleton::Get().GetEventBase(), Callback, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, 0, SecondSock);

View File

@ -177,13 +177,19 @@ void cTCPLinkImpl::EventCallback(bufferevent * a_BufferEvent, short a_What, void
if (a_What & BEV_EVENT_ERROR) if (a_What & BEV_EVENT_ERROR)
{ {
// Choose the proper callback to call based on whether we were waiting for connection or not: // Choose the proper callback to call based on whether we were waiting for connection or not:
int err = EVUTIL_SOCKET_ERROR();
if (Self->m_ConnectCallbacks != nullptr) if (Self->m_ConnectCallbacks != nullptr)
{ {
Self->m_ConnectCallbacks->OnError(EVUTIL_SOCKET_ERROR()); if (err == 0)
{
// This could be a DNS failure
err = bufferevent_socket_get_dns_error(a_BufferEvent);
}
Self->m_ConnectCallbacks->OnError(err, evutil_socket_error_to_string(err));
} }
else else
{ {
Self->m_Callbacks->OnError(*Self, EVUTIL_SOCKET_ERROR()); Self->m_Callbacks->OnError(*Self, err, evutil_socket_error_to_string(err));
if (Self->m_Server == nullptr) if (Self->m_Server == nullptr)
{ {
cNetworkSingleton::Get().RemoveLink(Self); cNetworkSingleton::Get().RemoveLink(Self);

View File

@ -60,9 +60,9 @@ class cEchoLinkCallbacks:
LOGD("%p (%s:%d): Remote has closed the connection.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort()); LOGD("%p (%s:%d): Remote has closed the connection.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort());
} }
virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) override virtual void OnError(cTCPLink & a_Link, int a_ErrorCode, const AString & a_ErrorMsg) override
{ {
LOGD("%p (%s:%d): Error %d in the cEchoLinkCallbacks.", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), a_ErrorCode); LOGD("%p (%s:%d): Error %d in the cEchoLinkCallbacks: %s", &a_Link, a_Link.GetRemoteIP().c_str(), a_Link.GetRemotePort(), a_ErrorCode, a_ErrorMsg.c_str());
} }
}; };

View File

@ -27,9 +27,9 @@ class cHTTPConnectCallbacks:
LOGD("HTTP GET queued."); LOGD("HTTP GET queued.");
} }
virtual void OnError(int a_ErrorCode) override virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
{ {
LOGD("Error while connecting HTTP: %d", a_ErrorCode); LOGD("Error while connecting HTTP: %d (%s)", a_ErrorCode, a_ErrorMsg.c_str());
m_Event.Set(); m_Event.Set();
} }
@ -64,9 +64,9 @@ class cDumpCallbacks:
m_Event.Set(); m_Event.Set();
} }
virtual void OnError(cTCPLink & a_Link, int a_ErrorCode) override virtual void OnError(cTCPLink & a_Link, int a_ErrorCode, const AString & a_ErrorMsg) override
{ {
LOGD("Error in the cDumpCallbacks."); LOGD("Error %d (%s) in the cDumpCallbacks.", a_ErrorCode, a_ErrorMsg.c_str());
m_Event.Set(); m_Event.Set();
} }

View File

@ -22,9 +22,9 @@ class cFinishLookupCallbacks:
LOGD("%s resolves to IP %s", a_Name.c_str(), a_IP.c_str()); LOGD("%s resolves to IP %s", a_Name.c_str(), a_IP.c_str());
} }
virtual void OnError(int a_ErrorCode) override virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
{ {
LOGD("Error %d while performing lookup!", a_ErrorCode); LOGD("Error %d (%s) while performing lookup!", a_ErrorCode, a_ErrorMsg.c_str());
exit(a_ErrorCode); exit(a_ErrorCode);
} }