4 #include "server/database.h"
5 #include "server/server.h"
6 #include "server/server_config.h"
7 #include "server/server_map.h"
13 // disconnect client without sending a packet (client won't recieve it)
14 static bool disconnect_handler(Client *client, bool good)
17 server_disconnect_client(client, DISCO_NO_SEND, NULL);
21 // insert client into player list and update state (if checks pass)
22 static bool auth_handler(Client *client, bool good)
24 char *name = read_string(client->fd, PLAYER_NAME_MAX);
34 pthread_rwlock_wrlock(&client->server->players_rwlck);
35 u8 success = list_put(&client->server->players, name, client);
39 client->state = CS_ACTIVE;
41 if (! database_load_player(client->name, &client->pos)) {
42 client->pos = (v3f64) {0.0, server_map.spawn_height + 0.5, 0.0};
44 database_create_player(client->name, client->pos);
50 pthread_mutex_lock(&client->mtx);
51 bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success);
54 && write_u32(client->fd, CC_INFO) && write_u32(client->fd, server_config.simulation_distance) && write_s32(client->fd, seed)
55 && write_u32(client->fd, CC_SETPOS) && write_v3f64(client->fd, client->pos)
56 && write_u32(client->fd, CC_TIMEOFDAY) && write_u64(client->fd, (u64) get_time_of_day());
57 pthread_mutex_unlock(&client->mtx);
59 pthread_rwlock_unlock(&client->server->players_rwlck);
61 printf("Authentication %s: %s -> %s\n", success ? "success" : "failure", client->address, name);
66 // set a node on the map
67 static bool setnode_handler(Client *client, bool good)
71 if (! read_v3s32(client->fd, &pos))
76 if (! read_u32(client->fd, &node))
80 map_set_node(server_map.map, pos, map_node_create(node, NULL, 0), false, NULL);
85 // update player's position
86 static bool pos_handler(Client *client, bool good)
90 if (! read_v3f64(client->fd, &pos))
95 database_update_player_pos(client->name, client->pos);
101 // tell server map manager client requested the block
102 static bool request_block_handler(Client *client, bool good)
106 if (! read_v3s32(client->fd, &pos))
110 server_map_requested_block(client, pos);
115 // declared in network.h
116 CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
118 {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
119 {&auth_handler, "AUTH", CS_CREATED},
120 {&setnode_handler, "SETNODE", CS_ACTIVE},
121 {&pos_handler, "POS", CS_ACTIVE},
122 {&request_block_handler, "REQUEST_BLOCK", CS_ACTIVE},