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,2008 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 it under |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
7 | * the terms of the Affero GNU General Public License as published by the |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * 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 Affero GNU General Public License |
16 | * You should have received a copy of the Affero GNU General Public License |
19 | * and the GNU General Public License along with this program. If not, see |
17 | * and the GNU General Public License along with this program. If not, see |
20 | * <http://www.gnu.org/licenses/>. |
18 | * <http://www.gnu.org/licenses/>. |
21 | * |
19 | * |
22 | * 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> |
23 | */ |
21 | */ |
24 | |
22 | |
25 | /** |
23 | /** |
26 | * \file |
24 | * \file |
… | |
… | |
43 | #include <netdb.h> |
41 | #include <netdb.h> |
44 | |
42 | |
45 | #include <unistd.h> |
43 | #include <unistd.h> |
46 | #include <arpa/inet.h> |
44 | #include <arpa/inet.h> |
47 | |
45 | |
48 | #include <loader.h> |
|
|
49 | |
|
|
50 | #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 |
51 | |
47 | |
52 | #define MAX_QUEUE_DEPTH 50 |
48 | #define MAX_QUEUE_DEPTH 50 |
53 | #define MAX_QUEUE_BACKLOG 3. |
49 | #define MAX_QUEUE_BACKLOG 3. |
54 | |
50 | |
… | |
… | |
67 | { |
63 | { |
68 | tstamp stamp = NOW; |
64 | tstamp stamp = NOW; |
69 | |
65 | |
70 | if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
66 | if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
71 | { |
67 | { |
72 | //TODO: just disconnect here? |
|
|
73 | reset_state (); |
68 | reset_state (); |
74 | send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
69 | send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
75 | } |
70 | } |
76 | else |
71 | else |
77 | { |
72 | { |
… | |
… | |
118 | } |
113 | } |
119 | |
114 | |
120 | void |
115 | void |
121 | client::tick () |
116 | client::tick () |
122 | { |
117 | { |
123 | if (!pl || destroyed ()) |
118 | if (destroyed ()) |
124 | return; |
119 | return; |
125 | |
120 | |
|
|
121 | if (pl) |
|
|
122 | { |
126 | pl->dirty = true; |
123 | pl->dirty = true; |
127 | |
124 | |
128 | /* Update the players stats once per tick. More efficient than |
125 | /* Update the players stats once per tick. More efficient than |
129 | * sending them whenever they change, and probably just as useful |
126 | * sending them whenever they change, and probably just as useful |
130 | */ |
127 | */ |
|
|
128 | pl->need_updated_stats (); |
131 | esrv_update_stats (pl); |
129 | esrv_update_stats (pl); |
132 | |
130 | |
|
|
131 | if (pl->ns->update_spells) |
|
|
132 | esrv_update_spells (pl); |
|
|
133 | |
133 | sint32 weight = pl->ob->client_weight (); |
134 | sint32 weight = pl->ob->client_weight (); |
134 | |
135 | |
135 | if (last_weight != weight) |
136 | if (last_weight != weight) |
136 | { |
137 | { |
137 | pl->ob->update_stats (); |
138 | pl->ob->update_stats (); |
138 | esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
139 | esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
139 | } |
140 | } |
140 | |
141 | |
141 | draw_client_map (pl); |
142 | draw_client_map (pl); |
142 | |
143 | |
143 | if (update_look) |
144 | if (update_look) |
144 | esrv_draw_look (pl); |
145 | esrv_draw_look (pl); |
145 | |
146 | |
146 | mapinfo_queue_run (); |
147 | mapinfo_queue_run (); |
|
|
148 | } |
147 | |
149 | |
148 | #if HAVE_TCP_INFO |
150 | #if HAVE_TCP_INFO |
149 | // check time of last ack, and, if too old, kill connection |
151 | // check time of last ack, and, if too old, kill connection |
150 | socklen_t len = sizeof (tcpi); |
152 | socklen_t len = sizeof (tcpi); |
151 | |
153 | |
… | |
… | |
179 | destroy (); |
181 | destroy (); |
180 | } |
182 | } |
181 | } |
183 | } |
182 | #endif |
184 | #endif |
183 | |
185 | |
|
|
186 | // limit budget surplus/deficit by one mss, add per-tick budget |
184 | rate_avail = min (max_rate + mss, rate_avail + max_rate); |
187 | rate_avail = min (rate_avail, mss) + max_rate; |
185 | |
188 | |
186 | int max_send = rate_avail; |
189 | int max_send = rate_avail; |
187 | |
190 | |
188 | #if HAVE_TCP_INFO |
191 | #if HAVE_TCP_INFO |
189 | // further restrict the available bandwidth by the excess bandwidth available |
192 | // further restrict the available bandwidth by the excess bandwidth available |
190 | 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); |
191 | #endif |
194 | #endif |
192 | |
195 | |
193 | // round to next-lowest mss |
196 | // round to next-lowest mss |
194 | max_send -= max_send % mss; |
197 | max_send -= max_send % mss; |
195 | |
198 | |
… | |
… | |
201 | { |
204 | { |
202 | ++scrub_idx; |
205 | ++scrub_idx; |
203 | |
206 | |
204 | if (!faces_sent [scrub_idx]) |
207 | if (!faces_sent [scrub_idx]) |
205 | if (faceinfo *f = face_info (scrub_idx)) |
208 | if (faceinfo *f = face_info (scrub_idx)) |
206 | 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 |
207 | { |
210 | { |
208 | send_face (scrub_idx, -120); |
211 | send_face (scrub_idx, -120); |
209 | flush_fx (); |
212 | flush_fx (); |
210 | |
213 | |
211 | 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 |
… | |
… | |
224 | if (avail <= 0) |
227 | if (avail <= 0) |
225 | break; |
228 | break; |
226 | |
229 | |
227 | ixsend &ix = ixface.back (); |
230 | ixsend &ix = ixface.back (); |
228 | |
231 | |
229 | if (facedata *d = face_data (ix.idx, faceset)) |
|
|
230 | { |
|
|
231 | // estimate the packet header overhead "ix " + idx + (new)ofs |
232 | // estimate the packet header overhead "ix " + idx + (new)ofs |
232 | 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); |
233 | int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; |
234 | int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; |
234 | |
235 | |
235 | // only transfer something if the amount of data transferred |
236 | // only transfer something if the amount of data transferred |
236 | // has a healthy relation to the header overhead |
237 | // has a healthy relation to the header overhead |
237 | if (chunk < 64) |
238 | if (chunk < 64) |
238 | break; |
239 | break; |
239 | |
240 | |
240 | chunk = min (chunk, (int)ix.ofs); |
241 | chunk = min (chunk, (int)ix.ofs); |
241 | |
242 | |
242 | ix.ofs -= chunk; |
243 | ix.ofs -= chunk; |
243 | |
244 | |
244 | //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 |
245 | |
246 | |
246 | packet sl ("ix"); |
247 | packet sl ("ix"); |
247 | |
248 | |
248 | sl << ber32 (ix.idx) |
249 | sl << ber32 (ix.idx) |
249 | << ber32 (ix.ofs) |
250 | << ber32 (ix.ofs) |
250 | << data (d->data.data () + ix.ofs, chunk); |
251 | << data (ix.data + ix.ofs, chunk); |
251 | |
252 | |
252 | send_packet (sl); |
253 | send_packet (sl); |
253 | } |
|
|
254 | else |
|
|
255 | ix.ofs = 0; |
|
|
256 | |
254 | |
257 | if (!ix.ofs) |
255 | if (!ix.ofs) |
258 | { |
256 | { |
259 | ixface.pop_back (); |
257 | ix_pop (); |
260 | |
258 | |
261 | if (ixface.empty ()) |
259 | if (ixface.empty ()) |
262 | break; |
260 | break; |
263 | } |
261 | } |
264 | } |
262 | } |
… | |
… | |
266 | |
264 | |
267 | rate_avail -= outputbuffer_len (); |
265 | rate_avail -= outputbuffer_len (); |
268 | } |
266 | } |
269 | |
267 | |
270 | void |
268 | void |
271 | client::flush_sockets (void) |
269 | client::flush_sockets () |
272 | { |
270 | { |
273 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
271 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
274 | (*i)->flush (); |
272 | (*i)->flush (); |
275 | } |
273 | } |
276 | |
274 | |
277 | void |
275 | void |
278 | client::clock (void) |
276 | client::clock () |
279 | { |
277 | { |
280 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
278 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
281 | (*i)->tick (); |
279 | (*i)->tick (); |
282 | |
280 | |
283 | // give them all the same chances |
281 | // give them all the same chances |