added generic error handler

This commit is contained in:
Robin Müller 2021-03-08 23:14:10 +01:00
parent 4e5e6e145e
commit 8be4f45969
4 changed files with 47 additions and 19 deletions

View File

@ -9,25 +9,39 @@ TcWinTcpServer::TcWinTcpServer(object_id_t objectId, object_id_t tmtcUnixUdpBrid
if(serverTcpSocket == 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TcWinTcpServer::TcWinTcpServer: Socket creation failed!" << std::endl;
handleSocketError();
handleError(ErrorSources::SOCKET_CALL);
#endif
}
setsockopt(serverTcpSocket, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST,
&tcpSockOpt, sizeof(tcpSockOpt));
int retval = setsockopt(serverTcpSocket, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST,
reinterpret_cast<const char*>(&tcpSockOpt), sizeof(tcpSockOpt));
if(retval != 0) {
}
}
TcWinTcpServer::~TcWinTcpServer() {
}
void TcWinTcpServer::handleSocketError() {
void TcWinTcpServer::handleError(ErrorSources errorSrc) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
int errCode = WSAGetLastError();
std::string errorSrcString;
if(errorSrc == ErrorSources::SETSOCKOPT_CALL) {
errorSrcString = "setsockopt call";
}
else if(errorSrc == ErrorSources::SOCKET_CALL) {
errorSrcString = "socket call";
}
switch(errCode) {
case(WSANOTINITIALISED): {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: WSAStartup"
" call necessary" << std::endl;
#endif
sif::warning << "TmTcWinUdpBridge::handleError: " << errorSrcString << " | "
"WSANOTINITIALISED: WSAStartup call necessary" << std::endl;
break;
}
case(WSAEINVAL): {
sif::warning << "TmTcWinUdpBridge::handleError: " << errorSrcString << " | "
"WSAEINVAL: Invalid parameters" << std::endl;
break;
}
default: {
@ -35,12 +49,9 @@ void TcWinTcpServer::handleSocketError() {
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::handleSocketError: Error code: " << errCode << std::endl;
#endif
break;
}
}
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
}

View File

@ -24,7 +24,12 @@ private:
std::vector<uint8_t> receptionBuffer;
int tcpSockOpt = 0;
void handleSocketError();
enum class ErrorSources {
SOCKET_CALL,
SETSOCKOPT_CALL
};
void handleError(ErrorSources errorSrc);
};
#endif /* FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_ */

View File

@ -44,8 +44,8 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(serverSocket == INVALID_SOCKET) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open"
" UDP socket!" << std::endl;
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open UDP socket!" <<
std::endl;
#endif
handleSocketError();
return;
@ -57,16 +57,23 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(setServerPort);
serverAddressLen = sizeof(serverAddress);
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,
int result = setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*>(&serverSocketOptions),
sizeof(serverSocketOptions));
if(result != 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not set socket options!" <<
std::endl;
#endif
handleSocketError();
}
clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddress.sin_port = htons(setClientPort);
clientAddressLen = sizeof(clientAddress);
int result = bind(serverSocket,
result = bind(serverSocket,
reinterpret_cast<struct sockaddr*>(&serverAddress),
serverAddressLen);
if(result != 0) {
@ -159,14 +166,14 @@ void TmTcWinUdpBridge::handleBindError() {
case(WSANOTINITIALISED): {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "TmTcWinUdpBridge::handleBindError: WSANOTINITIALISED: "
<< "WSAStartup(...) call " << "necessary" << std::endl;
<< "WSAStartup call necessary" << std::endl;
#endif
break;
}
case(WSAEADDRINUSE): {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::handleBindError: WSAEADDRINUSE: "
<< "Port is already in use!" << std::endl;
"Port is already in use!" << std::endl;
#endif
break;
}

View File

@ -41,6 +41,11 @@ private:
//! by another task.
MutexIF* mutex;
enum class ErrorSources {
SOCKET_CALL,
SETSOCKOPT_CALL
};
void handleSocketError();
void handleBindError();
void handleSendError();