Crossfire Server, Trunk  R20513
Data Structures | Typedefs | Functions | Variables
loop.c File Reference

Main client/server loops. More...

#include "global.h"
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include "image.h"
#include "newserver.h"
#include "server.h"
#include "sockproto.h"
#include "sproto.h"
Include dependency graph for loop.c:

Go to the source code of this file.

Data Structures

struct  client_cmd_mapping
 Definition of a function the client sends without player interaction. More...
 
struct  player_cmd_mapping
 Definition of a function called in reaction to player's action. More...
 

Typedefs

typedef void(* func_uint8_int_ns) (char *, int, socket_struct *)
 Prototype for functions the client sends without player interaction. More...
 
typedef void(* func_uint8_int_pl) (char *, int, player *)
 Prototype for functions used to handle player actions. More...
 

Functions

static void block_until_new_connection (void)
 Waits for new connection when there is no one connected. More...
 
bool connection_alive (socket_struct socket)
 Check whether the given socket's connection is alive or not. More...
 
void do_server (void)
 This checks the sockets for input and exceptions, does the right thing. More...
 
void handle_client (socket_struct *ns, player *pl)
 Handle commands from a client. More...
 
static int handle_cmd (socket_struct *ns, player *pl, char *cmd, char *data, int len)
 
static int is_fd_valid (int fd)
 Checks if file descriptor is valid. More...
 
static void new_connection (int listen_fd)
 Handle a new connection from a client. More...
 
void request_info_cmd (char *buf, int len, socket_struct *ns)
 request_info_cmd is sort of a meta command. More...
 

Variables

static const struct client_cmd_mapping client_commands []
 Commands sent directly by client, when connecting or when needed. More...
 
static const struct player_cmd_mapping player_commands []
 Dispatch tables for the server. More...
 
unsigned long todtick
 Ingame time. More...
 

Detailed Description

Main client/server loops.

Date
2003-12-02

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 Documentation

typedef void(* func_uint8_int_ns) (char *, int, socket_struct *)

Prototype for functions the client sends without player interaction.

Definition at line 62 of file loop.c.

typedef void(* func_uint8_int_pl) (char *, int, player *)

Prototype for functions used to handle player actions.

Definition at line 71 of file loop.c.

Function Documentation

static void block_until_new_connection ( void  )
static

Waits for new connection when there is no one connected.

Definition at line 333 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, tick_the_clock(), and watchdog().

Referenced by do_server().

Here is the call graph for this function:

Here is the caller graph for this function:

bool connection_alive ( socket_struct  socket)

Check whether the given socket's connection is alive or not.

Returns
True if connection is active, false if not.

Definition at line 480 of file loop.c.

References BEAT_INTERVAL, socket_struct::heartbeat, socket_struct::last_tick, and tick_length().

Referenced by do_server().

Here is the call graph for this function:

Here is the caller graph for this function:

void do_server ( void  )
void 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.

Parameters
nsSocket sending the command
plPlayer associated with this socket, or NULL

Definition at line 234 of file loop.c.

References SockList::buf, 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, socket_struct::status, strtok_r, todtick, and watchdog().

Referenced by do_server(), and handle_newcs_player().

Here is the call graph for this function:

Here is the caller graph for this function:

static int handle_cmd ( socket_struct ns,
player pl,
char *  cmd,
char *  data,
int  len 
)
static

Definition at line 195 of file loop.c.

References client_cmd_mapping::cmdname, player_cmd_mapping::cmdname, client_cmd_mapping::cmdproc, player_cmd_mapping::cmdproc, player_cmd_mapping::flag, socket_struct::host, llevDebug, LOG(), ST_PLAYING, and pl::state.

Referenced by handle_client().

Here is the call graph for this function:

Here is the caller graph for this function:

static int is_fd_valid ( int  fd)
static

Checks if file descriptor is valid.

Parameters
fdfile descriptor to check.
Returns
1 if fd is valid, 0 else.

Definition at line 395 of file loop.c.

Referenced by do_server().

Here is the caller graph for this function:

static void new_connection ( int  listen_fd)
static

Handle a new connection from a client.

Parameters
listen_fdfile descriptor the request came from.

Definition at line 407 of file loop.c.

References Socket_Info::allocated_sockets, checkbanned(), socket_struct::faces_sent, socket_struct::faces_sent_len, fatal(), socket_struct::fd, init_connection(), init_sockets, socket_struct::listen, llevDebug, llevError, llevInfo, LOG(), MAX_BUF, nrofpixmaps, Ns_Avail, OUT_OF_MEMORY, SEE_LAST_ERROR, snprintf, socket_info, socklen_t, and socket_struct::status.

