1 |
/* |
2 |
* CrossFire, A Multiplayer game for X-windows |
3 |
* |
4 |
* Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
5 |
* Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team |
6 |
* Copyright (C) 1992 Frank Tore Johansen |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
* |
22 |
* The author can be reached via e-mail to <crossfire@schmorp.de> |
23 |
*/ |
24 |
|
25 |
/** |
26 |
* \file |
27 |
* Main client/server loops. |
28 |
* |
29 |
* \date 2003-12-02 |
30 |
* |
31 |
* loop.c mainly deals with initialization and higher level socket |
32 |
* maintanance (checking for lost connections and if data has arrived.) |
33 |
*/ |
34 |
|
35 |
|
36 |
#include <global.h> |
37 |
#include <sproto.h> |
38 |
#include <sockproto.h> |
39 |
|
40 |
#include <sys/types.h> |
41 |
#include <sys/time.h> |
42 |
#include <sys/socket.h> |
43 |
#include <netinet/in.h> |
44 |
#include <netdb.h> |
45 |
|
46 |
#include <unistd.h> |
47 |
#include <arpa/inet.h> |
48 |
|
49 |
#include <loader.h> |
50 |
|
51 |
#define MAX_QUEUE_DEPTH 500 //TODO |
52 |
#define MAX_QUEUE_BACKLOG 3. //TODO |
53 |
|
54 |
void |
55 |
client::reset_state () |
56 |
{ |
57 |
if (!pl) |
58 |
return; |
59 |
|
60 |
pl->run_on = 0; |
61 |
pl->fire_on = 0; |
62 |
} |
63 |
|
64 |
void |
65 |
client::queue_command (packet_type *handler, char *data, int datalen) |
66 |
{ |
67 |
tstamp stamp = now (); |
68 |
|
69 |
if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
70 |
{ |
71 |
//TODO: just disconnect here? |
72 |
reset_state (); |
73 |
send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
74 |
} |
75 |
else |
76 |
{ |
77 |
cmd_queue.push_back (command ()); |
78 |
command &cmd = cmd_queue.back (); |
79 |
cmd.stamp = stamp; |
80 |
cmd.handler = handler; |
81 |
cmd.data = salloc<char> (datalen + 1, data); |
82 |
cmd.datalen = datalen; |
83 |
} |
84 |
} |
85 |
|
86 |
bool |
87 |
client::handle_command () |
88 |
{ |
89 |
bool skipping = false; |
90 |
|
91 |
while (!cmd_queue.empty () |
92 |
&& !(state == ST_PLAYING && pl->ob && pl->ob->speed_left <= 0)) |
93 |
{ |
94 |
command &cmd = cmd_queue.front (); |
95 |
|
96 |
if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
97 |
{ |
98 |
if (!skipping) |
99 |
{ |
100 |
skipping = true; |
101 |
reset_state (); |
102 |
send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); |
103 |
} |
104 |
|
105 |
cmd_queue.pop_front (); |
106 |
} |
107 |
else |
108 |
{ |
109 |
execute (cmd.handler, cmd.data, cmd.datalen); |
110 |
cmd_queue.pop_front (); |
111 |
return true; |
112 |
} |
113 |
} |
114 |
|
115 |
return false; |
116 |
} |
117 |
|
118 |
void |
119 |
flush_sockets (void) |
120 |
{ |
121 |
for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
122 |
(*i)->flush (); |
123 |
} |
124 |
|
125 |
/** |
126 |
* This checks the sockets for input, does the right thing. |
127 |
* |
128 |
* A bit of this code is grabbed out of socket.c |
129 |
* There are 2 lists we need to look through - init_sockets is a list |
130 |
* |
131 |
*/ |
132 |
void |
133 |
doeric_server (void) |
134 |
{ |
135 |
#ifdef CS_LOGSTATS |
136 |
if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
137 |
write_cs_stats (); |
138 |
#endif |
139 |
|
140 |
//TODO: should not be done here, either |
141 |
for (unsigned i = 0; i < clients.size (); ++i) |
142 |
{ |
143 |
client *s = clients [i]; |
144 |
player *pl = s->pl; |
145 |
|
146 |
if (pl && pl->ns && !pl->ns->destroyed ()) |
147 |
{ |
148 |
client *ns = pl->ns; |
149 |
|
150 |
/* Update the players stats once per tick. More efficient than |
151 |
* sending them whenever they change, and probably just as useful |
152 |
*/ |
153 |
esrv_update_stats (pl); |
154 |
|
155 |
if (ns->last_weight != -1 && ns->last_weight != WEIGHT (pl->ob)) |
156 |
{ |
157 |
esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
158 |
if (ns->last_weight != WEIGHT (pl->ob)) |
159 |
LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", |
160 |
(unsigned long) ns->last_weight, WEIGHT (pl->ob)); |
161 |
} |
162 |
|
163 |
draw_client_map (pl->ob); |
164 |
|
165 |
if (s->update_look) |
166 |
esrv_draw_look (pl->ob); |
167 |
|
168 |
while (!ns->askface.empty () && ns->outputbuffer_len () < ns->max_rate) |
169 |
{ |
170 |
// use a lifo to send most recently requested images |
171 |
faceidx face = ns->askface.back (); |
172 |
ns->askface.pop_back (); |
173 |
|
174 |
ns->send_image (face); |
175 |
} |
176 |
} |
177 |
|
178 |
s->refcnt_chk (); |
179 |
} |
180 |
} |
181 |
|