… | |
… | |
46 | |
46 | |
47 | #include <loader.h> |
47 | #include <loader.h> |
48 | |
48 | |
49 | #define BG_SCRUB_RATE 4 // how often to send a face in the background |
49 | #define BG_SCRUB_RATE 4 // how often to send a face in the background |
50 | |
50 | |
51 | #define MAX_QUEUE_DEPTH 500 |
51 | #define MAX_QUEUE_DEPTH 50 |
52 | #define MAX_QUEUE_BACKLOG 3. |
52 | #define MAX_QUEUE_BACKLOG 3. |
53 | |
53 | |
54 | void |
54 | void |
55 | client::reset_state () |
55 | client::reset_state () |
56 | { |
56 | { |
… | |
… | |
156 | draw_client_map (pl); |
156 | draw_client_map (pl); |
157 | |
157 | |
158 | if (update_look) |
158 | if (update_look) |
159 | esrv_draw_look (pl); |
159 | esrv_draw_look (pl); |
160 | |
160 | |
161 | bool overload = false; |
161 | if (ixface.empty ()) |
|
|
162 | { |
|
|
163 | // regularly send a new face when queue is empty |
|
|
164 | if (bg_scrub && !--bg_scrub && enable_bg_scrub) |
|
|
165 | while (scrub_idx < faces.size () - 1) |
|
|
166 | { |
|
|
167 | ++scrub_idx; |
|
|
168 | |
|
|
169 | if (!faces_sent [scrub_idx]) |
|
|
170 | if (faceinfo *f = face_info (scrub_idx)) |
|
|
171 | if (f->type == FT_FACE) // only scrub faces for now |
|
|
172 | { |
|
|
173 | send_face (scrub_idx, -120); |
|
|
174 | flush_fx (); |
|
|
175 | |
|
|
176 | bg_scrub = 1; // send up to one face per tick, unless an image was requested |
|
|
177 | break; |
|
|
178 | } |
|
|
179 | } |
|
|
180 | |
|
|
181 | rate_avail = max_rate - outputbuffer_len (); |
|
|
182 | } |
|
|
183 | else |
|
|
184 | { |
|
|
185 | int ol = outputbuffer_len (); |
|
|
186 | |
|
|
187 | rate_avail = min (max_rate + mss, rate_avail + max_rate); |
|
|
188 | |
|
|
189 | int avail = rate_avail; |
|
|
190 | |
|
|
191 | // if we can split images, round to next-lowest mss |
|
|
192 | if (fxix) avail -= avail % mss; |
|
|
193 | |
|
|
194 | rate_avail -= ol; |
|
|
195 | avail -= ol; |
162 | |
196 | |
163 | #if HAVE_TCP_INFO |
197 | #if HAVE_TCP_INFO |
164 | // do bandwidth checking |
198 | // further restrict the available bandwidth by the excess bandwidth available |
165 | if (tcpi.tcpi_unacked + 1 > max (4, tcpi.tcpi_snd_cwnd)) |
199 | avail = max (0, min (avail, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss)); |
166 | { |
200 | #endif |
167 | overload = true; |
201 | |
168 | if (next_rate_adjust <= NOW) |
202 | bg_scrub = BG_SCRUB_RATE; |
|
|
203 | |
|
|
204 | while (avail > 0) |
169 | { |
205 | { |
170 | next_rate_adjust = NOW + 2; |
206 | ixsend &ix = ixface.back (); |
171 | max_rate = max (max_rate >> 1, int (5000 * TICK)); |
|
|
172 | LOG (llevDebug, "tcp %s overloaded (%d + 1 > 4 | %d), rate now %d\n", |
|
|
173 | host, (int)tcpi.tcpi_unacked, (int)tcpi.tcpi_snd_cwnd, int (max_rate / TICK));//D |
|
|
174 | send_packet_printf ("drawinfo %d Due to possible network congestion, your output-rate has been reduced to %dbps.", |
|
|
175 | NDI_RED, int (max_rate / TICK)); |
|
|
176 | } |
|
|
177 | } |
|
|
178 | #endif |
|
|
179 | |
207 | |
180 | if (!overload) |
208 | if (facedata *d = face_data (ix.idx, faceset)) |
181 | { |
|
|
182 | if (ixface.empty ()) |
|
|
183 | { |
|
|
184 | // regularly send a new face when queue is empty |
|
|
185 | if (bg_scrub && !--bg_scrub && enable_bg_scrub) |
|
|
186 | while (scrub_idx < faces.size () - 1) |
|
|
187 | { |
|
|
188 | ++scrub_idx; |
|
|
189 | |
|
|
190 | if (!faces_sent [scrub_idx]) |
|
|
191 | if (faceinfo *f = face_info (scrub_idx)) |
|
|
192 | if (f->type == FT_FACE) // only scrub faces for now |
|
|
193 | { |
|
|
194 | send_face (scrub_idx, -120); |
|
|
195 | flush_fx (); |
|
|
196 | |
|
|
197 | bg_scrub = 1; // send up to one face per tick, unless an image was requested |
|
|
198 | break; |
|
|
199 | } |
|
|
200 | } |
|
|
201 | |
|
|
202 | rate_avail = max_rate - outputbuffer_len (); |
|
|
203 | } |
|
|
204 | else |
|
|
205 | { |
|
|
206 | int ol = outputbuffer_len (); |
|
|
207 | |
|
|
208 | rate_avail = min (max_rate, rate_avail + max_rate); |
|
|
209 | rate_avail -= ol; |
|
|
210 | |
|
|
211 | int avail = rate_avail; |
|
|
212 | |
|
|
213 | // if we can split images, transfer up to mss-sized packets if possible |
|
|
214 | // but never 768 bytes more. |
|
|
215 | if (fxix) avail += min (768, mss - (ol % mss)); |
|
|
216 | |
|
|
217 | bg_scrub = BG_SCRUB_RATE; |
|
|
218 | |
|
|
219 | while (avail > 0) |
|
|
220 | { |
209 | { |
221 | ixsend &ix = ixface.back (); |
210 | if (fxix) |
222 | |
|
|
223 | if (facedata *d = face_data (ix.idx, faceset)) |
|
|
224 | { |
211 | { |
225 | if (fxix) |
212 | // estimate the packet header overhead "ix " + idx + (new)ofs |
226 | { |
213 | int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs); |
|
|
214 | int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; |
|
|
215 | |
227 | // only transfer something if the amount of data transferred |
216 | // only transfer something if the amount of data transferred |
228 | // has a healthy relation to the header overhead |
217 | // has a healthy relation to the header overhead |
229 | if (avail <= 40) |
218 | if (chunk < 64) |
230 | break; |
219 | break; |
231 | |
220 | |
232 | // 9 bytes is enough for fx_FFFOOO |
221 | chunk = min (chunk, (int)ix.ofs); |
233 | int chunk = min (min (avail, MAXSOCKBUF - 9), (int)ix.ofs); |
|
|
234 | |
222 | |
235 | if (chunk <= 0) |
|
|
236 | break; |
|
|
237 | |
|
|
238 | ix.ofs -= chunk; |
223 | ix.ofs -= chunk; |
239 | |
224 | |
240 | //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D |
225 | //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D |
241 | |
226 | |
242 | packet sl ("ix"); |
227 | packet sl ("ix"); |
243 | |
228 | |
244 | sl << ber32 (ix.idx) |
229 | sl << ber32 (ix.idx) |
245 | << ber32 (ix.ofs) |
230 | << ber32 (ix.ofs) |
246 | << data (d->data.data () + ix.ofs, chunk); |
231 | << data (d->data.data () + ix.ofs, chunk); |
247 | |
232 | |
248 | send_packet (sl); |
233 | send_packet (sl); |
249 | } |
|
|
250 | else |
|
|
251 | { |
|
|
252 | send_image (ix.idx); |
|
|
253 | ix.ofs = 0; |
|
|
254 | } |
|
|
255 | } |
234 | } |
256 | else |
235 | else |
257 | ix.ofs = 0; |
|
|
258 | |
|
|
259 | int consumed = outputbuffer_len () - ol; |
|
|
260 | |
|
|
261 | avail -= consumed; |
|
|
262 | rate_avail -= consumed; |
|
|
263 | |
|
|
264 | ol = outputbuffer_len (); |
|
|
265 | |
|
|
266 | if (!ix.ofs) |
|
|
267 | { |
236 | { |
268 | ixface.pop_back (); |
237 | send_image (ix.idx); |
269 | |
238 | ix.ofs = 0; |
270 | if (ixface.empty ()) |
|
|
271 | break; |
|
|
272 | } |
239 | } |
273 | } |
240 | } |
|
|
241 | else |
|
|
242 | ix.ofs = 0; |
|
|
243 | |
|
|
244 | int consumed = outputbuffer_len () - ol; |
|
|
245 | |
|
|
246 | avail -= consumed; |
|
|
247 | rate_avail -= consumed; |
|
|
248 | |
|
|
249 | ol = outputbuffer_len (); |
|
|
250 | |
|
|
251 | if (!ix.ofs) |
|
|
252 | { |
|
|
253 | ixface.pop_back (); |
|
|
254 | |
|
|
255 | if (ixface.empty ()) |
|
|
256 | break; |
|
|
257 | } |
274 | } |
258 | } |
275 | } |
259 | } |
276 | } |
260 | } |
277 | |
261 | |