Browse Source

Updated TCPClient.transmit() to improve error handling and detection. This should prevent cases where an exception was thrown for a non-error (Success) condition. The bug was found in a log on an SNFMilter system where we were researching incomplete SYNC sessions. It is hoped this condition explains those incomplete SYNC sessions and that this new code will fix it.

git-svn-id: https://svn.microneil.com/svn/CodeDweller/trunk@20 d34b734f-a00e-4b39-a726-e4eeb87269ab
wx
madscientist 14 years ago
parent
commit
ecede0dcf9
1 changed files with 13 additions and 9 deletions
  1. 13
    9
      networking.cpp

+ 13
- 9
networking.cpp View File

//// TCPClient methods ///////////////////////////////////////////////////////// //// TCPClient methods /////////////////////////////////////////////////////////


int TCPClient::transmit(const char* bfr, int size) { // How to send a buffer of data. int TCPClient::transmit(const char* bfr, int size) { // How to send a buffer of data.
LastError = 0; // No errors yet.
if(0 == size) return 0; // Nothing to send, send nothing. if(0 == size) return 0; // Nothing to send, send nothing.
if(0 == bfr) // Watch out for null buffers. if(0 == bfr) // Watch out for null buffers.
throw Networking::SocketWriteError("TCPClient::transmit() NULL Bfr!"); throw Networking::SocketWriteError("TCPClient::transmit() NULL Bfr!");
if(0 > size) // Watch out for bad sizes. if(0 > size) // Watch out for bad sizes.
throw Networking::SocketWriteError("TCPClient::transmit() 0 > size!"); throw Networking::SocketWriteError("TCPClient::transmit() 0 > size!");


int ByteCount = send(Handle, bfr, size, MSG_NOSIGNAL); // Try to send and capture the count.
if(0 > ByteCount) ByteCount = 0; // Mask error results as 0 bytes sent.

if(size > ByteCount) { // If we didn't send it all check it out.
LastError = Network.getLastError(); // Grab the error code.
if(Network.WouldBlock(LastError)) { // If the error was WouldBlock then
return ByteCount; // it was a partial send - return.
LastError = 0; // No errors yet.
int ByteCount = 0; // No bytes sent yet this pass.
ByteCount = send(Handle, bfr, size, MSG_NOSIGNAL); // Try to send and capture the count.
LastError = Network.getLastError(); // Grab any error code.
bool AnErrorOccurred = (0 > ByteCount); // How to know if an error occurred.
const int NoBytesSent = 0; // This is our "Would Block" result.
if(AnErrorOccurred) { // If there was an error check it out.
if(Network.WouldBlock(LastError)) { // If the error was "Would Block" then
return NoBytesSent; // return no bytes sent (try again).
} else { // If this was a different kind of error } else { // If this was a different kind of error
throw Networking::SocketWriteError( // then throw! throw Networking::SocketWriteError( // then throw!
Network.DescriptiveError( Network.DescriptiveError(
"TCPClient::transmit().send()", LastError)); "TCPClient::transmit().send()", LastError));
} }
} }
return ByteCount; // Ultimately return the byte count.
return ByteCount; // Usually: return the sent byte count.
} }


int TCPClient::receive(char* bfr, int size) { // How to receive a buffer of data. int TCPClient::receive(char* bfr, int size) { // How to receive a buffer of data.

Loading…
Cancel
Save