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

View File

@ -24,7 +24,12 @@ private:
std::vector<uint8_t> receptionBuffer; std::vector<uint8_t> receptionBuffer;
int tcpSockOpt = 0; int tcpSockOpt = 0;
void handleSocketError(); enum class ErrorSources {
SOCKET_CALL,
SETSOCKOPT_CALL
};
void handleError(ErrorSources errorSrc);
}; };
#endif /* FSFW_OSAL_WINDOWS_TCWINTCPSERVER_H_ */ #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); serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(serverSocket == INVALID_SOCKET) { if(serverSocket == INVALID_SOCKET) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open" sif::warning << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open UDP socket!" <<
" UDP socket!" << std::endl; std::endl;
#endif #endif
handleSocketError(); handleSocketError();
return; return;
@ -57,16 +57,23 @@ TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(setServerPort); serverAddress.sin_port = htons(setServerPort);
serverAddressLen = sizeof(serverAddress); serverAddressLen = sizeof(serverAddress);
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, int result = setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*>(&serverSocketOptions), reinterpret_cast<const char*>(&serverSocketOptions),
sizeof(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_family = AF_INET;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY); clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddress.sin_port = htons(setClientPort); clientAddress.sin_port = htons(setClientPort);
clientAddressLen = sizeof(clientAddress); clientAddressLen = sizeof(clientAddress);
int result = bind(serverSocket, result = bind(serverSocket,
reinterpret_cast<struct sockaddr*>(&serverAddress), reinterpret_cast<struct sockaddr*>(&serverAddress),
serverAddressLen); serverAddressLen);
if(result != 0) { if(result != 0) {
@ -159,14 +166,14 @@ void TmTcWinUdpBridge::handleBindError() {
case(WSANOTINITIALISED): { case(WSANOTINITIALISED): {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "TmTcWinUdpBridge::handleBindError: WSANOTINITIALISED: " sif::info << "TmTcWinUdpBridge::handleBindError: WSANOTINITIALISED: "
<< "WSAStartup(...) call " << "necessary" << std::endl; << "WSAStartup call necessary" << std::endl;
#endif #endif
break; break;
} }
case(WSAEADDRINUSE): { case(WSAEADDRINUSE): {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "TmTcWinUdpBridge::handleBindError: WSAEADDRINUSE: " sif::warning << "TmTcWinUdpBridge::handleBindError: WSAEADDRINUSE: "
<< "Port is already in use!" << std::endl; "Port is already in use!" << std::endl;
#endif #endif
break; break;
} }

View File

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