Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Inc/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
};

Expand Down
1 change: 1 addition & 0 deletions Src/stream_file_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
2 changes: 2 additions & 0 deletions Src/stream_file_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
104 changes: 104 additions & 0 deletions Src/stream_socket_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,28 @@
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <errno.h>
#include <time.h>
#include <stdint.h>

#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;
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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 );

Expand Down
73 changes: 73 additions & 0 deletions Src/stream_socket_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>

#include "generics.h"
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down