medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 5256e8aa392155d18d74aa31e6fd0fd615b7be57
parent 69303fc7da6d9efedc119b35437335e1f84bcbe9
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Dec 28 21:48:06 +0200

Disconnect players if they don't send anything for 5 seconds

Diffstat:
hm.cc | 75++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
server/main.cc | 25++++++++++++++++++++++++-
2 files changed, 70 insertions(+), 30 deletions(-)
diff --git a/hm.cc b/hm.cc @@ -317,7 +317,7 @@ extern "C" GAME_FRAME( game_frame ) { ReadStream rs( buf, len ); - if( connected ) { + if( connected && len > 0 ) { char write_buf[ 1400 ]; WriteStream ws( write_buf ); write( &ws, sid ); @@ -330,57 +330,74 @@ extern "C" GAME_FRAME( game_frame ) { for( u16 i = 0; i < player_count; i++ ) { read_u64( &rs ); - v3 pos; - read( &rs, &pos ); + u8 player_state = read_u8( &rs ); + if( player_state == 0 ) { + v3 pos; + read( &rs, &pos ); + } } if( rs.done() ) { rs.reset( &rsc ); for( u16 i = 0; i < player_count; i++ ) { - u64 s = read_u64( &rs ); + u64 player_sid = read_u64( &rs ); + u8 player_state = read_u8( &rs ); v3 pos; - read( &rs, &pos ); + if( player_state == 0 ) { + read( &rs, &pos ); + } - Player * player; - if( !sid_to_player.get( s, &player ) ) { + Player * player = NULL; + if( !sid_to_player.get( player_sid, &player ) && player_state == 0 ) { player = players.acquire(); ASSERT( player != NULL ); // TODO - sid_to_player.add( s, player ); - player->sid = s; + sid_to_player.add( player_sid, player ); + player->sid = player_sid; } - player->pos = pos; + if( player_state == 0 ) { + player->pos = pos; + } + else if( player_state == 1 ) { + printf( "%08lx disconnected\n", player->sid ); + players.release( player ); + } } ASSERT( rs.done() ); } - - ImmediateContext imm; - ImmediateTriangle boxes[ 4096 ]; - immediate_init( &imm, boxes, ARRAY_COUNT( boxes ) ); - - for( const Player * player : players ) { - if( player->sid != sid ) { - v3 mins = player->pos - v3( 0.4, 0.4, 1.8 ); - v3 maxs = player->pos + v3( 0.4, 0.4, 0.1 ); - immediate_aabb( &imm, mins, maxs, v4( 1, 1, 0, 1 ) ); + else { + if( len > 0 ) { + FATAL( "bad message from the server" ); } } - - m4 VP = V * P; - renderer_ub_data( ub_sphere, &VP, sizeof( VP ) ); - - RenderState impact_render_state; - impact_render_state.shader = game->test_outline_shader; - impact_render_state.ubs[ UB_VS_HOT ] = ub_sphere; - immediate_render( &imm, impact_render_state ); } - else { + else if( len > 0 ) { sid = read_u64( &rs ); if( rs.ok ) { connected = true; } } + + ImmediateContext imm; + ImmediateTriangle boxes[ 4096 ]; + immediate_init( &imm, boxes, ARRAY_COUNT( boxes ) ); + + for( const Player * player : players ) { + if( player->sid != sid ) { + v3 mins = player->pos - v3( 0.4, 0.4, 1.8 ); + v3 maxs = player->pos + v3( 0.4, 0.4, 0.1 ); + immediate_aabb( &imm, mins, maxs, v4( 1, 1, 0, 1 ) ); + } + } + + m4 VP = V * P; + renderer_ub_data( ub_sphere, &VP, sizeof( VP ) ); + + RenderState impact_render_state; + impact_render_state.shader = game->test_outline_shader; + impact_render_state.ubs[ UB_VS_HOT ] = ub_sphere; + immediate_render( &imm, impact_render_state ); } { diff --git a/server/main.cc b/server/main.cc @@ -1,3 +1,5 @@ +#include <sys/time.h> + #include <stdio.h> #include <errno.h> @@ -14,10 +16,13 @@ #define PORT 13337 #define MAX_PLAYERS 128 +#define DISCONNECT_TIMEOUT 5.0 + struct PlayerState { u64 sid; v3 pos; sockaddr_storage addr; + double last_message_time; u16 next_free; }; @@ -81,6 +86,13 @@ static void send( int fd, const PlayerState * state, const WriteStream & ws ) { sendto( fd, ws.start, ws.len(), 0, ( sockaddr * ) &state->addr, sizeof( state->addr ) ); } +static double get_time() { + struct timespec ts; + clock_gettime( CLOCK_MONOTONIC, &ts ); + + return ( double ) ts.tv_sec + ts.tv_nsec / 1000000000.0; +} + int main() { int server_fd_ipv4 = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); int server_fd_ipv6 = socket( AF_INET6, SOCK_DGRAM, IPPROTO_UDP ); @@ -124,6 +136,7 @@ int main() { for( ;; ) { printf( "frame\n" ); + const double now = get_time(); sockaddr_storage addr; char read_buf[ 1400 ]; @@ -149,6 +162,7 @@ int main() { state->sid = new_sid; state->pos = pos; state->addr = addr; + state->last_message_time = now; sids_to_states.add( new_sid, state ); @@ -168,6 +182,7 @@ int main() { if( !r.ok ) continue; state->pos = pos; + state->last_message_time = now; } } } @@ -180,7 +195,15 @@ int main() { for( const PlayerState * state : states ) { write( &w, state->sid ); - write( &w, state->pos ); + if( state->last_message_time + DISCONNECT_TIMEOUT <= now ) { + printf( "%08lx disconnected\n", state->sid ); + write_u8( &w, 1 ); + states.release( state ); + } + else { + write_u8( &w, 0 ); + write( &w, state->pos ); + } player_count++; }