Skip to content

Commit

Permalink
see if this compiles on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Sep 17, 2024
1 parent 8bd7549 commit 7c3409b
Showing 1 changed file with 74 additions and 69 deletions.
143 changes: 74 additions & 69 deletions netcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,39 +461,6 @@ void netcode_socket_destroy( struct netcode_socket_t * socket )
}
}

#if NETCODE_PACKET_TAGGING && NETCODE_PLATFORM == NETCODE_PLATFORM_WINDOWS

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <ws2ipdef.h>
#include <wininet.h>
#include <iphlpapi.h>
#include <qos2.h>

#pragma comment( lib, "Qwave.lib" )

static int netcode_set_socket_codepoint( SOCKET socket, QOS_TRAFFIC_TYPE trafficType, QOS_FLOWID flowId, PSOCKADDR addr )
{
QOS_VERSION QosVersion = { 1 , 0 };
HANDLE qosHandle;
if ( QOSCreateHandle( &QosVersion, &qosHandle ) == FALSE )
{
// todo
printf( "QOSCreateHandler failed\n" );
return GetLastError();
}
if ( QOSAddSocketToFlow( qosHandle, socket, addr, trafficType, QOS_NON_ADAPTIVE_FLOW, &flowId ) == FALSE )
{
// todo
printf( "QOSAddSocketToFlow failed\n" );
return GetLastError();
}
return 0;
}

#endif // #if NETCODE_PACKET_TAGGING && NETCODE_PLATFORM == NETCODE_PLATFORM_WINDOWS

int netcode_socket_create( struct netcode_socket_t * s, struct netcode_address_t * address, int send_buffer_size, int receive_buffer_size )
{
netcode_assert( s );
Expand Down Expand Up @@ -719,52 +686,94 @@ int netcode_socket_create( struct netcode_socket_t * s, struct netcode_address_t

#elif NETCODE_PLATFORM == NETCODE_PLATFORM_WINDOWS

if ( netcode_packet_tagging_enabled )
// IMPORTANT: Tagging on Windows is QOS based and requires the destination address
// consequently, it's only suitable for clients, and needs to be done elsewhere.

#endif

#endif // #if NETCODE_PACKET_TAGGING

return NETCODE_SOCKET_ERROR_NONE;
}

#if NETCODE_PLATFORM == NETCODE_PLATFORM_WINDOWS

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <ws2ipdef.h>
#include <wininet.h>
#include <iphlpapi.h>
#include <qos2.h>

#pragma comment( lib, "Qwave.lib" )

static int netcode_set_socket_codepoint( SOCKET socket, QOS_TRAFFIC_TYPE trafficType, QOS_FLOWID flowId, PSOCKADDR addr )
{
QOS_VERSION QosVersion = { 1 , 0 };
HANDLE qosHandle;
if ( QOSCreateHandle( &QosVersion, &qosHandle ) == FALSE )
{
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
struct sockaddr * addr = NULL;
// todo
printf( "QOSCreateHandler failed\n" );
return GetLastError();
}
if ( QOSAddSocketToFlow( qosHandle, socket, addr, trafficType, QOS_NON_ADAPTIVE_FLOW, &flowId ) == FALSE )
{
// todo
int error = GetLastError();
printf( "QOSAddSocketToFlow failed: %d\n", error );
return error;
}
return 0;
}

if ( address->type == NETCODE_ADDRESS_IPV6 )
void netcode_socket_set_qos( struct netcode_socket_t * socket, struct netcode_address_t * to )
{
netcode_assert( socket );
netcode_assert( to );

if ( to->type == NETCODE_ADDRESS_IPV6 )
{
struct sockaddr_in6 socket_address;
memset( &socket_address, 0, sizeof( socket_address ) );
socket_address.sin6_family = AF_INET6;
int i;
for ( i = 0; i < 8; i++ )
{
addr = (struct sockaddr*) &sin6;
socklen_t len = sizeof( sin6 );
if ( getsockname( s->handle, addr, &len ) == -1 )
{
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to get socket address (ipv6)\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
}
address->port = ntohs( sin6.sin6_port );
( (uint16_t*) &socket_address.sin6_addr ) [i] = htons( to->data.ipv6[i] );
}
else
socket_address.sin6_port = htons( to->port );

if ( netcode_set_socket_codepoint( s->handle, QOSTrafficTypeAudioVideo, 0, &socket_address ) != 0 )
{
addr = (struct sockaddr*) &sin4;
socklen_t len = sizeof( sin4 );
if ( getsockname( s->handle, addr, &len ) == -1 )
{
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to get socket address (ipv4)\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
}
address->port = ntohs( sin4.sin_port );
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to enable packet tagging (ipv6)\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
}
}
else if ( to->type == NETCODE_ADDRESS_IPV4 )
{
struct sockaddr_in socket_address;
memset( &socket_address, 0, sizeof( socket_address ) );
socket_address.sin_family = AF_INET;
socket_address.sin_addr.s_addr = ( ( (uint32_t) to->data.ipv4[0] ) ) |
( ( (uint32_t) to->data.ipv4[1] ) << 8 ) |
( ( (uint32_t) to->data.ipv4[2] ) << 16 ) |
( ( (uint32_t) to->data.ipv4[3] ) << 24 );
socket_address.sin_port = htons( to->port );

if ( netcode_set_socket_codepoint( s->handle, QOSTrafficTypeAudioVideo, 0, addr ) != 0 )
if ( netcode_set_socket_codepoint( s->handle, QOSTrafficTypeAudioVideo, 0, &socket_address ) != 0 )
{
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to enable packet tagging\n" );
netcode_printf( NETCODE_LOG_LEVEL_ERROR, "error: failed to enable packet tagging (ipv4)\n" );
netcode_socket_destroy( s );
return NETCODE_SOCKET_ERROR_ENABLE_PACKET_TAGGING_FAILED;
}
}

#endif

#endif // #if NETCODE_PACKET_TAGGING

return NETCODE_SOCKET_ERROR_NONE;
}

#endif // #if NETCODE_PLATFORM == NETCODE_PLATFORM_WINDOWS

void netcode_socket_send_packet( struct netcode_socket_t * socket, struct netcode_address_t * to, void * packet_data, int packet_bytes )
{
netcode_assert( socket );
Expand Down Expand Up @@ -9151,8 +9160,6 @@ void test_packet_tagging()

netcode_enable_packet_tagging();

// todo
printf( "ipv4\n" );
{
struct netcode_server_config_t server_config;
netcode_default_server_config( &server_config );
Expand All @@ -9170,8 +9177,6 @@ void test_packet_tagging()
netcode_server_destroy( server );
}

// todo
printf( "ipv6\n" );
{
struct netcode_server_config_t server_config;
netcode_default_server_config( &server_config );
Expand Down

0 comments on commit 7c3409b

Please sign in to comment.