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 | /** |
… | |
… | |
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 | |
|
|
57 | void |
51 | void |
58 | client::reset_state () |
52 | client::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 ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
77 | } |
70 | } |
78 | else |
71 | else |
79 | { |
72 | { |
… | |
… | |
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); |
… | |
… | |
120 | pl->dirty = true; |
121 | pl->dirty = true; |
121 | |
122 | |
122 | /* Update the players stats once per tick. More efficient than |
123 | /* Update the players stats once per tick. More efficient than |
123 | * sending them whenever they change, and probably just as useful |
124 | * sending them whenever they change, and probably just as useful |
124 | */ |
125 | */ |
|
|
126 | pl->need_updated_stats (); |
125 | esrv_update_stats (pl); |
127 | esrv_update_stats (pl); |
|
|
128 | |
|
|
129 | if (pl->ns->update_spells) |
|
|
130 | esrv_update_spells (pl); |
126 | |
131 | |
127 | sint32 weight = pl->ob->client_weight (); |
132 | sint32 weight = pl->ob->client_weight (); |
128 | |
133 | |
129 | if (last_weight != weight) |
134 | if (last_weight != weight) |
130 | { |
135 | { |
… | |
… | |
158 | tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss, |
163 | tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss, |
159 | |
164 | |
160 | tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked)); |
165 | tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked)); |
161 | #endif |
166 | #endif |
162 | |
167 | |
163 | // fast-time-out a player by checking for missign acks |
168 | // fast-time-out a player by checking for missing acks |
164 | // do this only when player is active |
169 | // do this only when player is active |
165 | if (pl && pl->active |
170 | if (pl && pl->active |
166 | && tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) |
171 | && tcpi.tcpi_last_ack_recv > int (socket_timeout * 1000)) |
167 | { |
172 | { |
168 | send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
173 | send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
169 | write_outputbuffer (); |
174 | write_outputbuffer (); |
170 | |
175 | |
171 | LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
176 | LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
… | |
… | |
173 | destroy (); |
178 | destroy (); |
174 | } |
179 | } |
175 | } |
180 | } |
176 | #endif |
181 | #endif |
177 | |
182 | |
|
|
183 | // limit budget surplus/deficit by one mss, add per-tick budget |
178 | rate_avail = min (max_rate + mss, rate_avail + max_rate); |
184 | rate_avail = min (rate_avail, mss) + max_rate; |
179 | |
185 | |
180 | int max_send = rate_avail; |
186 | int max_send = rate_avail; |
181 | |
187 | |
182 | #if HAVE_TCP_INFO |
188 | #if HAVE_TCP_INFO |
183 | // further restrict the available bandwidth by the excess bandwidth available |
189 | // further restrict the available bandwidth by the excess bandwidth available |
184 | max_send = min (max_send, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss); |
190 | min_it (max_send, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss); |
185 | #endif |
191 | #endif |
186 | |
192 | |
187 | // if we can split images, round to next-lowest mss |
193 | // round to next-lowest mss |
188 | if (fxix) max_send -= max_send % mss; |
194 | max_send -= max_send % mss; |
189 | |
195 | |
190 | if (ixface.empty ()) |
196 | if (ixface.empty ()) |
191 | { |
197 | { |
192 | // regularly send a new face when queue is empty |
198 | // regularly send a new face when queue is empty |
193 | if (bg_scrub && !--bg_scrub && enable_bg_scrub) |
199 | if (bg_scrub && !--bg_scrub) |
194 | while (scrub_idx < faces.size () - 1) |
200 | while (scrub_idx < faces.size () - 1) |
195 | { |
201 | { |
196 | ++scrub_idx; |
202 | ++scrub_idx; |
197 | |
203 | |
198 | if (!faces_sent [scrub_idx]) |
204 | if (!faces_sent [scrub_idx]) |
… | |
… | |
220 | |
226 | |
221 | ixsend &ix = ixface.back (); |
227 | ixsend &ix = ixface.back (); |
222 | |
228 | |
223 | if (facedata *d = face_data (ix.idx, faceset)) |
229 | if (facedata *d = face_data (ix.idx, faceset)) |
224 | { |
230 | { |
225 | if (fxix) |
|
|
226 | { |
|
|
227 | // estimate the packet header overhead "ix " + idx + (new)ofs |
231 | // estimate the packet header overhead "ix " + idx + (new)ofs |
228 | int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs); |
232 | int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs); |
229 | int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; |
233 | int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; |
230 | |
234 | |
231 | // only transfer something if the amount of data transferred |
235 | // only transfer something if the amount of data transferred |
232 | // has a healthy relation to the header overhead |
236 | // has a healthy relation to the header overhead |
233 | if (chunk < 64) |
237 | if (chunk < 64) |
234 | break; |
238 | break; |
235 | |
239 | |
236 | chunk = min (chunk, (int)ix.ofs); |
240 | chunk = min (chunk, (int)ix.ofs); |
237 | |
241 | |
238 | ix.ofs -= chunk; |
242 | ix.ofs -= chunk; |
239 | |
243 | |
240 | //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D |
244 | //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D |
241 | |
245 | |
242 | packet sl ("ix"); |
246 | packet sl ("ix"); |
243 | |
247 | |
244 | sl << ber32 (ix.idx) |
248 | sl << ber32 (ix.idx) |
245 | << ber32 (ix.ofs) |
249 | << ber32 (ix.ofs) |
246 | << data (d->data.data () + ix.ofs, chunk); |
250 | << data (d->data + ix.ofs, chunk); |
247 | |
251 | |
248 | send_packet (sl); |
252 | send_packet (sl); |
249 | } |
|
|
250 | else |
|
|
251 | { |
|
|
252 | send_image (ix.idx); |
|
|
253 | ix.ofs = 0; |
|
|
254 | } |
|
|
255 | } |
253 | } |
256 | else |
254 | else |
257 | ix.ofs = 0; |
255 | ix.ofs = 0; |
258 | |
256 | |
259 | if (!ix.ofs) |
257 | if (!ix.ofs) |
… | |
… | |
268 | |
266 | |
269 | rate_avail -= outputbuffer_len (); |
267 | rate_avail -= outputbuffer_len (); |
270 | } |
268 | } |
271 | |
269 | |
272 | void |
270 | void |
273 | client::flush_sockets (void) |
271 | client::flush_sockets () |
274 | { |
272 | { |
275 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
273 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
276 | (*i)->flush (); |
274 | (*i)->flush (); |
277 | } |
275 | } |
278 | |
276 | |
279 | void |
277 | void |
280 | client::clock (void) |
278 | client::clock () |
281 | { |
279 | { |
282 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
280 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
283 | (*i)->tick (); |
281 | (*i)->tick (); |
284 | |
282 | |
285 | // give them all the same chances |
283 | // give them all the same chances |