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