]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/servercommands.c
Redesign MapBlock sending
[dragonblocks_alpha.git] / src / servercommands.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "mapgen.h"
4 #include "server.h"
5 #include "util.h"
6
7 static bool disconnect_handler(Client *client, bool good)
8 {
9         if (good)
10                 server_disconnect_client(client, DISCO_NO_SEND, NULL);
11         return true;
12 }
13
14 static bool send_map(Client *client)
15 {
16         for (size_t s = 0; s < client->server->map->sectors.siz; s++) {
17                 MapSector *sector = map_get_sector_raw(client->server->map, s);
18                 for (size_t b = 0; b < sector->blocks.siz; b++)
19                         if (! (write_u32(client->fd, CC_BLOCK) && map_serialize_block(client->fd, map_get_block_raw(sector, b))))
20                                 return false;
21         }
22         return true;
23 }
24
25 static bool auth_handler(Client *client, bool good)
26 {
27         char *name = read_string(client->fd, NAME_MAX);
28
29         if (! name)
30                 return false;
31
32         if (! good) {
33                 free(name);
34                 return true;
35         }
36
37         u8 success = list_put(&client->server->clients, name, client);
38
39         printf("Authentication %s: %s -> %s\n", success ? "success" : "failure", client->address, name);
40
41         if (success) {
42                 client->name = name;
43                 client->state = CS_ACTIVE;
44                 mapgen_start_thread(client);
45         } else {
46                 free(name);
47         }
48
49         pthread_mutex_lock(&client->mtx);
50         bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success) && (success ? send_map(client) : true);
51         pthread_mutex_unlock(&client->mtx);
52
53         return ret;
54 }
55
56 static bool setnode_handler(Client *client, bool good)
57 {
58         v3s32 pos;
59
60         if (! read_v3s32(client->fd, &pos))
61                 return false;
62
63         MapNode node;
64
65         if (! map_deserialize_node(client->fd, &node))
66                 return false;
67
68         if (good)
69                 map_set_node(client->server->map, pos, node);
70
71         return true;
72 }
73
74 static bool kick_handler(Client *client, bool good)
75 {
76         char *target_name = read_string(client->fd, NAME_MAX);
77
78         if (! target_name)
79                 return false;
80
81         if (good) {
82                 Client *target = list_get(&client->server->clients, target_name);
83                 if (target)
84                         server_disconnect_client(target, 0, "kicked");
85         }
86
87         free(target_name);
88         return true;
89 }
90
91 CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
92         {0},
93         {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
94         {&auth_handler, "AUTH", CS_CREATED},
95         {&setnode_handler, "SETNODE", CS_ACTIVE},
96         {&kick_handler, "KICK", CS_ACTIVE},
97 };