Skip to content

Commit

Permalink
Refactor BSD socket interface receive functions
Browse files Browse the repository at this point in the history
- Handle different error cases in receiveDataFromTCP and receiveDataFromUDP
- Log specific error messages for disconnection scenarios
- Return 0 when the connection is closed by the peer
  • Loading branch information
franz-hoepfinger-4diac committed Feb 22, 2024
1 parent efd44e9 commit a187cfe
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/arch/bsdsocketinterf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ int CBSDSocketInterface::receiveDataFromTCP(TSocketDescriptor paSockD, char* paD
if(nRetVal == -1){
DEVLOG_ERROR("CBSDSocketInterface: TCP-Socket recv() 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::receiveDataFromTCP recv() Disconnected: nRetVal: %d, ERR: %d %s\n", 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.
break;
}
}
return nRetVal;
}

Expand Down Expand Up @@ -287,5 +308,26 @@ int CBSDSocketInterface::receiveDataFromUDP(TSocketDescriptor paSockD, char* paD
if(nRetVal == -1){ //
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;
}

0 comments on commit a187cfe

Please sign in to comment.