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