1 #define _GNU_SOURCE // don't worry, GNU extensions are only used when available
2 #include <dragonstd/flag.h>
8 #include "client/client.h"
9 #include "client/client_auth.h"
10 #include "client/client_inventory.h"
11 #include "client/client_node.h"
12 #include "client/client_player.h"
13 #include "client/client_terrain.h"
14 #include "client/debug_menu.h"
15 #include "client/game.h"
16 #include "client/input.h"
18 #include "interrupt.h"
22 DragonnetPeer *client;
27 static bool on_recv(__attribute__((unused)) DragonnetPeer *peer, DragonnetTypeId type, __attribute__((unused)) void *pkt)
30 pthread_mutex_lock(&client_auth.mtx);
32 // this code exists to stop malicious or malfunctioning packets
33 switch (client_auth.state) {
34 // the server shouldn't send anything during auth preparation, drop it
39 // only the auth packet is allowed before auth is finished
41 allowed = type == DRAGONNET_TYPE_ToClientAuth;
44 // don't process auth packets when auth is already finished
46 allowed = type != DRAGONNET_TYPE_ToClientAuth;
51 It is important that the auth state does not change to until the packet is
54 However, the only state change done by other threads is AUTH_INIT -> AUTH_WAIT,
55 which is not problematic since packets that are received during AUTH_INIT
56 are not processed, they are always dropped.
58 Therefore the mutex can be unlocked at this point.
60 pthread_mutex_unlock(&client_auth.mtx);
64 static void on_disconnect(__attribute__((unused)) DragonnetPeer *peer)
67 // don't free the connection before all other client components have shut down
71 static void on_ToClientAuth(__attribute__((unused)) DragonnetPeer *peer, ToClientAuth *pkt)
73 pthread_mutex_lock(&client_auth.mtx);
75 client_auth.state = AUTH_SUCCESS;
76 printf("[access] authenticated successfully\n");
78 client_auth.state = AUTH_INIT;
79 printf("[access] authentication failed, please try again\n");
81 pthread_cond_signal(&client_auth.cv);
82 pthread_mutex_unlock(&client_auth.mtx);
84 // yield the connection until the game is fully initialized
89 static void on_ToClientInfo(__attribute__((unused)) DragonnetPeer *peer, ToClientInfo *pkt)
91 client_terrain_set_load_distance(pkt->load_distance);
95 static void on_ToClientTimeOfDay(__attribute__((unused)) DragonnetPeer *peer, ToClientTimeOfDay *pkt)
97 set_time_of_day(pkt->time_of_day);
100 static void on_ToClientMovement(__attribute__((unused)) DragonnetPeer *peer, ToClientMovement *pkt)
102 pthread_rwlock_wrlock(&client_player.lock_movement);
103 client_player.movement = *pkt;
104 pthread_rwlock_unlock(&client_player.lock_movement);
106 debug_menu_changed(ENTRY_FLIGHT);
107 debug_menu_changed(ENTRY_COLLISION);
110 int main(int argc, char **argv)
112 #ifdef __GLIBC__ // check whether bloat is enabled
113 pthread_setname_np(pthread_self(), "main");
117 fprintf(stderr, "[error] missing address\n");
121 if (!(client = dragonnet_connect(argv[1]))) {
122 fprintf(stderr, "[error] failed to connect to server\n");
126 printf("[access] connected to %s\n", client->address);
128 client->on_disconnect = &on_disconnect;
129 client->on_recv = (void *) &on_recv;
130 client->on_recv_type[DRAGONNET_TYPE_ToClientAuth ] = (void *) &on_ToClientAuth;
131 client->on_recv_type[DRAGONNET_TYPE_ToClientChunk ] = (void *) &client_terrain_receive_chunk;
132 client->on_recv_type[DRAGONNET_TYPE_ToClientInfo ] = (void *) &on_ToClientInfo;
133 client->on_recv_type[DRAGONNET_TYPE_ToClientTimeOfDay ] = (void *) &on_ToClientTimeOfDay;
134 client->on_recv_type[DRAGONNET_TYPE_ToClientMovement ] = (void *) &on_ToClientMovement;
135 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityAdd ] = (void *) &client_entity_add;
136 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityRemove ] = (void *) &client_entity_remove;
137 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityUpdatePosRot ] = (void *) &client_entity_update_pos_rot;
138 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityUpdateNametag] = (void *) &client_entity_update_nametag;
139 client->on_recv_type[DRAGONNET_TYPE_ToClientPlayerInventory ] = (void *) &client_inventory_update_player;
145 client_terrain_init();
146 client_player_init();
147 client_entity_init();
148 dragonnet_peer_run(client);
153 dragonnet_peer_shutdown(client);
154 client_auth_deinit();
155 client_entity_deinit();
156 client_player_deinit();
157 client_terrain_deinit();
160 pthread_t recv_thread = client->recv_thread;
163 pthread_join(recv_thread, NULL);