3 #include <dragontype/number.h>
4 #include "client/client.h"
5 #include "client/client_map.h"
6 #include "client/client_player.h"
11 static bool disconnect_handler(unused Client *client, bool good)
14 client_disconnect(false, NULL);
18 static bool auth_handler(Client *client, bool good)
21 if (! read_u8(client->fd, &success))
28 printf("Authenticated successfully\n");
29 client->state = CS_ACTIVE;
31 printf("Authentication failed, please try again\n");
32 client->state = CS_CREATED;
38 static bool block_handler(Client *client, bool good)
42 if (! read_v3s32(client->fd, &pos))
47 if (! read_u64(client->fd, &size))
50 if (size > sizeof(MapBlockData)) // guard to prevent malicious or malfunctioning packets from allocating huge unnecessary amounts of memory
54 if (! read_full(client->fd, data, size))
60 block = map_get_block(client_map.map, pos, true);
62 block = map_allocate_block(pos);
64 bool ret = map_deserialize_block(block, data, size);
67 client_map_block_received(block);
69 map_free_block(block);
74 static bool info_handler(Client *client, bool good)
76 u32 simulation_distance;
79 if (! (read_u32(client->fd, &simulation_distance) && read_s32(client->fd, &server_seed)))
83 client_map_set_simulation_distance(simulation_distance);
90 static bool setpos_handler(Client *client, bool good)
94 if (! read_v3f64(client->fd, &pos))
98 client_player_set_position(pos);
103 static bool timeofday_handler(Client *client, bool good)
107 if (! read_u64(client->fd, &time_of_day))
111 set_time_of_day(time_of_day);
116 CommandHandler command_handlers[CLIENT_COMMAND_COUNT] = {
118 {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_AUTH | CS_ACTIVE},
119 {&auth_handler, "AUTH", CS_AUTH},
120 {&block_handler, "BLOCK", CS_ACTIVE},
121 {&info_handler, "INFO", CS_ACTIVE},
122 {&setpos_handler, "SETPOS", CS_ACTIVE},
123 {&timeofday_handler, "TIMEOFDAY", CS_ACTIVE},