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_player.h"
11 #include "client/client_terrain.h"
12 #include "client/debug_menu.h"
13 #include "client/game.h"
14 #include "client/input.h"
16 #include "interrupt.h"
20 DragonnetPeer *client;
25 static bool on_recv(__attribute__((unused)) DragonnetPeer *peer, DragonnetTypeId type, __attribute__((unused)) void *pkt)
28 pthread_mutex_lock(&client_auth.mtx);
30 // this code exists to stop malicious or malfunctioning packets
31 switch (client_auth.state) {
32 // the server shouldn't send anything during auth preparation, drop it
37 // only the auth packet is allowed before auth is finished
39 allowed = type == DRAGONNET_TYPE_ToClientAuth;
42 // don't process auth packets when auth is already finished
44 allowed = type != DRAGONNET_TYPE_ToClientAuth;
49 It is important that the auth state does not change to until the packet is
52 However, the only state change done by other threads is AUTH_INIT -> AUTH_WAIT,
53 which is not problematic since packets that are received during AUTH_INIT
54 are not processed, they are always dropped.
56 Therefore the mutex can be unlocked at this point.
58 pthread_mutex_unlock(&client_auth.mtx);
62 static void on_disconnect(__attribute__((unused)) DragonnetPeer *peer)
65 // don't free the connection before all other client components have shut down
69 static void on_ToClientAuth(__attribute__((unused)) DragonnetPeer *peer, ToClientAuth *pkt)
71 pthread_mutex_lock(&client_auth.mtx);
73 client_auth.state = AUTH_SUCCESS;
74 printf("[access] authenticated successfully\n");
76 client_auth.state = AUTH_INIT;
77 printf("[access] authentication failed, please try again\n");
79 pthread_cond_signal(&client_auth.cv);
80 pthread_mutex_unlock(&client_auth.mtx);
82 // yield the connection until the game is fully initialized
87 static void on_ToClientChunk(__attribute__((unused)) DragonnetPeer *peer, ToClientChunk *pkt)
89 TerrainChunk *chunk = terrain_get_chunk(client_terrain, pkt->pos, true);
91 terrain_deserialize_chunk(chunk, pkt->data);
92 ((TerrainChunkMeta *) chunk->extra)->empty = (pkt->data.siz == 0);
93 client_terrain_chunk_received(chunk);
96 static void on_ToClientInfo(__attribute__((unused)) DragonnetPeer *peer, ToClientInfo *pkt)
98 client_terrain_set_load_distance(pkt->load_distance);
102 static void on_ToClientTimeOfDay(__attribute__((unused)) DragonnetPeer *peer, ToClientTimeOfDay *pkt)
104 set_time_of_day(pkt->time_of_day);
107 static void on_ToClientMovement(__attribute__((unused)) DragonnetPeer *peer, ToClientMovement *pkt)
109 pthread_rwlock_wrlock(&client_player.lock_movement);
110 client_player.movement = *pkt;
111 pthread_rwlock_unlock(&client_player.lock_movement);
113 debug_menu_changed(ENTRY_FLIGHT);
114 debug_menu_changed(ENTRY_COLLISION);
117 int main(int argc, char **argv)
119 #ifdef __GLIBC__ // check whether bloat is enabled
120 pthread_setname_np(pthread_self(), "main");
124 fprintf(stderr, "[error] missing address\n");
128 if (!(client = dragonnet_connect(argv[1]))) {
129 fprintf(stderr, "[error] failed to connect to server\n");
133 printf("[access] connected to %s\n", client->address);
135 client->on_disconnect = &on_disconnect;
136 client->on_recv = (void *) &on_recv;
137 client->on_recv_type[DRAGONNET_TYPE_ToClientAuth ] = (void *) &on_ToClientAuth;
138 client->on_recv_type[DRAGONNET_TYPE_ToClientChunk ] = (void *) &on_ToClientChunk;
139 client->on_recv_type[DRAGONNET_TYPE_ToClientInfo ] = (void *) &on_ToClientInfo;
140 client->on_recv_type[DRAGONNET_TYPE_ToClientTimeOfDay ] = (void *) &on_ToClientTimeOfDay;
141 client->on_recv_type[DRAGONNET_TYPE_ToClientMovement ] = (void *) &on_ToClientMovement;
142 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityAdd ] = (void *) &client_entity_add;
143 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityRemove ] = (void *) &client_entity_remove;
144 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityUpdatePosRot ] = (void *) &client_entity_update_pos_rot;
145 client->on_recv_type[DRAGONNET_TYPE_ToClientEntityUpdateNametag] = (void *) &client_entity_update_nametag;
151 client_terrain_init();
152 client_player_init();
153 client_entity_init();
154 dragonnet_peer_run(client);
156 if (!client_auth_init())
159 if (!game(&gfx_init))
162 dragonnet_peer_shutdown(client);
163 client_auth_deinit();
164 client_entity_deinit();
165 client_player_deinit();
166 client_terrain_deinit();
169 pthread_t recv_thread = client->recv_thread;
172 pthread_join(recv_thread, NULL);