Crossfire Server, Trunk
|
#include "global.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include "image.h"
#include "newserver.h"
#include "sockproto.h"
#include "sproto.h"
Go to the source code of this file.
Data Structures | |
struct | client_cmd_mapping |
struct | player_cmd_mapping |
Typedefs | |
typedef void(* | func_uint8_int_ns) (char *, int, socket_struct *) |
typedef void(* | func_uint8_int_pl) (char *, int, player *) |
Functions | |
static void | block_until_new_connection (void) |
bool | connection_alive (socket_struct socket) |
void | do_server (void) |
bool | handle_client (socket_struct *ns, player *pl) |
static int | handle_cmd (socket_struct *ns, player *pl, char *cmd, char *data, int len) |
static int | is_fd_valid (int fd) |
static void | new_connection (int listen_fd) |
void | request_info_cmd (char *buf, int len, socket_struct *ns) |
static void | send_delayed_buffers (player *pl) |
static void | send_updates (player *pl) |
void | update_players () |
Variables | |
static const struct client_cmd_mapping | client_commands [] |
static const struct player_cmd_mapping | player_commands [] |
Main client/server loops.
Mainly deals with initialization and higher level socket maintenance (checking for lost connections and if data has arrived.) The reading of data is handled in lowlevel.c
Definition in file loop.c.
typedef void(* func_uint8_int_ns) (char *, int, socket_struct *) |
typedef void(* func_uint8_int_pl) (char *, int, player *) |
|
static |
Waits for new connection when there is no one connected.
Definition at line 351 of file loop.c.
References Socket_Info::allocated_sockets, Settings::fastclock, flush_old_maps(), init_sockets, socket_struct::listen, llevInfo, LOG(), Socket_Info::max_filedescriptor, metaserver_update(), Ns_Add, reset_sleep(), settings, socket_info, takeitem::status, tick_the_clock(), and watchdog().
Referenced by do_server().
bool connection_alive | ( | socket_struct | socket | ) |
Check whether the given socket's connection is alive or not.
Definition at line 498 of file loop.c.
References BEAT_INTERVAL, socket_struct::heartbeat, socket_struct::last_tick, and tick_length().
Referenced by update_players().
void do_server | ( | void | ) |
This checks the sockets for input and exceptions, does the right thing.
A bit of this code is grabbed out of socket.c There are 2 lists we need to look through - init_sockets is a list
Definition at line 556 of file loop.c.
References Socket_Info::allocated_sockets, block_until_new_connection(), socket_struct::fd, final_free_player(), first_player, FREE_AND_CLEAR, free_newsocket(), get_sleep_remaining(), handle_client(), socket_struct::host, init_listening_socket(), init_sockets, is_fd_valid(), jump_time(), leave(), llevError, llevInfo, LOG(), Socket_Info::max_filedescriptor, obj::name, new_connection(), give::next, pl::next, Ns_Add, Ns_Avail, Ns_Dead, pl::ob, altar_valkyrie::pl, save_player(), send_delayed_buffers(), send_updates(), pl::socket, socket_info, takeitem::status, socket_struct::status, and Socket_Info::timeout.
Referenced by server_main().
bool handle_client | ( | socket_struct * | ns, |
player * | pl | ||
) |
Handle commands from a client. This function should only be called when the socket is readable. If an error occurs, the socket status will be set to Ns_Dead; it is up to the caller to clean it up.
ns | Socket sending the command |
pl | Player associated with this socket, or NULL |
Definition at line 253 of file loop.c.
References SockList::buf, navar-midane_time::data, socket_struct::faces_sent, socket_struct::fd, handle_cmd(), socket_struct::inbuf, socket_struct::last_tick, SockList::len, Ns_Dead, pl::ob, SockList_NullTerminate(), SockList_ReadPacket(), SockList_ResetRead(), obj::speed_left, ST_PLAYING, pl::state, takeitem::status, and socket_struct::status.
Referenced by do_server().
|
static |
Handle a command, either directly sent by the client, or sent for a player's action.
ns | socket sending the command, must not be NULL. |
pl | player associated to the socket, may be NULL. |
cmd | command name. |
data | additional data, can be NULL. |
len | length of data. |
Definition at line 205 of file loop.c.
References client_commands, client_cmd_mapping::cmdname, player_cmd_mapping::cmdname, client_cmd_mapping::cmdproc, player_cmd_mapping::cmdproc, navar-midane_time::data, player_cmd_mapping::flag, socket_struct::host, llevDebug, LOG(), player_commands, ST_PLAYING, and pl::state.
Referenced by handle_client().
|
static |
Checks if file descriptor is valid.
fd | file descriptor to check. |
Definition at line 413 of file loop.c.
Referenced by do_server().
|
static |
Handle a new connection from a client.
listen_fd | file descriptor the request came from. |
Definition at line 425 of file loop.c.
References altar_valkyrie::accept(), Socket_Info::allocated_sockets, buf, checkbanned(), socket_struct::faces_sent, socket_struct::faces_sent_len, fatal(), socket_struct::fd, get_faces_count(), init_connection(), init_sockets, log_login::ip, socket_struct::listen, llevDebug, llevError, llevInfo, LOG(), MAX_BUF, Ns_Avail, OUT_OF_MEMORY, SEE_LAST_ERROR, socket_info, socklen_t, takeitem::status, and socket_struct::status.
Referenced by do_server().
void request_info_cmd | ( | char * | buf, |
int | len, | ||
socket_struct * | ns | ||
) |
request_info_cmd is sort of a meta command. There is some specific request of information, but we call other functions to provide that information.
buf | buffer containing the information requested. |
len | length of buf, ignored. |
ns | socket to write data to. |
Definition at line 134 of file loop.c.
References buf, socket_struct::host, knowledge_send_info(), llevDebug, LOG(), roll-o-matic::params, send_class_info(), send_class_list(), send_exp_table(), send_file(), send_image_info(), send_image_sums(), send_map_info(), send_new_char_info(), send_race_info(), send_race_list(), send_skill_info(), send_spell_paths(), Send_With_Handling(), SockList_AddString(), SockList_Init(), and SockList_Term().
|
static |
Send all delayed buffers for a player.
pl | player to send buffers for. |
Definition at line 516 of file loop.c.
References buf, pl::delayed_buffers, pl::delayed_buffers_used, Send_With_Handling(), and pl::socket.
Referenced by do_server().
|
static |
Send updated stats, map, look, and inventory to the player.
Definition at line 526 of file loop.c.
References obj::container, draw_client_map(), esrv_draw_look(), esrv_send_inventory(), esrv_update_item(), esrv_update_stats(), pl::last_weight, llevError, LOG(), pl::ob, pl::socket, UPD_WEIGHT, socket_struct::update_inventory, socket_struct::update_look, and WEIGHT.
Referenced by do_server(), and update_players().
void update_players | ( | ) |
Send updates to players. Called once per tick.
Definition at line 699 of file loop.c.
References connection_alive(), first_player, socket_struct::last_tick, llevDebug, LOG(), give::next, pl::next, altar_valkyrie::pl, send_tick(), send_updates(), pl::socket, ST_PLAYING, pl::state, and socket_struct::tick.
Referenced by server_main().
|
static |
Commands sent directly by client, when connecting or when needed.
Definition at line 109 of file loop.c.
Referenced by handle_cmd().
|
static |
Dispatch tables for the server.
CmdMapping is the dispatch table for the server, used in handle_client, which gets called when the client has input. All commands called here use the same parameter form (char *data, int len, int clientnum. We do implicit casts, because the data that is being passed is unsigned (pretty much needs to be for binary data), however, most of these treat it only as strings, so it makes things easier to cast it here instead of a bunch of times in the function itself. flag is 1 if the player must be in the playing state to issue the command, 0 if they can issue it at any time. Commands sent by the client reacting to player's actions.
Definition at line 95 of file loop.c.
Referenced by handle_cmd().