ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/loop.C
(Generate patch)

Comparing deliantra/server/socket/loop.C (file contents):
Revision 1.74 by root, Tue Apr 22 07:28:05 2008 UTC vs.
Revision 1.99 by root, Sun Nov 18 09:29:25 2012 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002-2003,2007 Mark Wedel & The Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 5 *
8 * Deliantra is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 9 * option) any later version.
12 * 10 *
13 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 14 * GNU General Public License for more details.
17 * 15 *
18 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
20 * 19 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 20 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 21 */
23 22
24/** 23/**
25 * \file 24 * \file
42#include <netdb.h> 41#include <netdb.h>
43 42
44#include <unistd.h> 43#include <unistd.h>
45#include <arpa/inet.h> 44#include <arpa/inet.h>
46 45
47#include <loader.h>
48
49#define BG_SCRUB_RATE 4 // how often to send a face in the background 46#define BG_SCRUB_RATE 4 // how often to send a face in the background
50 47
51#define MAX_QUEUE_DEPTH 50 48#define MAX_QUEUE_DEPTH 50
52#define MAX_QUEUE_BACKLOG 3. 49#define MAX_QUEUE_BACKLOG 3.
53 50
54// disconnect a socket after this many seconds without an ack
55#define SOCKET_TIMEOUT 16.
56
57void 51void
58client::reset_state () 52client::reset_state ()
59{ 53{
60 if (!pl) 54 if (!pl)
61 return; 55 return;
69{ 63{
70 tstamp stamp = NOW; 64 tstamp stamp = NOW;
71 65
72 if (cmd_queue.size () >= MAX_QUEUE_DEPTH) 66 if (cmd_queue.size () >= MAX_QUEUE_DEPTH)
73 { 67 {
74 //TODO: just disconnect here?
75 reset_state (); 68 reset_state ();
76 send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); 69 send_packet_printf ("msg %d log command queue overflow, ignoring.", NDI_RED);
77 } 70 }
78 else 71 else
79 { 72 {
80 cmd_queue.resize (cmd_queue.size () + 1); 73 cmd_queue.resize (cmd_queue.size () + 1);
81 command &cmd = cmd_queue.back (); 74 command &cmd = cmd_queue.back ();
95 { 88 {
96 command &cmd = cmd_queue.front (); 89 command &cmd = cmd_queue.front ();
97 90
98 if (cmd.stamp + MAX_QUEUE_BACKLOG < NOW) 91 if (cmd.stamp + MAX_QUEUE_BACKLOG < NOW)
99 { 92 {
100 reset_state (); 93 reset_state (); // the command might actually reset some movement state etc.
101 send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); 94
95 if (pl)
96 pl->failmsg (
97 "Cannot keep up with your commands, ignoring them! "
98 "H<Your character cannot keep up with the instructions you give G<him|her>. "
99 "Try issuing commands slower, or, if G<he|she> is incapacitated (paralyzed and so on), "
100 "wait till your character can act again.\n\nIf G<he|she> is permanently stuck, then "
101 "try the B<suicide> command (or use B<chat> to ask somebody to B<invite> you out).>"
102 );
102 } 103 }
103 else 104 else
104 execute (cmd.handler, cmd.data, cmd.datalen); 105 execute (cmd.handler, cmd.data, cmd.datalen);
105 106
106 sfree<char> (cmd.data, cmd.datalen + 1); 107 sfree<char> (cmd.data, cmd.datalen + 1);
112} 113}
113 114
114void 115void
115client::tick () 116client::tick ()
116{ 117{
117 if (!pl || destroyed ()) 118 if (destroyed ())
118 return; 119 return;
119 120
121 if (pl)
122 {
123 pl->dirty = true;
124
120 /* Update the players stats once per tick. More efficient than 125 /* Update the players stats once per tick. More efficient than
121 * sending them whenever they change, and probably just as useful 126 * sending them whenever they change, and probably just as useful
122 */ 127 */
128 pl->need_updated_stats ();
123 esrv_update_stats (pl); 129 esrv_update_stats (pl);
124 130
131 if (pl->ns->update_spells)
132 esrv_update_spells (pl);
133
125 sint32 weight = pl->ob->client_weight (); 134 sint32 weight = pl->ob->client_weight ();
126 135
127 if (last_weight != weight) 136 if (last_weight != weight)
137 {
138 pl->ob->update_stats ();
128 esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); 139 esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob);
140 }
129 141
130 draw_client_map (pl); 142 draw_client_map (pl);
131 143
132 if (update_look) 144 if (update_look)
133 esrv_draw_look (pl); 145 esrv_draw_look (pl);
134 146
135 mapinfo_queue_run (); 147 mapinfo_queue_run ();
148 }
136 149
137#if HAVE_TCP_INFO 150#if HAVE_TCP_INFO
138 // check time of last ack, and, if too old, kill connection 151 // check time of last ack, and, if too old, kill connection
139 socklen_t len = sizeof (tcpi); 152 socklen_t len = sizeof (tcpi);
140 153
153 tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss, 166 tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss,
154 167
155 tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked)); 168 tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked));
156#endif 169#endif
157 170
158 // fast-time-out a player by checking for missign acks 171 // fast-time-out a player by checking for missing acks
159 // do this only when player is active 172 // do this only when player is active
160 if (pl && pl->active 173 if (pl && pl->active
161 && tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) 174 && tcpi.tcpi_last_ack_recv > int (socket_timeout * 1000))
162 { 175 {
163 send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); 176 send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)");
164 write_outputbuffer (); 177 write_outputbuffer ();
165 178
166 LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, 179 LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd,
168 destroy (); 181 destroy ();
169 } 182 }
170 } 183 }
171#endif 184#endif
172 185
186 // limit budget surplus/deficit by one mss, add per-tick budget
173 rate_avail = min (max_rate + mss, rate_avail + max_rate); 187 rate_avail = min (rate_avail, mss) + max_rate;
174 188
175 int max_send = rate_avail; 189 int max_send = rate_avail;
176 190
177#if HAVE_TCP_INFO 191#if HAVE_TCP_INFO
178 // further restrict the available bandwidth by the excess bandwidth available 192 // further restrict the available bandwidth by the excess bandwidth available
179 max_send = min (max_send, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss); 193 min_it (max_send, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss);
180#endif 194#endif
181 195
182 // if we can split images, round to next-lowest mss 196 // round to next-lowest mss
183 if (fxix) max_send -= max_send % mss; 197 max_send -= max_send % mss;
184 198
185 if (ixface.empty ()) 199 if (ixface.empty ())
186 { 200 {
187 // regularly send a new face when queue is empty 201 // regularly send a new face when queue is empty
188 if (bg_scrub && !--bg_scrub && enable_bg_scrub) 202 if (bg_scrub && !--bg_scrub)
189 while (scrub_idx < faces.size () - 1) 203 while (scrub_idx < faces.size () - 1)
190 { 204 {
191 ++scrub_idx; 205 ++scrub_idx;
192 206
193 if (!faces_sent [scrub_idx]) 207 if (!faces_sent [scrub_idx])
194 if (faceinfo *f = face_info (scrub_idx)) 208 if (faceinfo *f = face_info (scrub_idx))
195 if (f->type == FT_FACE || f->type == FT_SOUND) // only scrub faces and sounds for now 209 if (f->type == FT_IMAGE || f->type == FT_SOUND) // only scrub faces and sounds for now
196 { 210 {
197 send_face (scrub_idx, -120); 211 send_face (scrub_idx, -120);
198 flush_fx (); 212 flush_fx ();
199 213
200 bg_scrub = 1; // send up to one fx per tick, unless an image was requested 214 bg_scrub = 1; // send up to one fx per tick, unless an image was requested
213 if (avail <= 0) 227 if (avail <= 0)
214 break; 228 break;
215 229
216 ixsend &ix = ixface.back (); 230 ixsend &ix = ixface.back ();
217 231
218 if (facedata *d = face_data (ix.idx, faceset))
219 {
220 if (fxix)
221 {
222 // estimate the packet header overhead "ix " + idx + (new)ofs 232 // estimate the packet header overhead "ix " + idx + (new)ofs
223 int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs); 233 int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs);
224 int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; 234 int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen;
225 235
226 // only transfer something if the amount of data transferred 236 // only transfer something if the amount of data transferred
227 // has a healthy relation to the header overhead 237 // has a healthy relation to the header overhead
228 if (chunk < 64) 238 if (chunk < 64)
229 break; 239 break;
230 240
231 chunk = min (chunk, (int)ix.ofs); 241 chunk = min (chunk, (int)ix.ofs);
232 242
233 ix.ofs -= chunk; 243 ix.ofs -= chunk;
234 244
235 //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D 245 //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D
236 246
237 packet sl ("ix"); 247 packet sl ("ix");
238 248
239 sl << ber32 (ix.idx) 249 sl << ber32 (ix.idx)
240 << ber32 (ix.ofs) 250 << ber32 (ix.ofs)
241 << data (d->data.data () + ix.ofs, chunk); 251 << data (ix.data + ix.ofs, chunk);
242 252
243 send_packet (sl); 253 send_packet (sl);
244 }
245 else
246 {
247 send_image (ix.idx);
248 ix.ofs = 0;
249 }
250 }
251 else
252 ix.ofs = 0;
253 254
254 if (!ix.ofs) 255 if (!ix.ofs)
255 { 256 {
256 ixface.pop_back (); 257 ix_pop ();
257 258
258 if (ixface.empty ()) 259 if (ixface.empty ())
259 break; 260 break;
260 } 261 }
261 } 262 }
263 264
264 rate_avail -= outputbuffer_len (); 265 rate_avail -= outputbuffer_len ();
265} 266}
266 267
267void 268void
268client::flush_sockets (void) 269client::flush_sockets ()
269{ 270{
270 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) 271 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
271 (*i)->flush (); 272 (*i)->flush ();
272} 273}
273 274
274void 275void
275client::clock (void) 276client::clock ()
276{ 277{
277 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) 278 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
278 (*i)->tick (); 279 (*i)->tick ();
279 280
280 // give them all the same chances 281 // give them all the same chances

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines