]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client.c
55bfc4516c460bcf8472bd233dff68bb86927173
[dragonblocks_alpha.git] / src / client / client.c
1 #define _GNU_SOURCE // don't worry, GNU extensions are only used when available
2 #include <dragonstd/flag.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <pthread.h>
7 #include <unistd.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"
17 #include "day.h"
18 #include "interrupt.h"
19 #include "perlin.h"
20 #include "types.h"
21
22 DragonnetPeer *client;
23
24 static Flag finish;
25 static Flag gfx_init;
26
27 static bool on_recv(__attribute__((unused)) DragonnetPeer *peer, DragonnetTypeId type, __attribute__((unused)) void *pkt)
28 {
29         bool allowed = false;
30         pthread_mutex_lock(&client_auth.mtx);
31
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
35                 case AUTH_INIT:
36                         allowed = false;
37                         break;
38
39                 // only the auth packet is allowed before auth is finished
40                 case AUTH_WAIT:
41                         allowed = type == DRAGONNET_TYPE_ToClientAuth;
42                         break;
43
44                 // don't process auth packets when auth is already finished
45                 case AUTH_SUCCESS:
46                         allowed = type != DRAGONNET_TYPE_ToClientAuth;
47                         break;
48         }
49
50         /*
51                 It is important that the auth state does not change to until the packet is
52                         processed.
53
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.
57
58                 Therefore the mutex can be unlocked at this point.
59         */
60         pthread_mutex_unlock(&client_auth.mtx);
61         return allowed;
62 }
63
64 static void on_disconnect(__attribute__((unused)) DragonnetPeer *peer)
65 {
66         flag_set(&interrupt);
67         // don't free the connection before all other client components have shut down
68         flag_slp(&finish);
69 }
70
71 static void on_ToClientAuth(__attribute__((unused)) DragonnetPeer *peer, ToClientAuth *pkt)
72 {
73         pthread_mutex_lock(&client_auth.mtx);
74         if (pkt->success) {
75                 client_auth.state = AUTH_SUCCESS;
76                 printf("[access] authenticated successfully\n");
77         } else {
78                 client_auth.state = AUTH_INIT;
79                 printf("[access] authentication failed, please try again\n");
80         }
81         pthread_cond_signal(&client_auth.cv);
82         pthread_mutex_unlock(&client_auth.mtx);
83
84         // yield the connection until the game is fully initialized
85         if (pkt->success)
86                 flag_slp(&gfx_init);
87 }
88
89 static void on_ToClientInfo(__attribute__((unused)) DragonnetPeer *peer, ToClientInfo *pkt)
90 {
91         client_terrain_set_load_distance(pkt->load_distance);
92         seed = pkt->seed;
93 }
94
95 static void on_ToClientTimeOfDay(__attribute__((unused)) DragonnetPeer *peer, ToClientTimeOfDay *pkt)
96 {
97         set_time_of_day(pkt->time_of_day);
98 }
99
100 static void on_ToClientMovement(__attribute__((unused)) DragonnetPeer *peer, ToClientMovement *pkt)
101 {
102         pthread_rwlock_wrlock(&client_player.lock_movement);
103         client_player.movement = *pkt;
104         pthread_rwlock_unlock(&client_player.lock_movement);
105
106         debug_menu_changed(ENTRY_FLIGHT);
107         debug_menu_changed(ENTRY_COLLISION);
108 }
109
110 int main(int argc, char **argv)
111 {
112 #ifdef __GLIBC__ // check whether bloat is enabled
113         pthread_setname_np(pthread_self(), "main");
114 #endif // __GLIBC__
115
116         if (argc < 2) {
117                 fprintf(stderr, "[error] missing address\n");
118                 return EXIT_FAILURE;
119         }
120
121         if (!(client = dragonnet_connect(argv[1]))) {
122                 fprintf(stderr, "[error] failed to connect to server\n");
123                 return EXIT_FAILURE;
124         }
125
126         printf("[access] connected to %s\n", client->address);
127
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;
140
141         flag_ini(&finish);
142         flag_ini(&gfx_init);
143
144         interrupt_init();
145         client_terrain_init();
146         client_player_init();
147         client_entity_init();
148         dragonnet_peer_run(client);
149         client_auth_init();
150
151         game(&gfx_init);
152
153         dragonnet_peer_shutdown(client);
154         client_auth_deinit();
155         client_entity_deinit();
156         client_player_deinit();
157         client_terrain_deinit();
158         interrupt_deinit();
159
160         pthread_t recv_thread = client->recv_thread;
161
162         flag_set(&finish);
163         pthread_join(recv_thread, NULL);
164
165         flag_dst(&finish);
166         flag_dst(&gfx_init);
167
168         return EXIT_SUCCESS;
169 }