Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)

project(netcode VERSION 1.3.4 LANGUAGES C CXX)
project(netcode VERSION 1.3.5 LANGUAGES C CXX)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
Expand Down
61 changes: 46 additions & 15 deletions netcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,20 +680,43 @@ int netcode_socket_create( struct netcode_socket_t * s, struct netcode_address_t
}
}

// increase socket send and receive buffer sizes
// increase socket send and receive buffer sizes. linux and windows clamp requests that
// exceed the OS limit, but the BSDs reject them instead, so back off until accepted.

if ( setsockopt( s->handle, SOL_SOCKET, SO_SNDBUF, (char*)&send_buffer_size, sizeof(int) ) != 0 )
{
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to set socket send buffer size\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_SOCKOPT_SNDBUF_FAILED;
int size = send_buffer_size;
while ( setsockopt( s->handle, SOL_SOCKET, SO_SNDBUF, (char*)&size, sizeof(int) ) != 0 )
{
size /= 2;
if ( size < 256 * 1024 )
{
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to set socket send buffer size\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_SOCKOPT_SNDBUF_FAILED;
}
}
if ( size != send_buffer_size )
{
netcode_printf( NETCODE_LOG_LEVEL_INFO, "socket send buffer size reduced from %d to %d\n", send_buffer_size, size );
}
}

if ( setsockopt( s->handle, SOL_SOCKET, SO_RCVBUF, (char*)&receive_buffer_size, sizeof(int) ) != 0 )
{
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to set socket receive buffer size\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_SOCKOPT_RCVBUF_FAILED;
int size = receive_buffer_size;
while ( setsockopt( s->handle, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(int) ) != 0 )
{
size /= 2;
if ( size < 256 * 1024 )
{
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to set socket receive buffer size\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_SOCKOPT_RCVBUF_FAILED;
}
}
if ( size != receive_buffer_size )
{
netcode_printf( NETCODE_LOG_LEVEL_INFO, "socket receive buffer size reduced from %d to %d\n", receive_buffer_size, size );
}
}

// bind to port
Expand Down Expand Up @@ -5325,7 +5348,7 @@ int netcode_generate_connect_token( int num_server_addresses,

// ---------------------------------------------------------------

#if __APPLE__
#if NETCODE_PLATFORM == NETCODE_PLATFORM_MAC

// MacOS

Expand Down Expand Up @@ -5356,12 +5379,20 @@ double netcode_time()
return ( (double) ( current - start ) ) * ( (double) timebase_info.numer ) / ( (double) timebase_info.denom ) / 1000000000.0;
}

#elif __linux
#elif NETCODE_PLATFORM == NETCODE_PLATFORM_UNIX

// linux
// linux and other unix systems (openbsd, freebsd etc.)

#include <unistd.h>

// linux has CLOCK_MONOTONIC_RAW, but other unix systems only have CLOCK_MONOTONIC

#ifdef CLOCK_MONOTONIC_RAW
#define NETCODE_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
#else
#define NETCODE_MONOTONIC_CLOCK CLOCK_MONOTONIC
#endif

void netcode_sleep( double time )
{
struct timespec ts;
Expand All @@ -5376,17 +5407,17 @@ double netcode_time()
if ( start == -1 )
{
struct timespec ts;
clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
clock_gettime( NETCODE_MONOTONIC_CLOCK, &ts );
start = ts.tv_sec + ( (double) ( ts.tv_nsec ) ) / 1000000000.0;
return 0.0;
}
struct timespec ts;
clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
clock_gettime( NETCODE_MONOTONIC_CLOCK, &ts );
double current = ts.tv_sec + ( (double) ( ts.tv_nsec ) ) / 1000000000.0;
return current - start;
}

#elif defined( _WIN32 )
#elif NETCODE_PLATFORM == NETCODE_PLATFORM_WINDOWS

// windows

Expand Down
4 changes: 2 additions & 2 deletions netcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#ifndef NETCODE_H
#define NETCODE_H

#define NETCODE_VERSION_FULL "1.3.4"
#define NETCODE_VERSION_FULL "1.3.5"
#define NETCODE_VERSION_MAJOR 1
#define NETCODE_VERSION_MINOR 3
#define NETCODE_VERSION_PATCH 4
#define NETCODE_VERSION_PATCH 5

/*
IMPORTANT: netcode is single-threaded by design and is not thread safe.
Expand Down
Loading