4 #include "server/database.h"
5 #include "server/server.h"
6 #include "server/server_map.h"
12 // disconnect client without sending a packet (client won't recieve it)
13 static bool disconnect_handler(Client *client, bool good)
16 server_disconnect_client(client, DISCO_NO_SEND, NULL);
20 // insert client into player list and update state (if checks pass)
21 static bool auth_handler(Client *client, bool good)
23 char *name = read_string(client->fd, PLAYER_NAME_MAX);
33 pthread_rwlock_wrlock(&client->server->players_rwlck);
34 u8 success = list_put(&client->server->players, name, client);
38 client->state = CS_ACTIVE;
40 if (! database_load_player(client->name, &client->pos)) {
41 client->pos = (v3f64) {0.0, server_map.spawn_height + 0.5, 0.0};
43 database_create_player(client->name, client->pos);
49 pthread_mutex_lock(&client->mtx);
50 bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success);
53 && write_u32(client->fd, CC_INFO) && write_u32(client->fd, client->server->config.simulation_distance) && write_s32(client->fd, seed)
54 && write_u32(client->fd, CC_SETPOS) && write_v3f64(client->fd, client->pos)
55 && write_u32(client->fd, CC_TIMEOFDAY) && write_u64(client->fd, (u64) get_time_of_day());
56 pthread_mutex_unlock(&client->mtx);
58 pthread_rwlock_unlock(&client->server->players_rwlck);
60 printf("Authentication %s: %s -> %s\n", success ? "success" : "failure", client->address, name);
65 // set a node on the map
66 static bool setnode_handler(Client *client, bool good)
70 if (! read_v3s32(client->fd, &pos))
75 if (! map_deserialize_node(client->fd, &node))
79 map_set_node(server_map.map, pos, node, false, NULL);
84 // update player's position
85 static bool pos_handler(Client *client, bool good)
89 if (! read_v3f64(client->fd, &pos))
94 database_update_player_pos(client->name, client->pos);
100 // tell server map manager client requested the block
101 static bool request_block_handler(Client *client, bool good)
105 if (! read_v3s32(client->fd, &pos))
109 server_map_requested_block(client, pos);
114 // declared in network.h
115 CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
117 {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
118 {&auth_handler, "AUTH", CS_CREATED},
119 {&setnode_handler, "SETNODE", CS_ACTIVE},
120 {&pos_handler, "POS", CS_ACTIVE},
121 {&request_block_handler, "REQUEST_BLOCK", CS_ACTIVE},