On Unix systems, a socket disconnection no longer stops the program with signal SIGPIPE (#72)

This commit is contained in:
Laurent Gomila 2013-06-14 15:18:08 +02:00
parent 7051d43c72
commit 94fc605a70
3 changed files with 21 additions and 2 deletions

View File

@ -108,6 +108,14 @@ void Socket::create(SocketHandle handle)
err() << "Failed to set socket option \"TCP_NODELAY\" ; " err() << "Failed to set socket option \"TCP_NODELAY\" ; "
<< "all your TCP packets will be buffered" << std::endl; << "all your TCP packets will be buffered" << std::endl;
} }
// On Mac OS X, disable the SIGPIPE signal on disconnection
#ifdef SFML_SYSTEM_MACOS
if (setsockopt(m_socket, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<char*>(&yes), sizeof(yes)) == -1)
{
err() << "Failed to set socket option \"SO_NOSIGPIPE\"" << std::endl;
}
#endif
} }
else else
{ {

View File

@ -38,6 +38,16 @@
#endif #endif
namespace
{
// Define the low-level send/receive flags, which depend on the OS
#ifdef SFML_SYSTEM_LINUX
const int flags = MSG_NOSIGNAL;
#else
const int flags = 0;
#endif
}
namespace sf namespace sf
{ {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -221,7 +231,7 @@ Socket::Status TcpSocket::send(const void* data, std::size_t size)
for (int length = 0; length < sizeToSend; length += sent) for (int length = 0; length < sizeToSend; length += sent)
{ {
// Send a chunk of data // Send a chunk of data
sent = ::send(getHandle(), static_cast<const char*>(data) + length, sizeToSend - length, 0); sent = ::send(getHandle(), static_cast<const char*>(data) + length, sizeToSend - length, flags);
// Check for errors // Check for errors
if (sent < 0) if (sent < 0)
@ -246,7 +256,7 @@ Socket::Status TcpSocket::receive(void* data, std::size_t size, std::size_t& rec
} }
// Receive a chunk of bytes // Receive a chunk of bytes
int sizeReceived = recv(getHandle(), static_cast<char*>(data), static_cast<int>(size), 0); int sizeReceived = recv(getHandle(), static_cast<char*>(data), static_cast<int>(size), flags);
// Check the number of bytes received // Check the number of bytes received
if (sizeReceived > 0) if (sizeReceived > 0)

View File

@ -90,6 +90,7 @@ Socket::Status SocketImpl::getErrorStatus()
case ETIMEDOUT : return Socket::Disconnected; case ETIMEDOUT : return Socket::Disconnected;
case ENETRESET : return Socket::Disconnected; case ENETRESET : return Socket::Disconnected;
case ENOTCONN : return Socket::Disconnected; case ENOTCONN : return Socket::Disconnected;
case EPIPE : return Socket::Disconnected;
default : return Socket::Error; default : return Socket::Error;
} }
} }