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
) {
27 * Send message to single client, with authentication checking
29 static void gameserver_send_to_client( i32 client_id
,
30 const void *pData
, u32 cbData
,
32 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
34 if( !client
->steamid
)
37 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
38 hSteamNetworkingSockets
, client
->connection
,
39 pData
, cbData
, nSendFlags
, NULL
);
43 * Send message to all clients if they are authenticated
45 static void gameserver_send_to_all( int ignore
,
46 const void *pData
, u32 cbData
,
48 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ ){
49 struct gameserver_client
*client
= &gameserver
.clients
[i
];
52 gameserver_send_to_client( i
, pData
, cbData
, nSendFlags
);
56 static void gameserver_send_version_to_client( int index
){
57 struct gameserver_client
*client
= &gameserver
.clients
[index
];
59 netmsg_version version
;
60 version
.inetmsg_id
= k_inetmsg_version
;
61 version
.version
= NETWORK_SKATERIFT_VERSION
;
62 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
63 hSteamNetworkingSockets
, client
->connection
,
64 &version
, sizeof(netmsg_version
),
65 k_nSteamNetworkingSend_Reliable
, NULL
);
69 * handle server update that client #'index' has joined
71 static void gameserver_player_join( int index
){
72 struct gameserver_client
*joiner
= &gameserver
.clients
[index
];
74 netmsg_playerjoin join
= { .inetmsg_id
= k_inetmsg_playerjoin
,
76 .steamid
= joiner
->steamid
};
78 gameserver_send_to_all( index
, &join
, sizeof(join
),
79 k_nSteamNetworkingSend_Reliable
);
82 * update the joining user about current connections and our version
84 gameserver_send_version_to_client( index
);
86 netmsg_playerusername
*username
=
87 alloca( sizeof(netmsg_playerusername
) + NETWORK_USERNAME_MAX
);
88 username
->inetmsg_id
= k_inetmsg_playerusername
;
90 netmsg_playeritem
*item
=
91 alloca( sizeof(netmsg_playeritem
) + ADDON_UID_MAX
);
92 item
->inetmsg_id
= k_inetmsg_playeritem
;
94 netmsg_region
*region
= alloca( sizeof(netmsg_region
) + NETWORK_REGION_MAX
);
95 region
->inetmsg_id
= k_inetmsg_region
;
97 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ ){
98 struct gameserver_client
*client
= &gameserver
.clients
[i
];
100 if( (i
== index
) || !client
->steamid
)
104 netmsg_playerjoin init
= { .inetmsg_id
= k_inetmsg_playerjoin
,
106 .steamid
= client
->steamid
};
107 gameserver_send_to_client( index
, &init
, sizeof(init
),
108 k_nSteamNetworkingSend_Reliable
);
112 u32 chs
= vg_strncpy( client
->username
, username
->name
,
113 NETWORK_USERNAME_MAX
,
114 k_strncpy_always_add_null
);
115 u32 size
= sizeof(netmsg_playerusername
) + chs
+ 1;
116 gameserver_send_to_client( index
, username
, size
,
117 k_nSteamNetworkingSend_Reliable
);
120 for( int j
=0; j
<k_netmsg_playeritem_max
; j
++ ){
121 chs
= vg_strncpy( client
->items
[j
].uid
, item
->uid
, ADDON_UID_MAX
,
122 k_strncpy_always_add_null
);
123 item
->type_index
= j
;
125 size
= sizeof(netmsg_playeritem
) + chs
+ 1;
126 gameserver_send_to_client( index
, item
, size
,
127 k_nSteamNetworkingSend_Reliable
);
133 region
->flags
= client
->region_flags
;
134 u32 l
= vg_strncpy( client
->region
, region
->loc
, NETWORK_REGION_MAX
,
135 k_strncpy_always_add_null
);
136 size
= sizeof(netmsg_region
) + l
+ 1;
138 gameserver_send_to_client( index
, region
, size
,
139 k_nSteamNetworkingSend_Reliable
);
144 * Handle server update that player has left
146 static void gameserver_player_leave( int index
){
147 if( gameserver
.auth_mode
== eServerModeAuthentication
){
148 if( !gameserver
.clients
[ index
].steamid
)
152 netmsg_playerleave leave
;
153 leave
.inetmsg_id
= k_inetmsg_playerleave
;
156 vg_info( "Player leave (%d)\n", index
);
157 gameserver_send_to_all( index
, &leave
, sizeof(leave
),
158 k_nSteamNetworkingSend_Reliable
);
161 static void gameserver_update_all_knowledge( int client
, int clear
);
164 * Deletes client at index and disconnects the connection handle if it was
167 static void remove_client( int index
){
168 struct gameserver_client
*client
= &gameserver
.clients
[index
];
169 if( client
->connection
){
170 SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
171 hSteamNetworkingSockets
, client
->connection
, -1 );
172 SteamAPI_ISteamNetworkingSockets_CloseConnection(
173 hSteamNetworkingSockets
, client
->connection
,
174 k_ESteamNetConnectionEnd_Misc_InternalError
,
177 memset( client
, 0, sizeof(struct gameserver_client
) );
178 gameserver_update_all_knowledge( index
, 1 );
182 * Handle incoming new connection and init flags on the steam handle. if the
183 * server is full the userdata (client_id) will be set to -1 on the handle.
185 static void handle_new_connection( HSteamNetConnection conn
){
186 SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
187 hSteamNetworkingSockets
, conn
, -1 );
191 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ ){
192 if( !gameserver
.clients
[i
].active
){
199 vg_error( "Server full\n" );
200 SteamAPI_ISteamNetworkingSockets_CloseConnection(
201 hSteamNetworkingSockets
, conn
,
207 struct gameserver_client
*client
= &gameserver
.clients
[index
];
208 EResult accept_status
= SteamAPI_ISteamNetworkingSockets_AcceptConnection(
209 hSteamNetworkingSockets
, conn
);
211 if( accept_status
== k_EResultOK
){
212 vg_success( "Accepted client (id: %u, index: %d)\n", conn
, index
);
215 client
->connection
= conn
;
217 SteamAPI_ISteamNetworkingSockets_SetConnectionPollGroup(
218 hSteamNetworkingSockets
, conn
, gameserver
.client_group
);
220 SteamAPI_ISteamNetworkingSockets_SetConnectionUserData(
221 hSteamNetworkingSockets
, conn
, index
);
224 vg_warn( "Error accepting connection (id: %u)\n", conn
);
225 SteamAPI_ISteamNetworkingSockets_CloseConnection(
226 hSteamNetworkingSockets
, conn
,
227 k_ESteamNetConnectionEnd_Misc_InternalError
,
232 static void on_auth_status( CallbackMsg_t
*msg
){
233 SteamNetAuthenticationStatus_t
*info
= (void *)msg
->m_pubParam
;
234 vg_info( " Authentication availibility: %s\n",
235 string_ESteamNetworkingAvailability(info
->m_eAvail
) );
236 vg_info( " %s\n", info
->m_debugMsg
);
240 * Get client id of connection handle. Will be -1 if unkown to us either because
241 * the server is full or we already disconnected them
243 static i32
gameserver_conid( HSteamNetConnection hconn
){
244 i64 id
= SteamAPI_ISteamNetworkingSockets_GetConnectionUserData(
245 hSteamNetworkingSockets
, hconn
);
247 if( (id
< 0) || (id
>= NETWORK_MAX_PLAYERS
) )
254 * Callback for steam connection state change
256 static void on_connect_status( CallbackMsg_t
*msg
){
257 SteamNetConnectionStatusChangedCallback_t
*info
= (void *)msg
->m_pubParam
;
258 vg_info( " Connection status changed for %lu\n", info
->m_hConn
);
260 vg_info( " %s -> %s\n",
261 string_ESteamNetworkingConnectionState(info
->m_eOldState
),
262 string_ESteamNetworkingConnectionState(info
->m_info
.m_eState
) );
264 if( info
->m_info
.m_eState
==k_ESteamNetworkingConnectionState_Connecting
){
265 handle_new_connection( info
->m_hConn
);
268 if( (info
->m_info
.m_eState
==
269 k_ESteamNetworkingConnectionState_ClosedByPeer
) ||
270 (info
->m_info
.m_eState
==
271 k_ESteamNetworkingConnectionState_ProblemDetectedLocally
) ||
272 (info
->m_info
.m_eState
==
273 k_ESteamNetworkingConnectionState_Dead
) ||
274 (info
->m_info
.m_eState
==
275 k_ESteamNetworkingConnectionState_None
) )
277 vg_info( "End reason: %d\n", info
->m_info
.m_eEndReason
);
279 int client_id
= gameserver_conid( info
->m_hConn
);
280 if( client_id
!= -1 ){
281 gameserver_player_leave( client_id
);
282 remove_client( client_id
);
285 SteamAPI_ISteamNetworkingSockets_CloseConnection(
286 hSteamNetworkingSockets
, info
->m_hConn
, 0, NULL
, 0 );
291 static void gameserver_rx_version( SteamNetworkingMessage_t
*msg
){
292 netmsg_version
*version
= msg
->m_pData
;
294 int client_id
= gameserver_conid( msg
->m_conn
);
295 if( client_id
== -1 ) {
296 vg_warn( "Recieved version from unkown connection (%u)\n", msg
->m_conn
);
297 SteamAPI_ISteamNetworkingSockets_CloseConnection(
298 hSteamNetworkingSockets
, msg
->m_conn
,
299 k_ESteamNetConnectionEnd_Misc_InternalError
,
304 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
306 if( client
->version
){
307 vg_warn( "Already have version for this client (%d conn: %u)",
308 client_id
, msg
->m_conn
);
312 client
->version
= version
->version
;
314 if( client
->version
!= NETWORK_SKATERIFT_VERSION
){
315 gameserver_send_version_to_client( client_id
);
316 remove_client( client_id
);
320 /* this is the sign on point for non-auth servers,
321 * for auth servers it comes at the end of rx_auth
323 if( gameserver
.auth_mode
!= eServerModeAuthentication
){
324 client
->steamid
= k_steamid_max
;
325 gameserver_player_join( client_id
);
330 * recieve auth ticket from connection. will only accept it if we've added them
331 * to the client list first.
333 static void gameserver_rx_auth( SteamNetworkingMessage_t
*msg
){
334 if( gameserver
.auth_mode
!= eServerModeAuthentication
){
335 vg_warn( "Running server without authentication. "
336 "Connection %u tried to authenticate.\n", msg
->m_conn
);
340 int client_id
= gameserver_conid( msg
->m_conn
);
341 if( client_id
== -1 ) {
342 vg_warn( "Recieved auth ticket from unkown connection (%u)\n",
344 SteamAPI_ISteamNetworkingSockets_CloseConnection(
345 hSteamNetworkingSockets
, msg
->m_conn
,
346 k_ESteamNetConnectionEnd_Misc_InternalError
, NULL
, 1 );
350 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
351 if( client
->steamid
){
352 vg_warn( "Already authorized this user but another app ticket was sent"
353 " again (%d conn: %u)\n", client_id
, msg
->m_conn
);
357 if( client
->version
== 0 ){
358 vg_error( "Client has not sent their version yet (%u)\n", msg
->m_conn
);
359 remove_client( client_id
);
363 vg_low( "Attempting to verify user\n" );
365 if( msg
->m_cbSize
< sizeof(netmsg_auth
) ){
366 vg_error( "Malformed auth ticket, too small (%u)\n", msg
->m_conn
);
367 remove_client( client_id
);
371 netmsg_auth
*auth
= msg
->m_pData
;
373 if( msg
->m_cbSize
< sizeof(netmsg_auth
)+auth
->ticket_length
||
374 auth
->ticket_length
> 1024 ){
375 vg_error( "Malformed auth ticket, ticket_length incorrect (%u)\n",
376 auth
->ticket_length
);
377 remove_client( client_id
);
382 u32 ticket_len
= 1024;
384 int success
= SteamEncryptedAppTicket_BDecryptTicket(
385 auth
->ticket
, auth
->ticket_length
, decrypted
,
386 &ticket_len
, gameserver
.app_symmetric_key
,
387 k_nSteamEncryptedAppTicketSymmetricKeyLen
);
390 vg_error( "Failed to decrypt users ticket (client %u)\n", msg
->m_conn
);
391 vg_error( " ticket length: %u\n", auth
->ticket_length
);
392 remove_client( client_id
);
396 if( SteamEncryptedAppTicket_GetTicketIssueTime( decrypted
, ticket_len
)){
397 RTime32 ctime
= time(NULL
),
398 tickettime
= SteamEncryptedAppTicket_GetTicketIssueTime(
399 decrypted
, ticket_len
),
400 expiretime
= tickettime
+ 24*3*60*60;
402 if( ctime
> expiretime
){
403 vg_error( "Ticket expired (client %u)\n", msg
->m_conn
);
404 remove_client( client_id
);
410 SteamEncryptedAppTicket_GetTicketSteamID( decrypted
, ticket_len
, &steamid
);
411 vg_success( "User is authenticated! steamid %lu (%u)\n",
412 steamid
.m_unAll64Bits
, msg
->m_conn
);
414 client
->steamid
= steamid
.m_unAll64Bits
;
415 gameserver_player_join( client_id
);
419 * Player updates sent to us
420 * -----------------------------------------------------------------------------
423 static int packet_minsize( SteamNetworkingMessage_t
*msg
, u32 size
){
424 if( msg
->m_cbSize
< size
) {
425 vg_error( "Invalid packet size (must be at least %u)\n", size
);
433 struct db_set_username_thread_data
{
435 char username
[ NETWORK_USERNAME_MAX
];
438 static void gameserver_update_db_username( db_request
*db_req
){
439 struct db_set_username_thread_data
*inf
= (void *)db_req
->data
;
441 if( inf
->steamid
== k_steamid_max
)
445 if( inf
->steamid
== 76561198072130043 )
448 db_updateuser( inf
->steamid
, inf
->username
, admin
);
451 static int gameserver_item_eq( struct gameserver_item
*ia
,
452 struct gameserver_item
*ib
){
453 if( ia
->hash
== ib
->hash
)
454 if( !strcmp(ia
->uid
,ib
->uid
) )
461 * Match addons between two player IDs. if clear is set, then the flags between
462 * those two IDs will all be set to 0.
464 static void gameserver_update_knowledge_table( int client0
, int client1
,
466 u32 idx
= network_pair_index( client0
, client1
);
468 struct gameserver_client
*c0
= &gameserver
.clients
[client0
],
469 *c1
= &gameserver
.clients
[client1
];
474 if( gameserver_item_eq(&c0
->items
[k_netmsg_playeritem_world0
],
475 &c1
->items
[k_netmsg_playeritem_world0
]))
476 flags
|= CLIENT_KNOWLEDGE_SAME_WORLD0
;
478 if( gameserver_item_eq(&c0
->items
[k_netmsg_playeritem_world1
],
479 &c1
->items
[k_netmsg_playeritem_world1
]))
480 flags
|= CLIENT_KNOWLEDGE_SAME_WORLD1
;
483 gameserver
.client_knowledge_mask
[idx
] = flags
;
487 * If a change has been made on this client, then it will adjust the entire
488 * table of other players. if clear is set, all references to client will be set
491 static void gameserver_update_all_knowledge( int client
, int clear
){
492 for( int i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
496 struct gameserver_client
*ci
= &gameserver
.clients
[i
];
499 gameserver_update_knowledge_table( client
, i
, clear
);
503 static void gameserver_propogate_player_frame( int client_id
,
504 netmsg_playerframe
*frame
,
506 u32 basic_size
= sizeof(netmsg_playerframe
) + ((24*3)/8);
507 netmsg_playerframe
*full
= alloca(size
),
508 *basic
= alloca(basic_size
);
510 memcpy( full
, frame
, size
);
511 memcpy( basic
, frame
, basic_size
);
513 full
->client
= client_id
;
514 basic
->client
= client_id
;
515 basic
->subsystem
= 4; /* (.._basic_info: 24f*3 animator ) */
516 basic
->sound_effects
= 0;
518 struct gameserver_client
*c0
= &gameserver
.clients
[client_id
];
519 c0
->instance
= frame
->flags
& NETMSG_PLAYERFRAME_INSTANCE_ID
;
521 for( int i
=0; i
<vg_list_size(gameserver
.clients
); i
++ ){
525 struct gameserver_client
*ci
= &gameserver
.clients
[i
];
529 if( c0
->instance
== ci
->instance
){
530 u32 k_index
= network_pair_index( client_id
, i
);
531 u8 k_mask
= gameserver
.client_knowledge_mask
[ k_index
];
533 if( (k_mask
& (CLIENT_KNOWLEDGE_SAME_WORLD0
<<c0
->instance
)) )
538 gameserver_send_to_client( i
, full
, size
,
539 k_nSteamNetworkingSend_Unreliable
);
542 gameserver_send_to_client( i
, basic
, basic_size
,
543 k_nSteamNetworkingSend_Unreliable
);
548 static void gameserver_rx_200_300( SteamNetworkingMessage_t
*msg
){
549 netmsg_blank
*tmp
= msg
->m_pData
;
551 int client_id
= gameserver_conid( msg
->m_conn
);
552 if( client_id
== -1 ) return;
554 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
556 if( tmp
->inetmsg_id
== k_inetmsg_playerusername
){
557 if( !packet_minsize( msg
, sizeof(netmsg_playerusername
)+1 ))
560 netmsg_playerusername
*src
= msg
->m_pData
;
562 u32 name_len
= network_msgstring( src
->name
, msg
->m_cbSize
,
563 sizeof(netmsg_playerusername
),
565 NETWORK_USERNAME_MAX
);
567 /* update other users about this change */
568 netmsg_playerusername
*prop
= alloca(sizeof(netmsg_playerusername
)+
569 NETWORK_USERNAME_MAX
);
571 prop
->inetmsg_id
= k_inetmsg_playerusername
;
572 prop
->index
= client_id
;
573 u32 chs
= vg_strncpy( client
->username
, prop
->name
, NETWORK_USERNAME_MAX
,
574 k_strncpy_always_add_null
);
576 vg_info( "client #%d changed name to: %s\n", client_id
, prop
->name
);
578 u32 propsize
= sizeof(netmsg_playerusername
) + chs
+ 1;
579 gameserver_send_to_all( client_id
, prop
, propsize
,
580 k_nSteamNetworkingSend_Reliable
);
582 /* update database about this */
583 db_request
*call
= db_alloc_request(
584 sizeof(struct db_set_username_thread_data
) );
585 struct db_set_username_thread_data
*inf
= (void *)call
->data
;
586 inf
->steamid
= client
->steamid
;
587 vg_strncpy( client
->username
, inf
->username
,
588 sizeof(inf
->username
), k_strncpy_always_add_null
);
589 call
->handler
= gameserver_update_db_username
;
590 db_send_request( call
);
592 else if( tmp
->inetmsg_id
== k_inetmsg_playerframe
){
593 gameserver_propogate_player_frame( client_id
,
594 msg
->m_pData
, msg
->m_cbSize
);
596 else if( tmp
->inetmsg_id
== k_inetmsg_playeritem
){
597 netmsg_playeritem
*item
= msg
->m_pData
;
601 if( item
->type_index
>= k_netmsg_playeritem_max
){
602 vg_warn( "Client #%d invalid equip type %u\n",
603 client_id
, (u32
)item
->type_index
);
607 char *dest
= client
->items
[ item
->type_index
].uid
;
609 network_msgstring( item
->uid
, msg
->m_cbSize
, sizeof(netmsg_playeritem
),
610 dest
, ADDON_UID_MAX
);
612 vg_info( "Client #%d equiped: [%s] %s\n",
614 (const char *[]){[k_netmsg_playeritem_board
]="board",
615 [k_netmsg_playeritem_player
]="player",
616 [k_netmsg_playeritem_world0
]="world0",
617 [k_netmsg_playeritem_world1
]="world1"
618 }[item
->type_index
], item
->uid
);
620 gameserver_update_all_knowledge( client_id
, 0 );
623 netmsg_playeritem
*prop
= alloca(msg
->m_cbSize
);
624 memcpy( prop
, msg
->m_pData
, msg
->m_cbSize
);
625 prop
->client
= client_id
;
626 gameserver_send_to_all( client_id
, prop
, msg
->m_cbSize
,
627 k_nSteamNetworkingSend_Reliable
);
629 else if( tmp
->inetmsg_id
== k_inetmsg_chat
){
630 netmsg_chat
*chat
= msg
->m_pData
,
631 *prop
= alloca( sizeof(netmsg_chat
) + NETWORK_MAX_CHAT
);
632 prop
->inetmsg_id
= k_inetmsg_chat
;
633 prop
->client
= client_id
;
635 u32 l
= network_msgstring( chat
->msg
, msg
->m_cbSize
, sizeof(netmsg_chat
),
636 prop
->msg
, NETWORK_MAX_CHAT
);
637 vg_info( "[%d]: %s\n", client_id
, prop
->msg
);
639 gameserver_send_to_all( client_id
, prop
, sizeof(netmsg_chat
)+l
+1,
640 k_nSteamNetworkingSend_Reliable
);
642 else if( tmp
->inetmsg_id
== k_inetmsg_region
){
643 netmsg_region
*region
= msg
->m_pData
,
644 *prop
= alloca( sizeof(netmsg_region
) + NETWORK_REGION_MAX
);
646 prop
->inetmsg_id
= k_inetmsg_region
;
647 prop
->client
= client_id
;
648 prop
->flags
= region
->flags
;
650 u32 l
= network_msgstring(
651 region
->loc
, msg
->m_cbSize
, sizeof(netmsg_region
),
652 client
->region
, NETWORK_REGION_MAX
);
653 client
->region_flags
= region
->flags
;
655 l
= vg_strncpy( client
->region
, prop
->loc
, NETWORK_REGION_MAX
,
656 k_strncpy_always_add_null
);
658 gameserver_send_to_all( client_id
, prop
, sizeof(netmsg_region
)+l
+1,
659 k_nSteamNetworkingSend_Reliable
);
660 vg_info( "client %d moved to region: %s\n", client_id
, client
->region
);
663 vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
668 static void gameserver_request_respond( enum request_status status
,
669 netmsg_request
*res
, vg_msg
*body
,
670 SteamNetworkingMessage_t
*msg
){
671 int client_id
= gameserver_conid( msg
->m_conn
);
675 vg_low( "[%d#%d] Response: %d\n", client_id
, (i32
)res
->id
, status
);
676 vg_msg_print( body
, len
);
679 res
->status
= status
;
681 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
682 hSteamNetworkingSockets
, msg
->m_conn
,
683 res
, sizeof(netmsg_request
) + len
,
684 k_nSteamNetworkingSend_Reliable
, NULL
);
685 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
688 struct user_request_thread_data
{
689 SteamNetworkingMessage_t
*msg
;
692 static u32
gameserver_get_current_week(void){
693 return time(NULL
) / (7*24*60*60);
696 static enum request_status
gameserver_cat_table(
698 const char *mod
, const char *route
, u32 week
, const char *alias
)
700 char table_name
[ DB_TABLE_UID_MAX
];
701 if( !db_get_highscore_table_name( mod
, route
, week
, table_name
) )
702 return k_request_status_out_of_memory
;
706 vg_strnull( &q
, buf
, 512 );
707 vg_strcat( &q
, "SELECT * FROM \"" );
708 vg_strcat( &q
, table_name
);
709 vg_strcat( &q
, "\" ORDER BY time ASC LIMIT 10;" );
710 if( !vg_strgood(&q
) )
711 return k_request_status_out_of_memory
;
713 sqlite3_stmt
*stmt
= db_stmt( q
.buffer
);
715 return k_request_status_database_error
;
717 vg_msg_frame( msg
, alias
);
718 for( u32 i
=0; i
<10; i
++ ){
719 int fc
= sqlite3_step( stmt
);
721 if( fc
== SQLITE_ROW
){
722 i32 time
= sqlite3_column_int( stmt
, 1 );
723 i64 steamid_i64
= sqlite3_column_int64( stmt
, 0 );
724 u64 steamid
= *((u64
*)&steamid_i64
);
726 if( steamid
== k_steamid_max
)
729 vg_msg_frame( msg
, "" );
730 vg_msg_wkvnum( msg
, "time", k_vg_msg_u32
, 1, &time
);
731 vg_msg_wkvnum( msg
, "steamid", k_vg_msg_u64
, 1, &steamid
);
734 if( db_getuserinfo( steamid
, username
, sizeof(username
), NULL
) )
735 vg_msg_wkvstr( msg
, "username", username
);
736 vg_msg_end_frame( msg
);
738 else if( fc
== SQLITE_DONE
){
747 sqlite3_finalize( stmt
);
748 vg_msg_end_frame( msg
);
749 return k_request_status_ok
;
752 static void gameserver_process_user_request( db_request
*db_req
){
753 struct user_request_thread_data
*inf
= (void *)db_req
->data
;
754 SteamNetworkingMessage_t
*msg
= inf
->msg
;
756 int client_id
= gameserver_conid( msg
->m_conn
);
757 if( client_id
== -1 ){
758 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
762 struct gameserver_client
*client
= &gameserver
.clients
[ client_id
];
764 netmsg_request
*req
= (netmsg_request
*)msg
->m_pData
;
766 vg_msg_init( &data
, req
->q
, msg
->m_cbSize
- sizeof(netmsg_request
) );
768 /* create response packet */
769 netmsg_request
*res
= alloca( sizeof(netmsg_request
) + NETWORK_REQUEST_MAX
);
770 res
->inetmsg_id
= k_inetmsg_response
;
773 vg_msg_init( &body
, res
->q
, NETWORK_REQUEST_MAX
);
775 const char *endpoint
= vg_msg_getkvstr( &data
, "endpoint" );
778 gameserver_request_respond( k_request_status_invalid_endpoint
,
783 if( !strcmp( endpoint
, "scoreboard" ) ){
784 const char *mod
= vg_msg_getkvstr( &data
, "mod" );
785 const char *route
= vg_msg_getkvstr( &data
, "route" );
787 vg_msg_getkvintg( &data
, "week", k_vg_msg_u32
, &week
, NULL
);
789 if( week
== NETWORK_LEADERBOARD_CURRENT_WEEK
){
790 gameserver_cat_table( &body
, mod
, route
,
791 gameserver_get_current_week(), "rows_weekly" );
793 else if( week
== NETWORK_LEADERBOARD_ALLTIME_AND_CURRENT_WEEK
){
794 gameserver_cat_table( &body
, mod
, route
, 0, "rows" );
795 gameserver_cat_table( &body
, mod
, route
,
796 gameserver_get_current_week(), "rows_weekly" );
799 gameserver_cat_table( &body
, mod
, route
, week
, "rows" );
801 if( body
.error
!= k_vg_msg_error_OK
){
802 gameserver_request_respond( k_request_status_out_of_memory
,
807 gameserver_request_respond( k_request_status_ok
, res
, &body
, msg
);
809 else if( !strcmp( endpoint
, "setlap" ) ){
810 if( client
->steamid
== k_steamid_max
){
811 gameserver_request_respond( k_request_status_unauthorized
,
816 const char *mod
= vg_msg_getkvstr( &data
, "mod" );
817 const char *route
= vg_msg_getkvstr( &data
, "route" );
819 char weekly_table
[ DB_TABLE_UID_MAX
],
820 alltime_table
[ DB_TABLE_UID_MAX
];
822 u32 week
= gameserver_get_current_week();
824 if( !db_get_highscore_table_name( mod
, route
, 0, alltime_table
) ||
825 !db_get_highscore_table_name( mod
, route
, week
, weekly_table
) ){
826 gameserver_request_respond( k_request_status_out_of_memory
,
832 vg_msg_getkvintg( &data
, "time", k_vg_msg_i32
, ¢iseconds
, NULL
);
833 if( centiseconds
< 5*100 ){
834 gameserver_request_respond( k_request_status_client_error
,
839 db_writeusertime( alltime_table
, client
->steamid
, centiseconds
, 1 );
840 db_writeusertime( weekly_table
, client
->steamid
, centiseconds
, 1 );
841 gameserver_request_respond( k_request_status_ok
, res
, NULL
, msg
);
844 gameserver_request_respond( k_request_status_invalid_endpoint
,
849 static void gameserver_rx_300_400( SteamNetworkingMessage_t
*msg
){
850 netmsg_blank
*tmp
= msg
->m_pData
;
852 int client_id
= gameserver_conid( msg
->m_conn
);
853 if( client_id
== -1 ){
854 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
858 if( tmp
->inetmsg_id
== k_inetmsg_request
){
859 if( !packet_minsize( msg
, sizeof(netmsg_request
)+1 ))
862 db_request
*call
= db_alloc_request(
863 sizeof(struct user_request_thread_data
) );
864 struct user_request_thread_data
*inf
= (void *)call
->data
;
866 call
->handler
= gameserver_process_user_request
;
867 db_send_request( call
);
870 vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
872 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
876 static void poll_connections(void){
877 SteamNetworkingMessage_t
*messages
[32];
881 len
= SteamAPI_ISteamNetworkingSockets_ReceiveMessagesOnPollGroup(
882 hSteamNetworkingSockets
,
883 gameserver
.client_group
, messages
, vg_list_size(messages
) );
888 for( int i
=0; i
<len
; i
++ ){
889 SteamNetworkingMessage_t
*msg
= messages
[i
];
891 if( msg
->m_cbSize
< sizeof(netmsg_blank
) ){
892 vg_warn( "Discarding message (too small: %d)\n",
897 netmsg_blank
*tmp
= msg
->m_pData
;
899 if( (tmp
->inetmsg_id
>= 200) && (tmp
->inetmsg_id
< 300) ){
900 gameserver_rx_200_300( msg
);
901 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
903 else if( (tmp
->inetmsg_id
>= 300) && (tmp
->inetmsg_id
< 400) ){
904 gameserver_rx_300_400( msg
);
907 if( tmp
->inetmsg_id
== k_inetmsg_auth
)
908 gameserver_rx_auth( msg
);
909 else if( tmp
->inetmsg_id
== k_inetmsg_version
){
910 gameserver_rx_version( msg
);
913 vg_warn( "Unknown inetmsg_id recieved from client. (%u)\n",
916 SteamAPI_SteamNetworkingMessage_t_Release( msg
);
922 static u64
seconds_to_server_ticks( double s
){
926 int main( int argc
, char *argv
[] ){
927 signal( SIGINT
, inthandler
);
928 signal( SIGQUIT
, inthandler
);
929 signal( SIGPIPE
, SIG_IGN
);
932 while( vg_argp( argc
, argv
) ){
933 if( vg_long_opt( "noauth" ) )
934 gameserver
.auth_mode
= eServerModeNoAuthentication
;
937 vg_set_mem_quota( 80*1024*1024 );
942 * --------------------------------------------------------------- */
943 steamworks_ensure_txt( "2103940" );
944 if( gameserver
.auth_mode
== eServerModeAuthentication
){
945 if( !vg_load_steam_symetric_key( "application_key",
946 gameserver
.app_symmetric_key
)){
951 vg_warn( "Running without user authentication.\n" );
954 if( !SteamGameServer_Init( 0, NETWORK_PORT
, NETWORK_PORT
+1,
955 gameserver
.auth_mode
, "1.0.0.0" ) ){
956 vg_error( "SteamGameServer_Init failed\n" );
960 void *hSteamGameServer
= SteamAPI_SteamGameServer();
961 SteamAPI_ISteamGameServer_LogOnAnonymous( hSteamGameServer
);
963 SteamAPI_ManualDispatch_Init();
964 HSteamPipe hsteampipe
= SteamGameServer_GetHSteamPipe();
965 hSteamNetworkingSockets
=
966 SteamAPI_SteamGameServerNetworkingSockets_SteamAPI();
968 steam_register_callback( k_iSteamNetAuthenticationStatus
, on_auth_status
);
969 steam_register_callback( k_iSteamNetConnectionStatusChangedCallBack
,
972 vg_success( "Steamworks API running\n" );
973 steamworks_event_loop( hsteampipe
);
978 HSteamListenSocket listener
;
979 SteamNetworkingIPAddr localAddr
;
980 SteamAPI_SteamNetworkingIPAddr_Clear( &localAddr
);
981 localAddr
.m_port
= NETWORK_PORT
;
983 listener
= SteamAPI_ISteamNetworkingSockets_CreateListenSocketIP(
984 hSteamNetworkingSockets
, &localAddr
, 0, NULL
);
985 gameserver
.client_group
= SteamAPI_ISteamNetworkingSockets_CreatePollGroup(
986 hSteamNetworkingSockets
);
988 u64 server_ticks
= 8000,
989 last_record_save
= 8000,
990 last_scoreboard_gen
= 0;
993 steamworks_event_loop( hsteampipe
);
1003 SteamAPI_ISteamNetworkingSockets_DestroyPollGroup( hSteamNetworkingSockets
,
1004 gameserver
.client_group
);
1005 SteamAPI_ISteamNetworkingSockets_CloseListenSocket(
1006 hSteamNetworkingSockets
, listener
);
1008 vg_info( "Shutting down\n..." );
1009 SteamGameServer_Shutdown();