Referenced by do_server().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters
bufbuffer containing the information requested.
lenlength of buf, ignored.
nssocket to write data to.

Definition at line 133 of file loop.c.

References socket_struct::host, knowledge_send_info(), llevDebug, LOG(), 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().

Here is the call graph for this function:

Variable Documentation

const struct client_cmd_mapping client_commands[]
static
Initial value:
= {
{ "addme", add_me_cmd },
{ "askface", send_face_cmd },
{ "beat", NULL },
{ "requestinfo", request_info_cmd },
{ "setup", set_up_cmd },
{ "version", version_cmd },
{ "asksmooth", ask_smooth_cmd },
{ "accountlogin", account_login_cmd },
{ "accountnew", account_new_cmd },
{ "accountaddplayer", account_add_player_cmd },
{ "accountplay", account_play_cmd },
{ "accountpw", account_password },
{ "createplayer", create_player_cmd },
{ NULL, NULL }
}
void account_login_cmd(char *buf, int len, socket_struct *ns)
Handles the account login.
Definition: request.c:2037
void version_cmd(char *buf, int len, socket_struct *ns)
Client tells its version.
Definition: request.c:581
void account_password(char *buf, int len, socket_struct *ns)
Handles the account password change.
Definition: request.c:2862
void account_new_cmd(char *buf, int len, socket_struct *ns)
Handles the account creation This function shares a fair amount of the same logic as account_login_cm...
Definition: request.c:2163
void set_up_cmd(char *buf, int len, socket_struct *ns)
This is the Setup cmd - easy first implementation.
Definition: request.c:93
void request_info_cmd(char *buf, int len, socket_struct *ns)
request_info_cmd is sort of a meta command.
Definition: loop.c:133
void ask_smooth_cmd(char *buf, int len, socket_struct *ns)
Tells client the picture it has to use to smooth a picture number given as argument.
Definition: request.c:423
void account_add_player_cmd(char *buf, int len, socket_struct *ns)
Handle accountaddplayer from server (add a character to this account).
Definition: request.c:2271
void add_me_cmd(char *buf, int len, socket_struct *ns)
The client has requested to be added to the game.
Definition: request.c:347
void send_face_cmd(char *buff, int len, socket_struct *ns)
Client has requested pixmap that it somehow missed getting.
Definition: image.c:43
void create_player_cmd(char *buf, int len, socket_struct *ns)
We have received a createplayer command.
Definition: request.c:2487
void account_play_cmd(char *buf, int len, socket_struct *ns)
We have received an accountplay command.
Definition: request.c:2400

Commands sent directly by client, when connecting or when needed.

Definition at line 108 of file loop.c.

const struct player_cmd_mapping player_commands[]
static
Initial value:
= {
{ "examine", examine_cmd, 1 },
{ "apply", apply_cmd, 1 },
{ "move", move_cmd, 1 },
{ "reply", reply_cmd, 0 },
{ "lookat", look_at_cmd, 1 },
{ "inscribe", inscribe_scroll_cmd, 0 },
{ NULL, NULL, 0 }
}
void mark_item_cmd(uint8_t *data, int len, player *pl)
Client wants to mark some object.
Definition: item.c:723
void look_at_cmd(char *buf, int len, player *pl)
Client wants to look at some object.
Definition: item.c:819
void new_player_cmd(uint8_t *buf, int len, player *pl)
This handles the commands issued by the player (ie, north, fire, cast, etc.).
Definition: request.c:447
void inscribe_scroll_cmd(char *buf, int len, player *pl)
Definition: item.c:908
void lock_item_cmd(uint8_t *data, int len, player *pl)
Client wants to apply some object.
Definition: item.c:671
void(* func_uint8_int_pl)(char *, int, player *)
Prototype for functions used to handle player actions.
Definition: loop.c:71
void examine_cmd(char *buf, int len, player *pl)
Client wants to examine some object.
Definition: item.c:612
void apply_cmd(char *buf, int len, player *pl)
Client wants to apply some object.
Definition: item.c:631
void reply_cmd(char *buf, int len, player *pl)
This is a reply to a previous query.
Definition: request.c:509
void move_cmd(char *buf, int len, player *pl)
Moves an object (typically, container to inventory).
Definition: request.c:636

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 94 of file loop.c.

unsigned long todtick

Ingame time.

Definition at line 440 of file init.c.

Referenced by handle_client(), init_clocks(), and write_todclock().