|
|
@@ -281,7 +281,7 @@ TCPListener::TCPListener(unsigned short Port) { |
|
|
|
LastError = Network.getLastError(); // grab the error code and |
|
|
|
throw Networking::SocketCreationError( // throw. |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPListener::open().socket()", LastError)); |
|
|
|
"TCPListener::TCPListener().socket()", LastError)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -301,7 +301,7 @@ TCPListener::TCPListener(SocketAddress& WhereToBind) { |
|
|
|
LastError = Network.getLastError(); // grab the error code and |
|
|
|
throw Networking::SocketCreationError( // throw. |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPListener::open().socket()", LastError)); |
|
|
|
"TCPListener::TCPListener().socket()", LastError)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -331,7 +331,7 @@ void TCPListener::open() { |
|
|
|
LastError = Network.getLastError(); // Capture the error information and |
|
|
|
throw Networking::SocketSetSockOptError( // throw. |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPListener::open().setsockopt()", LastError)); |
|
|
|
"TCPListener::open().setsockopt(SO_REUSEADDR)", LastError)); |
|
|
|
} |
|
|
|
OpenStage1Complete = true; // Stage 1 complete now. |
|
|
|
} // End of open() stage 1 |
|
|
@@ -388,11 +388,35 @@ TCPClient* TCPListener::acceptClient() { |
|
|
|
if(!Network.WouldBlock(LastError)) { // If it's not a EWOULDBLOCK error |
|
|
|
throw Networking::SocketAcceptError( // then we need to throw. |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPListener::acceptClient()", LastError)); |
|
|
|
"TCPListener::acceptClient().accept()", LastError)); |
|
|
|
} else { // EWOULDBLOCK errors are normal in |
|
|
|
return NULL; // non blocking mode so we return |
|
|
|
} // NULL when we see them. |
|
|
|
} |
|
|
|
|
|
|
|
// Set SO_NOSIGPIPE if needed
|
|
|
|
|
|
|
|
if( // On some systems we may have to
|
|
|
|
0 != SO_NOSIGPIPE && // use SO_NOSIPIPE but if they offer
|
|
|
|
0 == MSG_NOSIGNAL // MSG_NOSIGNAL we prefer that instead.
|
|
|
|
) {
|
|
|
|
int TurnedOn = 1; // Prepare to turn this option on.
|
|
|
|
int result = // Set SO_NOSIGPIPE.
|
|
|
|
setsockopt(
|
|
|
|
NewHandle,
|
|
|
|
SOL_SOCKET,
|
|
|
|
SO_NOSIGPIPE,
|
|
|
|
(char*) &TurnedOn,
|
|
|
|
sizeof(TurnedOn));
|
|
|
|
|
|
|
|
if(0 > result) { // If there was an error then
|
|
|
|
LastError = Network.getLastError(); // Capture the error information
|
|
|
|
Network.closeSocket(NewHandle); // close the handle (avoid leaks)
|
|
|
|
throw Networking::SocketSetSockOptError( // and throw a descriptive exception.
|
|
|
|
Network.DescriptiveError(
|
|
|
|
"TCPListener::acceptClient().setsockopt(SO_NOSIGPIPE)", LastError));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If things have gone well we can do what we came for. |
|
|
|
|
|
|
@@ -420,7 +444,7 @@ int TCPClient::transmit(const char* bfr, int size) { |
|
|
|
} else { // If this was a different kind of error |
|
|
|
throw Networking::SocketWriteError( // then throw! |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPClient::transmit()", LastError)); |
|
|
|
"TCPClient::transmit().send()", LastError)); |
|
|
|
} |
|
|
|
} |
|
|
|
return ByteCount; // Ultimately return the byte count. |
|
|
@@ -483,7 +507,7 @@ TCPHost::TCPHost(unsigned short Port) { |
|
|
|
LastError = Network.getLastError(); // grab the error code and |
|
|
|
throw Networking::SocketCreationError( // throw. |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPHost::open().socket()", LastError)); |
|
|
|
"TCPHost::TCPHost().socket()", LastError)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -503,7 +527,7 @@ TCPHost::TCPHost(SocketAddress& Remote) { |
|
|
|
LastError = Network.getLastError(); // grab the error code and |
|
|
|
throw Networking::SocketCreationError( // throw. |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPHost::open().socket()", LastError)); |
|
|
|
"TCPHost::TCPHost().socket()", LastError)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -516,16 +540,19 @@ void TCPHost::open() { |
|
|
|
LastError = 0; // Clear our LastError value. |
|
|
|
bool SuccessFlag = true; // Begin optimistically. |
|
|
|
|
|
|
|
// Set SO_REUSEADDR if turned on |
|
|
|
|
|
|
|
// Set Socket Options
|
|
|
|
|
|
|
|
if(!OpenStage1Complete) { // If we haven't done this yet: |
|
|
|
|
|
|
|
// Set SO_REUSEADDR if turned on
|
|
|
|
|
|
|
|
int ReuseAddress_Flag = (ReuseAddress? 1:0); // Setup an appropriate integer flag. |
|
|
|
int result = // Set SO_REUSEADDR before bind(). |
|
|
|
setsockopt( |
|
|
|
Handle, |
|
|
|
SOL_SOCKET, |
|
|
|
SO_REUSEADDR, |
|
|
|
(char *)&ReuseAddress_Flag, |
|
|
|
(char*) &ReuseAddress_Flag, |
|
|
|
sizeof(ReuseAddress_Flag)); |
|
|
|
|
|
|
|
if(0 > result) { // If there was an error then |
|
|
@@ -533,8 +560,33 @@ void TCPHost::open() { |
|
|
|
LastError = Network.getLastError(); // Capture the error information and |
|
|
|
throw Networking::SocketSetSockOptError( // throw. |
|
|
|
Network.DescriptiveError( |
|
|
|
"TCPListener::open().setsockopt()", LastError)); |
|
|
|
"TCPHost::open().setsockopt(SO_REUSEADDR)", LastError)); |
|
|
|
} |
|
|
|
|
|
|
|
// Set SO_NOSIGPIPE if needed
|
|
|
|
|
|
|
|
if( // On some systems we may have to
|
|
|
|
0 != SO_NOSIGPIPE && // use SO_NOSIPIPE but if they offer
|
|
|
|
0 == MSG_NOSIGNAL // MSG_NOSIGNAL we prefer that instead.
|
|
|
|
) {
|
|
|
|
int TurnedOn = 1; // Prepare to turn this option on.
|
|
|
|
int result = // Set SO_NOSIGPIPE.
|
|
|
|
setsockopt(
|
|
|
|
Handle,
|
|
|
|
SOL_SOCKET,
|
|
|
|
SO_NOSIGPIPE,
|
|
|
|
(char*) &TurnedOn,
|
|
|
|
sizeof(TurnedOn));
|
|
|
|
|
|
|
|
if(0 > result) { // If there was an error then
|
|
|
|
SuccessFlag = false; // we did not succeed.
|
|
|
|
LastError = Network.getLastError(); // Capture the error information and
|
|
|
|
throw Networking::SocketSetSockOptError( // throw.
|
|
|
|
Network.DescriptiveError(
|
|
|
|
"TCPHost::open().setsockopt(SO_NOSIGPIPE)", LastError));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenStage1Complete = true; // Skip this section from now on. |
|
|
|
} // Done with stage 1. |
|
|
|
|