diff --git a/Inc/stream.h b/Inc/stream.h index 345cfe76..29f7d500 100644 --- a/Inc/stream.h +++ b/Inc/stream.h @@ -16,10 +16,19 @@ enum ReceiveResult RECEIVE_RESULT_ERROR, }; +enum TransmitResult +{ + TRANSMIT_RESULT_OK, + TRANSMIT_RESULT_TIMEOUT, + TRANSMIT_RESULT_ERROR, +}; + struct Stream { enum ReceiveResult ( *receive )( struct Stream *stream, void *buffer, size_t bufferSize, struct timeval *timeout, size_t *receivedSize ); + enum TransmitResult ( *send )( struct Stream *stream, const void *buffer, size_t bufferSize, + struct timeval *timeout, size_t *transmitSize ); void ( *close )( struct Stream *stream ); }; diff --git a/Src/stream_file_posix.c b/Src/stream_file_posix.c index 0b684f1b..b1831ed5 100644 --- a/Src/stream_file_posix.c +++ b/Src/stream_file_posix.c @@ -90,6 +90,7 @@ struct Stream *streamCreateFile( const char *file ) } stream->base.receive = _posixFileStreamReceive; + stream->base.send = NULL; /* Files are opened read-only */ stream->base.close = _posixFileStreamClose; stream->file = _posixFileStreamCreate( file ); diff --git a/Src/stream_file_win32.c b/Src/stream_file_win32.c index c23a79f4..2e21c352 100644 --- a/Src/stream_file_win32.c +++ b/Src/stream_file_win32.c @@ -61,5 +61,7 @@ struct Stream *streamCreateFile( const char *file ) return NULL; } + stream->base.base.send = NULL; /* Files are opened read-only */ + return &stream->base.base; } \ No newline at end of file diff --git a/Src/stream_socket_posix.c b/Src/stream_socket_posix.c index b011c54f..fc99cfdc 100644 --- a/Src/stream_socket_posix.c +++ b/Src/stream_socket_posix.c @@ -10,12 +10,28 @@ #include #include #include +#include +#include +#include #include "generics.h" /* How long to wait for a connection before declaring failure */ #define CONNECT_WAIT_TIME_MS (2000) +/* Don't let a send to a closed peer raise SIGPIPE and kill the process. Linux has a + * per-call flag; macOS/BSD use the SO_NOSIGPIPE socket option set at creation instead. */ +#ifdef MSG_NOSIGNAL + #define NOSIGNAL_FLAG MSG_NOSIGNAL +#else + #define NOSIGNAL_FLAG 0 +#endif + +/* The socket is blocking, and a blocking send() waits until the whole buffer is queued, + * which would bypass the timeout. MSG_DONTWAIT makes each call take only what fits, and + * the loop goes back to select() for the rest. */ +#define SEND_FLAGS ( MSG_DONTWAIT | NOSIGNAL_FLAG ) + struct PosixSocketStream { struct Stream base; @@ -67,6 +83,90 @@ static enum ReceiveResult _posixSocketStreamReceive( struct Stream *stream, void return RECEIVE_RESULT_OK; } +// ==================================================================================================== +static uint64_t _nowMs( void ) +{ + struct timespec ts; + clock_gettime( CLOCK_MONOTONIC, &ts ); + return ( uint64_t )ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} + +// ==================================================================================================== +static enum TransmitResult _posixSocketStreamTransmit( struct Stream *stream, const void *buffer, size_t bufferSize, + struct timeval *timeout, size_t *transmitSize ) +{ + struct PosixSocketStream *self = SELF( stream ); + const char *p = buffer; + uint64_t deadline = 0; + + /* The timeout covers the whole call, not each select() */ + if ( timeout ) + { + deadline = _nowMs() + ( uint64_t )timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + } + + *transmitSize = 0; + + while ( *transmitSize < bufferSize ) + { + fd_set writeFd; + FD_ZERO( &writeFd ); + FD_SET( self->socket, &writeFd ); + + struct timeval tv; + struct timeval *tvp = NULL; + + if ( timeout ) + { + uint64_t now = _nowMs(); + uint64_t left = ( now < deadline ) ? deadline - now : 0; + tv.tv_sec = left / 1000; + tv.tv_usec = ( left % 1000 ) * 1000; + tvp = &tv; + } + + int r = select( self->socket + 1, NULL, &writeFd, NULL, tvp ); + + if ( r < 0 ) + { + if ( errno == EINTR ) + { + return TRANSMIT_RESULT_TIMEOUT; + } + + return TRANSMIT_RESULT_ERROR; + } + + if ( r == 0 ) + { + return TRANSMIT_RESULT_TIMEOUT; + } + + ssize_t result = send( self->socket, p + *transmitSize, bufferSize - *transmitSize, SEND_FLAGS ); + + if ( result < 0 ) + { + if ( errno == EAGAIN || errno == EWOULDBLOCK ) + { + /* Buffer filled up since select(): wait for space again */ + continue; + } + + if ( errno == EINTR ) + { + return TRANSMIT_RESULT_TIMEOUT; + } + + // report connection broken as error + return TRANSMIT_RESULT_ERROR; + } + + *transmitSize += result; + } + + return TRANSMIT_RESULT_OK; +} + // ==================================================================================================== static void _posixSocketStreamClose( struct Stream *stream ) { @@ -90,6 +190,9 @@ static int _posixSocketStreamCreate( const char *server, int port ) } setsockopt( sockfd, SOL_SOCKET, SO_REUSEPORT, ( const void * )&flag, sizeof( flag ) ); +#ifdef SO_NOSIGPIPE + setsockopt( sockfd, SOL_SOCKET, SO_NOSIGPIPE, ( const void * )&flag, sizeof( flag ) ); +#endif if ( sockfd < 0 ) { @@ -172,6 +275,7 @@ struct Stream *streamCreateSocket( const char *server, int port ) } stream->base.receive = _posixSocketStreamReceive; + stream->base.send = _posixSocketStreamTransmit; stream->base.close = _posixSocketStreamClose; stream->socket = _posixSocketStreamCreate( server, port ); diff --git a/Src/stream_socket_win32.c b/Src/stream_socket_win32.c index 75165486..de188612 100644 --- a/Src/stream_socket_win32.c +++ b/Src/stream_socket_win32.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "generics.h" @@ -28,6 +29,72 @@ struct Win32SocketStream // ==================================================================================================== +// ==================================================================================================== +static enum TransmitResult _win32SocketStreamTransmit( struct Stream *stream, const void *buffer, size_t bufferSize, + struct timeval *timeout, size_t *transmitSize ) + +{ + SOCKET s = ( SOCKET )( intptr_t )SELF( stream )->base.source; + const char *p = buffer; + ULONGLONG deadline = 0; + + if ( timeout ) + { + deadline = GetTickCount64() + ( ULONGLONG )timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + } + + *transmitSize = 0; + + while ( *transmitSize < bufferSize ) + { + fd_set writeFd; + FD_ZERO( &writeFd ); + FD_SET( s, &writeFd ); + + struct timeval tv; + struct timeval *tvp = NULL; + + if ( timeout ) + { + ULONGLONG now = GetTickCount64(); + ULONGLONG left = ( now < deadline ) ? deadline - now : 0; + tv.tv_sec = ( long )( left / 1000 ); + tv.tv_usec = ( long )( ( left % 1000 ) * 1000 ); + tvp = &tv; + } + + int r = select( 0, NULL, &writeFd, NULL, tvp ); + + if ( r == SOCKET_ERROR ) + { + return TRANSMIT_RESULT_ERROR; + } + + if ( r == 0 ) + { + return TRANSMIT_RESULT_TIMEOUT; + } + + size_t remaining = bufferSize - *transmitSize; + int result = send( s, p + *transmitSize, ( int )( remaining > INT_MAX ? INT_MAX : remaining ), 0 ); + + if ( result == SOCKET_ERROR ) + { + if ( WSAGetLastError() == WSAEWOULDBLOCK ) + { + /* Buffer filled up since select(): wait for space again */ + continue; + } + + return TRANSMIT_RESULT_ERROR; + } + + *transmitSize += result; + } + + return TRANSMIT_RESULT_OK; +} + // ==================================================================================================== static void _win32SocketStreamClose( struct Stream *stream ) { @@ -82,6 +149,11 @@ static HANDLE _win32SocketStreamCreate( const char *server, int port ) return INVALID_HANDLE_VALUE; } + /* Non-blocking, so a send() to a peer that has stopped reading can't block past its + * timeout. Receive uses overlapped ReadFile, which non-blocking mode doesn't affect. */ + u_long nonBlocking = 1; + ioctlsocket( sockfd, FIONBIO, &nonBlocking ); + return ( HANDLE )( intptr_t )sockfd; } @@ -109,6 +181,7 @@ struct Stream *streamCreateSocket( const char *server, int port ) return NULL; } + stream->base.base.send = _win32SocketStreamTransmit; stream->base.base.close = _win32SocketStreamClose; return &stream->base.base;