Skip to content

Commit

Permalink
Refactor error handling in socket interface functions
Browse files Browse the repository at this point in the history
- Added a new method to handle errors in TCP and UDP data reception.
- Updated error handling logic in receiveDataFromTCP and receiveDataFromUDP functions.
  • Loading branch information
franz-hoepfinger-4diac committed Feb 24, 2024
1 parent a187cfe commit 9add98f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
59 changes: 23 additions & 36 deletions src/arch/bsdsocketinterf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,40 +137,47 @@ int CBSDSocketInterface::sendDataOnTCP(TSocketDescriptor paSockD, const char* pa
return nRetVal;
}

int CBSDSocketInterface::receiveDataFromTCP(TSocketDescriptor paSockD, char* paData,
unsigned int paBufSize){
int nRetVal;
do{
nRetVal = static_cast<int>(recv(paSockD, paData, paBufSize, 0));
} while((-1 == nRetVal) && (EINTR == errno)); // recv got interrupt / recieving again

if(nRetVal == -1){
DEVLOG_ERROR("CBSDSocketInterface: TCP-Socket recv() failed: %s\n", strerror(errno));
}

int CBSDSocketInterface::handleError(int nRetVal, int err, const char* msg) {
// recv only sets errno if res is <= 0
if(nRetVal <= 0) {
switch(errno){
case EWOULDBLOCK:
case ENOENT: //caused by vfs
//TODO: this is a "connected = true" case, for the moment we leave it open.
//connected = true;
break;
case ENOTCONN:
case EPIPE:
case ECONNRESET:
case ECONNREFUSED:
case ECONNABORTED:
DEVLOG_ERROR("CBSDSocketInterface::receiveDataFromTCP recv() Disconnected: nRetVal: %d, ERR: %d %s\n", nRetVal, errno, strerror(errno));
//connected = false;
DEVLOG_ERROR("CBSDSocketInterface::receiveDataFrom%s recv() Disconnected: nRetVal: %d, ERR: %d %s\n", msg, nRetVal, errno, strerror(errno));
return 0; //Connection closed by peer
default:
DEVLOG_ERROR("CBSDSocketInterface::receiveDataFromTCP recv() Unexpected: nRetVal: %d, ERR: %d %s\n", nRetVal, errno, strerror(errno));
//TODO: this is a "connected = true" case, for the moment we leave it open.
DEVLOG_ERROR("CBSDSocketInterface::receiveDataFrom%s recv() Unexpected: nRetVal: %d, ERR: %d %s\n", msg, nRetVal, errno, strerror(errno));
//connected = true;
break;
}
}
return nRetVal;
}




int CBSDSocketInterface::receiveDataFromTCP(TSocketDescriptor paSockD, char* paData,
unsigned int paBufSize){
int nRetVal;
do{
nRetVal = static_cast<int>(recv(paSockD, paData, paBufSize, 0));
} while((-1 == nRetVal) && (EINTR == errno)); // recv got interrupt / recieving again

if(nRetVal == -1){
DEVLOG_ERROR("CBSDSocketInterface: TCP-Socket recv() failed: %s\n", strerror(errno));
}
return handleError(nRetVal, errno, "TCP");
}

CBSDSocketInterface::TSocketDescriptor CBSDSocketInterface::openUDPSendPort(char *paIPAddr,
unsigned short paPort, TUDPDestAddr *mDestAddr, const char *paMCInterface){
DEVLOG_INFO("CBSDSocketInterface: Opening UDP sending connection at: %s:%d\n", paIPAddr, paPort);
Expand Down Expand Up @@ -309,25 +316,5 @@ int CBSDSocketInterface::receiveDataFromUDP(TSocketDescriptor paSockD, char* paD
DEVLOG_ERROR("CBSDSocketInterface: UDP-Socket recvfrom() failed: %s\n", strerror(errno));
}

// recv only sets errno if res is <= 0
if(nRetVal <= 0) {
switch(errno){
case EWOULDBLOCK:
case ENOENT: //caused by vfs
//TODO: this is a "connected = true" case, for the moment we leave it open.
break;
case ENOTCONN:
case EPIPE:
case ECONNRESET:
case ECONNREFUSED:
case ECONNABORTED:
DEVLOG_ERROR("CBSDSocketInterface::receiveDataFromUDP recvfrom() Disconnected: nRetVal: %d, ERR: %d %s\n", nRetVal, errno, strerror(errno));
return 0; //Connection closed by peer
default:
DEVLOG_ERROR("CBSDSocketInterface::receiveDataFromUDP recvfrom() Unexpected: nRetVal: %d, ERR: %d %s\n", nRetVal, errno, strerror(errno));
//TODO: this is a "connected = true" case, for the moment we leave it open.
break;
}
}
return nRetVal;
return handleError(nRetVal, errno, "UDP");
}
4 changes: 4 additions & 0 deletions src/arch/bsdsocketinterf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class CBSDSocketInterface{
static int sendDataOnUDP(TSocketDescriptor paSockD, TUDPDestAddr *paDestAddr, char* paData, unsigned int paSize);
static int receiveDataFromUDP(TSocketDescriptor paSockD, char* paData, unsigned int paBufSize);


static int handleError(int nRetVal, int err, const char* msg);


CBSDSocketInterface() = delete;
};

Expand Down

0 comments on commit 9add98f

Please sign in to comment.