2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
5 #define _DEFAULT_SOURCE
11 volatile sig_atomic_t sig_stop
;
13 #include "gameserver.h"
14 #include "vg/vg_opt.h"
15 #include "network_common.h"
16 #include "gameserver_db.h"
18 #include "vg/vg_msg.h"
20 static u64
const k_steamid_max
= 0xffffffffffffffff;
22 static void inthandler( int signum
) {
26 static void release_message( SteamNetworkingMessage_t
*msg
)
30 if( msg
->m_nUserData
== 0 )
31 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
35 * Send message to single client, with authentication checking
37 static void gameserver_send_to_client( i32 client_id
,
38 const void *pData
, u32 cbData
,
41 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
43 if( gameserver
.loopback_test
&& !client
->connection
)
46 if( !client
->steamid
)
49 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
50 hSteamNetworkingSockets
, client
->connection
,
51 pData
, cbData
, nSendFlags
, NULL
);
55 * Send message to all clients if they are authenticated
57 static void gameserver_send_to_all( int ignore
,
58 const void *pData
, u32 cbData
,
61 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ )
63 struct gameserver_client
*client
= &gameserver
.clients
[i
];
66 gameserver_send_to_client( i
, pData
, cbData
, nSendFlags
);
70 static void gameserver_send_version_to_client( int index
)
72 struct gameserver_client
*client
= &gameserver
.clients
[index
];
74 if( gameserver
.loopback_test
&& !client
->connection
)
77 netmsg_version version
;
78 version
.inetmsg_id
= k_inetmsg_version
;
79 version
.version
= NETWORK_SKATERIFT_VERSION
;
80 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
81 hSteamNetworkingSockets
, client
->connection
,
82 &version
, sizeof(netmsg_version
),
83 k_nSteamNetworkingSend_Reliable
, NULL
);
87 * handle server update that client #'index' has joined
89 static void gameserver_player_join( int index
)
91 struct gameserver_client
*joiner
= &gameserver
.clients
[index
];
93 netmsg_playerjoin join
= { .inetmsg_id
= k_inetmsg_playerjoin
,
95 .steamid
= joiner
->steamid
};
97 gameserver_send_to_all( index
, &join
, sizeof(join
),
98 k_nSteamNetworkingSend_Reliable
);
101 * update the joining user about current connections and our version
103 gameserver_send_version_to_client( index
);
105 netmsg_playerusername
*username
=
106 alloca( sizeof(netmsg_playerusername
) + NETWORK_USERNAME_MAX
);
107 username
->inetmsg_id
= k_inetmsg_playerusername
;
109 netmsg_playeritem
*item
=
110 alloca( sizeof(netmsg_playeritem
) + ADDON_UID_MAX
);
111 item
->inetmsg_id
= k_inetmsg_playeritem
;
113 netmsg_region
*region
= alloca( sizeof(netmsg_region
) + NETWORK_REGION_MAX
);
114 region
->inetmsg_id
= k_inetmsg_region
;
116 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ )
118 struct gameserver_client
*client
= &gameserver
.clients
[i
];
120 if( (i
== index
) || !client
->steamid
)
124 netmsg_playerjoin init
= { .inetmsg_id
= k_inetmsg_playerjoin
,
126 .steamid
= client
->steamid
};
127 gameserver_send_to_client( index
, &init
, sizeof(init
),
128 k_nSteamNetworkingSend_Reliable
);
132 u32 chs
= vg_strncpy( client
->username
, username
->name
,
133 NETWORK_USERNAME_MAX
,
134 k_strncpy_always_add_null
);
135 u32 size
= sizeof(netmsg_playerusername
) + chs
+ 1;
136 gameserver_send_to_client( index
, username
, size
,
137 k_nSteamNetworkingSend_Reliable
);
140 for( int j
=0; j
<k_netmsg_playeritem_max
; j
++ )
142 chs
= vg_strncpy( client
->items
[j
].uid
, item
->uid
, ADDON_UID_MAX
,
143 k_strncpy_always_add_null
);
144 item
->type_index
= j
;
146 size
= sizeof(netmsg_playeritem
) + chs
+ 1;
147 gameserver_send_to_client( index
, item
, size
,
148 k_nSteamNetworkingSend_Reliable
);
154 region
->flags
= client
->region_flags
;
155 u32 l
= vg_strncpy( client
->region
, region
->loc
, NETWORK_REGION_MAX
,
156 k_strncpy_always_add_null
);
157 size
= sizeof(netmsg_region
) + l
+ 1;
159 gameserver_send_to_client( index
, region
, size
,
160 k_nSteamNetworkingSend_Reliable
);
165 * Handle server update that player has left
167 static void gameserver_player_leave( int index
){
168 if( gameserver
.auth_mode
== eServerModeAuthentication
){
169 if( !gameserver
.clients
[ index
].steamid
)
173 netmsg_playerleave leave
;
174 leave
.inetmsg_id
= k_inetmsg_playerleave
;
177 vg_info( "Player leave (%d)\n", index
);
178 gameserver_send_to_all( index
, &leave
, sizeof(leave
),
179 k_nSteamNetworkingSend_Reliable
);
182 static void gameserver_update_all_knowledge( int client
, int clear
);
185 * Deletes client at index and disconnects the connection handle if it was
188 static void remove_client( int index
){
189 struct gameserver_client
*client
= &gameserver
.clients
[index
];
190 if( client
->connection
){
191 SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
192 hSteamNetworkingSockets
, client
->connection
, -1 );
193 SteamAPI_ISteamNetworkingSockets_CloseConnection(
194 hSteamNetworkingSockets
, client
->connection
,
195 k_ESteamNetConnectionEnd_Misc_InternalError
,
198 memset( client
, 0, sizeof(struct gameserver_client
) );
199 gameserver_update_all_knowledge( index
, 1 );
203 * Handle incoming new connection and init flags on the steam handle. if the
204 * server is full the userdata (client_id) will be set to -1 on the handle.
206 static void handle_new_connection( HSteamNetConnection conn
)
208 SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
209 hSteamNetworkingSockets
, conn
, -1 );
213 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ ){
214 if( !gameserver
.clients
[i
].active
){
221 vg_error( "Server full\n" );
222 SteamAPI_ISteamNetworkingSockets_CloseConnection(
223 hSteamNetworkingSockets
, conn
,
229 struct gameserver_client
*client
= &gameserver
.clients
[index
];
230 EResult accept_status
= SteamAPI_ISteamNetworkingSockets_AcceptConnection(
231 hSteamNetworkingSockets
, conn
);
233 if( accept_status
== k_EResultOK
)
235 vg_success( "Accepted client (id: %u, index: %d)\n", conn
, index
);
238 client
->connection
= conn
;
240 SteamAPI_ISteamNetworkingSockets_SetConnectionPollGroup(
241 hSteamNetworkingSockets
, conn
, gameserver
.client_group
);
243 SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
244 hSteamNetworkingSockets
, conn
, index
);
246 if( gameserver
.loopback_test
)
248 vg_warn( "[DEV] Creating loopback client\n" );
249 struct gameserver_client
*loopback
= &gameserver
.clients
[1];
250 loopback
->active
= 1;
251 loopback
->connection
= 0;
256 vg_warn( "Error accepting connection (id: %u)\n", conn
);
257 SteamAPI_ISteamNetworkingSockets_CloseConnection(
258 hSteamNetworkingSockets
, conn
,
259 k_ESteamNetConnectionEnd_Misc_InternalError
,
264 static void on_auth_status( CallbackMsg_t
*msg
){
265 SteamNetAuthenticationStatus_t
*info
= (void *)msg
->m_pubParam
;
266 vg_info( " Authentication availibility: %s\n",
267 string_ESteamNetworkingAvailability(info
->m_eAvail
) );
268 vg_info( " %s\n", info
->m_debugMsg
);
272 * Get client id of connection handle. Will be -1 if unkown to us either because
273 * the server is full or we already disconnected them
275 static i32
gameserver_conid( HSteamNetConnection hconn
)
281 if( gameserver
.loopback_test
)
287 id
= SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
288 hSteamNetworkingSockets
, hconn
);
290 if( (id
< 0) || (id
>= NETWORK_MAX_PLAYERS
) )
297 * Callback for steam connection state change
299 static void on_connect_status( CallbackMsg_t
*msg
)
301 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
302 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
304 vg_info( " %s -> %s\n",
305 string_ESteamNetworkingConnectionState(info
->m_eOldState
),
306 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
) );
308 if( info
->m_info
.m_eState
==k_ESteamNetworkingConnectionState_Connecting
)
310 handle_new_connection( info
->m_hConn
);
313 if( (info
->m_info
.m_eState
==
314 k_ESteamNetworkingConnectionState_ClosedByPeer
) ||
315 (info
->m_info
.m_eState
==
316 k_ESteamNetworkingConnectionState_ProblemDetectedLocally
) ||
317 (info
->m_info
.m_eState
==
318 k_ESteamNetworkingConnectionState_Dead
) ||
319 (info
->m_info
.m_eState
==
320 k_ESteamNetworkingConnectionState_None
) )
322 vg_info( "End reason: %d\n", info
->m_info
.m_eEndReason
);
324 int client_id
= gameserver_conid( info
->m_hConn
);
325 if( client_id
!= -1 )
327 gameserver_player_leave( client_id
);
328 remove_client( client_id
);
330 if( gameserver
.loopback_test
)
332 gameserver_player_leave( 1 );
338 SteamAPI_ISteamNetworkingSockets_CloseConnection(
339 hSteamNetworkingSockets
, info
->m_hConn
, 0, NULL
, 0 );
344 static void gameserver_rx_version( SteamNetworkingMessage_t
*msg
)
346 netmsg_version
*version
= msg
->m_pData
;
348 int client_id
= gameserver_conid( msg
->m_conn
);
349 if( client_id
== -1 )
351 vg_warn( "Recieved version from unkown connection (%u)\n", msg
->m_conn
);
352 SteamAPI_ISteamNetworkingSockets_CloseConnection(
353 hSteamNetworkingSockets
, msg
->m_conn
,
354 k_ESteamNetConnectionEnd_Misc_InternalError
,
359 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
361 if( client
->version
)
363 vg_warn( "Already have version for this client (%d conn: %u)",
364 client_id
, msg
->m_conn
);
368 client
->version
= version
->version
;
370 if( client
->version
!= NETWORK_SKATERIFT_VERSION
)
372 gameserver_send_version_to_client( client_id
);
373 remove_client( client_id
);
377 /* this is the sign on point for non-auth servers,
378 * for auth servers it comes at the end of rx_auth
380 if( gameserver
.auth_mode
!= eServerModeAuthentication
)
382 client
->steamid
= k_steamid_max
;
383 gameserver_player_join( client_id
);
385 if( gameserver
.loopback_test
)
387 struct gameserver_client
*loopback
= &gameserver
.clients
[1];
388 loopback
->steamid
= k_steamid_max
;
389 gameserver_player_join( 1 );
395 * recieve auth ticket from connection. will only accept it if we've added them
396 * to the client list first.
398 static void gameserver_rx_auth( SteamNetworkingMessage_t
*msg
){
399 if( gameserver
.auth_mode
!= eServerModeAuthentication
){
400 vg_warn( "Running server without authentication. "
401 "Connection %u tried to authenticate.\n", msg
->m_conn
);
405 int client_id
= gameserver_conid( msg
->m_conn
);
406 if( client_id
== -1 ) {
407 vg_warn( "Recieved auth ticket from unkown connection (%u)\n",
409 SteamAPI_ISteamNetworkingSockets_CloseConnection(
410 hSteamNetworkingSockets
, msg
->m_conn
,
411 k_ESteamNetConnectionEnd_Misc_InternalError
, NULL
, 1 );
415 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
416 if( client
->steamid
){
417 vg_warn( "Already authorized this user but another app ticket was sent"
418 " again (%d conn: %u)\n", client_id
, msg
->m_conn
);
422 if( client
->version
== 0 ){
423 vg_error( "Client has not sent their version yet (%u)\n", msg
->m_conn
);
424 remove_client( client_id
);
428 vg_low( "Attempting to verify user\n" );
430 if( msg
->m_cbSize
< sizeof(netmsg_auth
) ){
431 vg_error( "Malformed auth ticket, too small (%u)\n", msg
->m_conn
);
432 remove_client( client_id
);
436 netmsg_auth
*auth
= msg
->m_pData
;
438 if( msg
->m_cbSize
< sizeof(netmsg_auth
)+auth
->ticket_length
||
439 auth
->ticket_length
> 1024 ){
440 vg_error( "Malformed auth ticket, ticket_length incorrect (%u)\n",
441 auth
->ticket_length
);
442 remove_client( client_id
);
447 u32 ticket_len
= 1024;
449 int success
= SteamEncryptedAppTicket_BDecryptTicket(
450 auth
->ticket
, auth
->ticket_length
, decrypted
,
451 &ticket_len
, gameserver
.app_symmetric_key
,
452 k_nSteamEncryptedAppTicketSymmetricKeyLen
);
455 vg_error( "Failed to decrypt users ticket (client %u)\n", msg
->m_conn
);
456 vg_error( " ticket length: %u\n", auth
->ticket_length
);
457 remove_client( client_id
);
461 if( SteamEncryptedAppTicket_GetTicketIssueTime( decrypted
, ticket_len
)){
462 RTime32 ctime
= time(NULL
),
463 tickettime
= SteamEncryptedAppTicket_GetTicketIssueTime(
464 decrypted
, ticket_len
),
465 expiretime
= tickettime
+ 24*3*60*60;
467 if( ctime
> expiretime
){
468 vg_error( "Ticket expired (client %u)\n", msg
->m_conn
);
469 remove_client( client_id
);
475 SteamEncryptedAppTicket_GetTicketSteamID( decrypted
, ticket_len
, &steamid
);
476 vg_success( "User is authenticated! steamid %lu (%u)\n",
477 steamid
.m_unAll64Bits
, msg
->m_conn
);
479 client
->steamid
= steamid
.m_unAll64Bits
;
480 gameserver_player_join( client_id
);
484 * Player updates sent to us
485 * -----------------------------------------------------------------------------
488 static int packet_minsize( SteamNetworkingMessage_t
*msg
, u32 size
){
489 if( msg
->m_cbSize
< size
) {
490 vg_error( "Invalid packet size (must be at least %u)\n", size
);
498 struct db_set_username_thread_data
{
500 char username
[ NETWORK_USERNAME_MAX
];
503 static void gameserver_update_db_username( db_request
*db_req
){
504 struct db_set_username_thread_data
*inf
= (void *)db_req
->data
;
506 if( inf
->steamid
== k_steamid_max
)
510 if( inf
->steamid
== 76561198072130043 )
513 db_updateuser( inf
->steamid
, inf
->username
, admin
);
516 static int gameserver_item_eq( struct gameserver_item
*ia
,
517 struct gameserver_item
*ib
){
518 if( ia
->hash
== ib
->hash
)
519 if( !strcmp(ia
->uid
,ib
->uid
) )
526 * Match addons between two player IDs. if clear is set, then the flags between
527 * those two IDs will all be set to 0.
529 static void gameserver_update_knowledge_table( int client0
, int client1
,
531 u32 idx
= network_pair_index( client0
, client1
);
533 struct gameserver_client
*c0
= &gameserver
.clients
[client0
],
534 *c1
= &gameserver
.clients
[client1
];
539 if( gameserver_item_eq(&c0
->items
[k_netmsg_playeritem_world0
],
540 &c1
->items
[k_netmsg_playeritem_world0
]))
541 flags
|= CLIENT_KNOWLEDGE_SAME_WORLD0
;
543 if( gameserver_item_eq(&c0
->items
[k_netmsg_playeritem_world1
],
544 &c1
->items
[k_netmsg_playeritem_world1
]))
545 flags
|= CLIENT_KNOWLEDGE_SAME_WORLD1
;
548 gameserver
.client_knowledge_mask
[idx
] = flags
;
552 * If a change has been made on this client, then it will adjust the entire
553 * table of other players. if clear is set, all references to client will be set
556 static void gameserver_update_all_knowledge( int client
, int clear
){
557 for( int i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
561 struct gameserver_client
*ci
= &gameserver
.clients
[i
];
564 gameserver_update_knowledge_table( client
, i
, clear
);
568 static void gameserver_propogate_player_frame( int client_id
,
569 netmsg_playerframe
*frame
,
571 u32 basic_size
= sizeof(netmsg_playerframe
) + ((24*3)/8);
572 netmsg_playerframe
*full
= alloca(size
),
573 *basic
= alloca(basic_size
);
575 memcpy( full
, frame
, size
);
576 memcpy( basic
, frame
, basic_size
);
578 full
->client
= client_id
;
579 basic
->client
= client_id
;
580 basic
->subsystem
= 4; /* (.._basic_info: 24f*3 animator ) */
581 basic
->sound_effects
= 0;
583 struct gameserver_client
*c0
= &gameserver
.clients
[client_id
];
584 c0
->instance
= frame
->flags
& NETMSG_PLAYERFRAME_INSTANCE_ID
;
586 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ )
591 struct gameserver_client
*ci
= &gameserver
.clients
[i
];
595 if( c0
->instance
== ci
->instance
)
597 u32 k_index
= network_pair_index( client_id
, i
);
598 u8 k_mask
= gameserver
.client_knowledge_mask
[ k_index
];
600 if( (k_mask
& (CLIENT_KNOWLEDGE_SAME_WORLD0
<<c0
->instance
)) )
606 gameserver_send_to_client( i
, full
, size
,
607 k_nSteamNetworkingSend_Unreliable
);
611 gameserver_send_to_client( i
, basic
, basic_size
,
612 k_nSteamNetworkingSend_Unreliable
);
617 static void gameserver_rx_200_300( SteamNetworkingMessage_t
*msg
)
619 netmsg_blank
*tmp
= msg
->m_pData
;
621 int client_id
= gameserver_conid( msg
->m_conn
);
622 if( client_id
== -1 ) return;
624 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
626 if( tmp
->inetmsg_id
== k_inetmsg_playerusername
)
628 if( !packet_minsize( msg
, sizeof(netmsg_playerusername
)+1 ))
631 netmsg_playerusername
*src
= msg
->m_pData
;
633 u32 name_len
= network_msgstring( src
->name
, msg
->m_cbSize
,
634 sizeof(netmsg_playerusername
),
636 NETWORK_USERNAME_MAX
);
638 /* update other users about this change */
639 netmsg_playerusername
*prop
= alloca(sizeof(netmsg_playerusername
)+
640 NETWORK_USERNAME_MAX
);
642 prop
->inetmsg_id
= k_inetmsg_playerusername
;
643 prop
->index
= client_id
;
644 u32 chs
= vg_strncpy( client
->username
, prop
->name
, NETWORK_USERNAME_MAX
,
645 k_strncpy_always_add_null
);
647 vg_info( "client #%d changed name to: %s\n", client_id
, prop
->name
);
649 u32 propsize
= sizeof(netmsg_playerusername
) + chs
+ 1;
650 gameserver_send_to_all( client_id
, prop
, propsize
,
651 k_nSteamNetworkingSend_Reliable
);
653 /* update database about this */
654 db_request
*call
= db_alloc_request(
655 sizeof(struct db_set_username_thread_data
) );
656 struct db_set_username_thread_data
*inf
= (void *)call
->data
;
657 inf
->steamid
= client
->steamid
;
658 vg_strncpy( client
->username
, inf
->username
,
659 sizeof(inf
->username
), k_strncpy_always_add_null
);
660 call
->handler
= gameserver_update_db_username
;
661 db_send_request( call
);
663 else if( tmp
->inetmsg_id
== k_inetmsg_playerframe
)
665 gameserver_propogate_player_frame( client_id
,
666 msg
->m_pData
, msg
->m_cbSize
);
668 else if( tmp
->inetmsg_id
== k_inetmsg_playeritem
)
670 netmsg_playeritem
*item
= msg
->m_pData
;
673 if( item
->type_index
>= k_netmsg_playeritem_max
)
675 vg_warn( "Client #%d invalid equip type %u\n",
676 client_id
, (u32
)item
->type_index
);
680 char *dest
= client
->items
[ item
->type_index
].uid
;
682 network_msgstring( item
->uid
, msg
->m_cbSize
, sizeof(netmsg_playeritem
),
683 dest
, ADDON_UID_MAX
);
685 vg_info( "Client #%d equiped: [%s] %s\n",
687 (const char *[]){[k_netmsg_playeritem_board
]="board",
688 [k_netmsg_playeritem_player
]="player",
689 [k_netmsg_playeritem_world0
]="world0",
690 [k_netmsg_playeritem_world1
]="world1"
691 }[item
->type_index
], item
->uid
);
693 gameserver_update_all_knowledge( client_id
, 0 );
696 netmsg_playeritem
*prop
= alloca(msg
->m_cbSize
);
697 memcpy( prop
, msg
->m_pData
, msg
->m_cbSize
);
698 prop
->client
= client_id
;
699 gameserver_send_to_all( client_id
, prop
, msg
->m_cbSize
,
700 k_nSteamNetworkingSend_Reliable
);
702 else if( tmp
->inetmsg_id
== k_inetmsg_chat
)
704 netmsg_chat
*chat
= msg
->m_pData
,
705 *prop
= alloca( sizeof(netmsg_chat
) + NETWORK_MAX_CHAT
);
706 prop
->inetmsg_id
= k_inetmsg_chat
;
707 prop
->client
= client_id
;
709 u32 l
= network_msgstring( chat
->msg
, msg
->m_cbSize
, sizeof(netmsg_chat
),
710 prop
->msg
, NETWORK_MAX_CHAT
);
711 vg_info( "[%d]: %s\n", client_id
, prop
->msg
);
713 gameserver_send_to_all( client_id
, prop
, sizeof(netmsg_chat
)+l
+1,
714 k_nSteamNetworkingSend_Reliable
);
716 else if( tmp
->inetmsg_id
== k_inetmsg_region
)
718 netmsg_region
*region
= msg
->m_pData
,
719 *prop
= alloca( sizeof(netmsg_region
) + NETWORK_REGION_MAX
);
721 prop
->inetmsg_id
= k_inetmsg_region
;
722 prop
->client
= client_id
;
723 prop
->flags
= region
->flags
;
725 u32 l
= network_msgstring(
726 region
->loc
, msg
->m_cbSize
, sizeof(netmsg_region
),
727 client
->region
, NETWORK_REGION_MAX
);
728 client
->region_flags
= region
->flags
;
730 l
= vg_strncpy( client
->region
, prop
->loc
, NETWORK_REGION_MAX
,
731 k_strncpy_always_add_null
);
733 gameserver_send_to_all( client_id
, prop
, sizeof(netmsg_region
)+l
+1,
734 k_nSteamNetworkingSend_Reliable
);
735 vg_info( "client %d moved to region: %s\n", client_id
, client
->region
);
739 vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
744 static void gameserver_request_respond( enum request_status status
,
745 netmsg_request
*res
, vg_msg
*body
,
746 SteamNetworkingMessage_t
*msg
){
747 int client_id
= gameserver_conid( msg
->m_conn
);
751 vg_low( "[%d#%d] Response: %d\n", client_id
, (i32
)res
->id
, status
);
752 vg_msg_print( body
, len
);
755 res
->status
= status
;
757 if( gameserver
.loopback_test
&& !msg
->m_conn
)
759 release_message( msg
);
763 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
764 hSteamNetworkingSockets
, msg
->m_conn
,
765 res
, sizeof(netmsg_request
) + len
,
766 k_nSteamNetworkingSend_Reliable
, NULL
);
768 release_message( msg
);
771 struct user_request_thread_data
{
772 SteamNetworkingMessage_t
*msg
;
775 static u32
gameserver_get_current_week(void){
776 return time(NULL
) / (7*24*60*60);
779 static enum request_status
gameserver_cat_table(
781 const char *mod
, const char *route
, u32 week
, const char *alias
)
783 char table_name
[ DB_TABLE_UID_MAX
];
784 if( !db_get_highscore_table_name( mod
, route
, week
, table_name
) )
785 return k_request_status_out_of_memory
;
789 vg_strnull( &q
, buf
, 512 );
790 vg_strcat( &q
, "SELECT * FROM \"" );
791 vg_strcat( &q
, table_name
);
792 vg_strcat( &q
, "\" ORDER BY time ASC LIMIT 10;" );
793 if( !vg_strgood(&q
) )
794 return k_request_status_out_of_memory
;
796 sqlite3_stmt
*stmt
= db_stmt( q
.buffer
);
798 return k_request_status_database_error
;
800 vg_msg_frame( msg
, alias
);
801 for( u32 i
=0; i
<10; i
++ ){
802 int fc
= sqlite3_step( stmt
);
804 if( fc
== SQLITE_ROW
){
805 i32 time
= sqlite3_column_int( stmt
, 1 );
806 i64 steamid_i64
= sqlite3_column_int64( stmt
, 0 );
807 u64 steamid
= *((u64
*)&steamid_i64
);
809 if( steamid
== k_steamid_max
)
812 vg_msg_frame( msg
, "" );
813 vg_msg_wkvnum( msg
, "time", k_vg_msg_u32
, 1, &time
);
814 vg_msg_wkvnum( msg
, "steamid", k_vg_msg_u64
, 1, &steamid
);
817 if( db_getuserinfo( steamid
, username
, sizeof(username
), NULL
) )
818 vg_msg_wkvstr( msg
, "username", username
);
819 vg_msg_end_frame( msg
);
821 else if( fc
== SQLITE_DONE
){
830 sqlite3_finalize( stmt
);
831 vg_msg_end_frame( msg
);
832 return k_request_status_ok
;
835 static void gameserver_process_user_request( db_request
*db_req
)
837 struct user_request_thread_data
*inf
= (void *)db_req
->data
;
838 SteamNetworkingMessage_t
*msg
= inf
->msg
;
840 int client_id
= gameserver_conid( msg
->m_conn
);
841 if( client_id
== -1 )
843 release_message( msg
);
847 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
849 netmsg_request
*req
= (netmsg_request
*)msg
->m_pData
;
851 vg_msg_init( &data
, req
->q
, msg
->m_cbSize
- sizeof(netmsg_request
) );
853 /* create response packet */
854 netmsg_request
*res
= alloca( sizeof(netmsg_request
) + NETWORK_REQUEST_MAX
);
855 res
->inetmsg_id
= k_inetmsg_response
;
858 vg_msg_init( &body
, res
->q
, NETWORK_REQUEST_MAX
);
860 const char *endpoint
= vg_msg_getkvstr( &data
, "endpoint" );
863 gameserver_request_respond( k_request_status_invalid_endpoint
,
868 if( !strcmp( endpoint
, "scoreboard" ) ){
869 const char *mod
= vg_msg_getkvstr( &data
, "mod" );
870 const char *route
= vg_msg_getkvstr( &data
, "route" );
872 vg_msg_getkvintg( &data
, "week", k_vg_msg_u32
, &week
, NULL
);
874 if( week
== NETWORK_LEADERBOARD_CURRENT_WEEK
){
875 gameserver_cat_table( &body
, mod
, route
,
876 gameserver_get_current_week(), "rows_weekly" );
878 else if( week
== NETWORK_LEADERBOARD_ALLTIME_AND_CURRENT_WEEK
){
879 gameserver_cat_table( &body
, mod
, route
, 0, "rows" );
880 gameserver_cat_table( &body
, mod
, route
,
881 gameserver_get_current_week(), "rows_weekly" );
884 gameserver_cat_table( &body
, mod
, route
, week
, "rows" );
886 if( body
.error
!= k_vg_msg_error_OK
){
887 gameserver_request_respond( k_request_status_out_of_memory
,
892 gameserver_request_respond( k_request_status_ok
, res
, &body
, msg
);
894 else if( !strcmp( endpoint
, "setlap" ) ){
895 if( client
->steamid
== k_steamid_max
){
896 gameserver_request_respond( k_request_status_unauthorized
,
901 const char *mod
= vg_msg_getkvstr( &data
, "mod" );
902 const char *route
= vg_msg_getkvstr( &data
, "route" );
904 char weekly_table
[ DB_TABLE_UID_MAX
],
905 alltime_table
[ DB_TABLE_UID_MAX
];
907 u32 week
= gameserver_get_current_week();
909 if( !db_get_highscore_table_name( mod
, route
, 0, alltime_table
) ||
910 !db_get_highscore_table_name( mod
, route
, week
, weekly_table
) ){
911 gameserver_request_respond( k_request_status_out_of_memory
,
917 vg_msg_getkvintg( &data
, "time", k_vg_msg_i32
, ¢iseconds
, NULL
);
918 if( centiseconds
< 5*100 ){
919 gameserver_request_respond( k_request_status_client_error
,
924 db_writeusertime( alltime_table
, client
->steamid
, centiseconds
, 1 );
925 db_writeusertime( weekly_table
, client
->steamid
, centiseconds
, 1 );
926 gameserver_request_respond( k_request_status_ok
, res
, NULL
, msg
);
929 gameserver_request_respond( k_request_status_invalid_endpoint
,
934 static void gameserver_rx_300_400( SteamNetworkingMessage_t
*msg
)
936 netmsg_blank
*tmp
= msg
->m_pData
;
938 int client_id
= gameserver_conid( msg
->m_conn
);
939 if( client_id
== -1 )
941 release_message( msg
);
945 if( tmp
->inetmsg_id
== k_inetmsg_request
)
947 if( gameserver
.loopback_test
&& (client_id
== 1) )
949 release_message( msg
);
953 if( !packet_minsize( msg
, sizeof(netmsg_request
)+1 ))
955 release_message( msg
);
959 db_request
*call
= db_alloc_request(
960 sizeof(struct user_request_thread_data
) );
961 struct user_request_thread_data
*inf
= (void *)call
->data
;
963 call
->handler
= gameserver_process_user_request
;
964 db_send_request( call
);
968 vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
970 release_message( msg
);
974 static void process_network_message( SteamNetworkingMessage_t
*msg
)
976 if( msg
->m_cbSize
< sizeof(netmsg_blank
) ){
977 vg_warn( "Discarding message (too small: %d)\n",
982 netmsg_blank
*tmp
= msg
->m_pData
;
984 if( (tmp
->inetmsg_id
>= 200) && (tmp
->inetmsg_id
< 300) )
986 gameserver_rx_200_300( msg
);
987 release_message( msg
);
989 else if( (tmp
->inetmsg_id
>= 300) && (tmp
->inetmsg_id
< 400) )
991 gameserver_rx_300_400( msg
);
994 if( tmp
->inetmsg_id
== k_inetmsg_auth
)
995 gameserver_rx_auth( msg
);
996 else if( tmp
->inetmsg_id
== k_inetmsg_version
){
997 gameserver_rx_version( msg
);
1000 vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
1003 release_message( msg
);
1007 static void poll_connections(void)
1009 SteamNetworkingMessage_t
*messages
[32];
1014 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
1015 hSteamNetworkingSockets
,
1016 gameserver
.client_group
, messages
, vg_list_size(messages
) );
1021 for( int i
=0; i
<len
; i
++ )
1023 SteamNetworkingMessage_t
*msg
= messages
[i
];
1024 msg
->m_nUserData
= 1;
1026 if( gameserver
.loopback_test
)
1028 HSteamNetConnection conid
= msg
->m_conn
;
1030 msg
->m_nUserData
++;
1031 process_network_message( msg
);
1032 msg
->m_conn
= conid
;
1035 process_network_message( msg
);
1040 static u64
seconds_to_server_ticks( double s
){
1044 int main( int argc
, char *argv
[] ){
1045 signal( SIGINT
, inthandler
);
1046 signal( SIGQUIT
, inthandler
);
1047 signal( SIGPIPE
, SIG_IGN
);
1050 while( vg_argp( argc
, argv
) )
1052 if( vg_long_opt( "noauth" ) )
1053 gameserver
.auth_mode
= eServerModeNoAuthentication
;
1055 if( vg_long_opt( "loopback" ) )
1056 gameserver
.loopback_test
= 1;
1059 vg_set_mem_quota( 80*1024*1024 );
1064 * --------------------------------------------------------------- */
1065 steamworks_ensure_txt( "2103940" );
1066 if( gameserver
.auth_mode
== eServerModeAuthentication
){
1067 if( !vg_load_steam_symetric_key( "application_key",
1068 gameserver
.app_symmetric_key
)){
1073 vg_warn( "Running without user authentication.\n" );
1076 if( !SteamGameServer_Init( 0, NETWORK_PORT
, NETWORK_PORT
+1,
1077 gameserver
.auth_mode
, "1.0.0.0" ) ){
1078 vg_error( "SteamGameServer_Init failed\n" );
1082 void *hSteamGameServer
= SteamAPI_SteamGameServer();
1083 SteamAPI_ISteamGameServer_LogOnAnonymous( hSteamGameServer
);
1085 SteamAPI_ManualDispatch_Init();
1086 HSteamPipe hsteampipe
= SteamGameServer_GetHSteamPipe();
1087 hSteamNetworkingSockets
=
1088 SteamAPI_SteamGameServerNetworkingSockets_SteamAPI();
1090 steam_register_callback( k_iSteamNetAuthenticationStatus
, on_auth_status
);
1091 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack
,
1092 on_connect_status
);
1094 vg_success( "Steamworks API running\n" );
1095 steamworks_event_loop( hsteampipe
);
1100 HSteamListenSocket listener
;
1101 SteamNetworkingIPAddr localAddr
;
1102 SteamAPI_SteamNetworkingIPAddr_Clear( &localAddr
);
1103 localAddr
.m_port
= NETWORK_PORT
;
1105 listener
= SteamAPI_ISteamNetworkingSockets_CreateListenSocketIP(
1106 hSteamNetworkingSockets
, &localAddr
, 0, NULL
);
1107 gameserver
.client_group
= SteamAPI_ISteamNetworkingSockets_CreatePollGroup(
1108 hSteamNetworkingSockets
);
1110 u64 server_ticks
= 8000,
1111 last_record_save
= 8000,
1112 last_scoreboard_gen
= 0;
1115 steamworks_event_loop( hsteampipe
);
1125 SteamAPI_ISteamNetworkingSockets_DestroyPollGroup( hSteamNetworkingSockets
,
1126 gameserver
.client_group
);
1127 SteamAPI_ISteamNetworkingSockets_CloseListenSocket(
1128 hSteamNetworkingSockets
, listener
);
1130 vg_info( "Shutting down\n..." );
1131 SteamGameServer_Shutdown();