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 (©) 2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
6 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
10 | * option) any later version. |
10 | * |
11 | * |
11 | * 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, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | * |
16 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * |
20 | * 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> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | /** |
24 | /** |
24 | * \file |
25 | * \file |
… | |
… | |
64 | tstamp stamp = NOW; |
65 | tstamp stamp = NOW; |
65 | |
66 | |
66 | if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
67 | if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
67 | { |
68 | { |
68 | reset_state (); |
69 | reset_state (); |
69 | send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
70 | send_packet_printf ("msg %d log command queue overflow, ignoring.", NDI_RED); |
70 | } |
71 | } |
71 | else |
72 | else |
72 | { |
73 | { |
73 | cmd_queue.resize (cmd_queue.size () + 1); |
74 | cmd_queue.resize (cmd_queue.size () + 1); |
74 | command &cmd = cmd_queue.back (); |
75 | command &cmd = cmd_queue.back (); |
… | |
… | |
113 | } |
114 | } |
114 | |
115 | |
115 | void |
116 | void |
116 | client::tick () |
117 | client::tick () |
117 | { |
118 | { |
118 | if (!pl || destroyed ()) |
119 | if (destroyed ()) |
119 | return; |
120 | return; |
120 | |
121 | |
|
|
122 | if (pl) |
|
|
123 | { |
121 | pl->dirty = true; |
124 | pl->dirty = true; |
122 | |
125 | |
123 | /* Update the players stats once per tick. More efficient than |
126 | /* Update the players stats once per tick. More efficient than |
124 | * sending them whenever they change, and probably just as useful |
127 | * sending them whenever they change, and probably just as useful |
125 | */ |
128 | */ |
126 | pl->need_updated_stats (); |
129 | pl->need_updated_stats (); |
127 | esrv_update_stats (pl); |
130 | esrv_update_stats (pl); |
128 | |
131 | |
129 | if (pl->ns->update_spells) |
132 | if (pl->ns->update_spells) |
130 | esrv_update_spells (pl); |
133 | esrv_update_spells (pl); |
131 | |
134 | |
132 | sint32 weight = pl->ob->client_weight (); |
135 | sint32 weight = pl->ob->client_weight (); |
133 | |
136 | |
134 | if (last_weight != weight) |
137 | if (last_weight != weight) |
135 | { |
138 | { |
136 | pl->ob->update_stats (); |
139 | pl->ob->update_stats (); |
137 | esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
140 | esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
138 | } |
141 | } |
139 | |
142 | |
140 | draw_client_map (pl); |
143 | draw_client_map (pl); |
141 | |
144 | |
142 | if (update_look) |
145 | if (update_look) |
143 | esrv_draw_look (pl); |
146 | esrv_draw_look (pl); |
144 | |
147 | |
145 | mapinfo_queue_run (); |
148 | mapinfo_queue_run (); |
|
|
149 | } |
146 | |
150 | |
147 | #if HAVE_TCP_INFO |
151 | #if HAVE_TCP_INFO |
148 | // check time of last ack, and, if too old, kill connection |
152 | // check time of last ack, and, if too old, kill connection |
149 | socklen_t len = sizeof (tcpi); |
153 | socklen_t len = sizeof (tcpi); |
150 | |
154 | |
… | |
… | |
201 | { |
205 | { |
202 | ++scrub_idx; |
206 | ++scrub_idx; |
203 | |
207 | |
204 | if (!faces_sent [scrub_idx]) |
208 | if (!faces_sent [scrub_idx]) |
205 | if (faceinfo *f = face_info (scrub_idx)) |
209 | if (faceinfo *f = face_info (scrub_idx)) |
206 | 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 |
207 | { |
211 | { |
208 | send_face (scrub_idx, -120); |
212 | send_face (scrub_idx, -120); |
209 | flush_fx (); |
213 | flush_fx (); |
210 | |
214 | |
211 | 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 |
… | |
… | |
224 | if (avail <= 0) |
228 | if (avail <= 0) |
225 | break; |
229 | break; |
226 | |
230 | |
227 | ixsend &ix = ixface.back (); |
231 | ixsend &ix = ixface.back (); |
228 | |
232 | |
229 | if (facedata *d = face_data (ix.idx, faceset)) |
|
|
230 | { |
|
|
231 | // estimate the packet header overhead "ix " + idx + (new)ofs |
233 | // estimate the packet header overhead "ix " + idx + (new)ofs |
232 | 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); |
233 | int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; |
235 | int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; |
234 | |
236 | |
235 | // only transfer something if the amount of data transferred |
237 | // only transfer something if the amount of data transferred |
236 | // has a healthy relation to the header overhead |
238 | // has a healthy relation to the header overhead |
237 | if (chunk < 64) |
239 | if (chunk < 64) |
238 | break; |
240 | break; |
239 | |
241 | |
240 | chunk = min (chunk, (int)ix.ofs); |
242 | chunk = min (chunk, (int)ix.ofs); |
241 | |
243 | |
242 | ix.ofs -= chunk; |
244 | ix.ofs -= chunk; |
243 | |
245 | |
244 | //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 |
245 | |
247 | |
246 | packet sl ("ix"); |
248 | packet sl ("ix"); |
247 | |
249 | |
248 | sl << ber32 (ix.idx) |
250 | sl << ber32 (ix.idx) |
249 | << ber32 (ix.ofs) |
251 | << ber32 (ix.ofs) |
250 | << data (d->data + ix.ofs, chunk); |
252 | << data_n (ix.data + ix.ofs, chunk); |
251 | |
253 | |
252 | send_packet (sl); |
254 | send_packet (sl); |
253 | } |
|
|
254 | else |
|
|
255 | ix.ofs = 0; |
|
|
256 | |
255 | |
257 | if (!ix.ofs) |
256 | if (!ix.ofs) |
258 | { |
257 | { |
259 | ixface.pop_back (); |
258 | ix_pop (); |
260 | |
259 | |
261 | if (ixface.empty ()) |
260 | if (ixface.empty ()) |
262 | break; |
261 | break; |
263 | } |
262 | } |
264 | } |
263 | } |
… | |
… | |
268 | } |
267 | } |
269 | |
268 | |
270 | void |
269 | void |
271 | client::flush_sockets () |
270 | client::flush_sockets () |
272 | { |
271 | { |
273 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
272 | for (auto &&i : clients) |
274 | (*i)->flush (); |
273 | i->flush (); |
275 | } |
274 | } |
276 | |
275 | |
277 | void |
276 | void |
278 | client::clock () |
277 | client::clock () |
279 | { |
278 | { |
280 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
279 | for (auto &&i : clients) |
281 | (*i)->tick (); |
280 | i->tick (); |
282 | |
281 | |
283 | // give them all the same chances |
282 | // give them all the same chances |
284 | flush_sockets (); |
283 | flush_sockets (); |
285 | |
284 | |
286 | //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... |
287 | for (unsigned i = 0; i < clients.size (); ++i) |
288 | for (unsigned i = 0; i < clients.size (); ++i) |
288 | clients[i]->refcnt_chk (); |
289 | clients[i]->refcnt_chk (); |
289 | } |
290 | } |
290 | |
291 | |