| 348 | |
| 349 | |
| 350 | class Mux(Handler): |
| 351 | |
| 352 | def __init__(self, rfile, wfile): |
| 353 | Handler.__init__(self, [rfile, wfile]) |
| 354 | self.rfile = rfile |
| 355 | self.wfile = wfile |
| 356 | self.new_channel = self.got_dns_req = self.got_routes = None |
| 357 | self.got_udp_open = self.got_udp_data = self.got_udp_close = None |
| 358 | self.got_host_req = self.got_host_list = None |
| 359 | self.channels = {} |
| 360 | self.chani = 0 |
| 361 | self.want = 0 |
| 362 | self.inbuf = b('') |
| 363 | self.outbuf = [] |
| 364 | self.fullness = 0 |
| 365 | self.too_full = False |
| 366 | self.send(0, CMD_PING, b('chicken')) |
| 367 | |
| 368 | def next_channel(self): |
| 369 | # channel 0 is special, so we never allocate it |
| 370 | for _ in range(1024): |
| 371 | self.chani += 1 |
| 372 | if self.chani > MAX_CHANNEL: |
| 373 | self.chani = 1 |
| 374 | if not self.channels.get(self.chani): |
| 375 | return self.chani |
| 376 | |
| 377 | def amount_queued(self): |
| 378 | total = 0 |
| 379 | for byte in self.outbuf: |
| 380 | total += len(byte) |
| 381 | return total |
| 382 | |
| 383 | def check_fullness(self): |
| 384 | if self.fullness > LATENCY_BUFFER_SIZE: |
| 385 | if not self.too_full: |
| 386 | self.send(0, CMD_PING, b('rttest')) |
| 387 | self.too_full = True |
| 388 | |
| 389 | def send(self, channel, cmd, data): |
| 390 | assert isinstance(data, bytes) |
| 391 | assert len(data) <= 65535 |
| 392 | p = struct.pack('!ccHHH', b('S'), b('S'), channel, cmd, len(data)) \ |
| 393 | + data |
| 394 | self.outbuf.append(p) |
| 395 | debug2(' > channel=%d cmd=%s len=%d (fullness=%d)' |
| 396 | % (channel, cmd_to_name.get(cmd, hex(cmd)), |
| 397 | len(data), self.fullness)) |
| 398 | # debug3('>>> data: %r' % data) |
| 399 | self.fullness += len(data) |
| 400 | |
| 401 | def got_packet(self, channel, cmd, data): |
| 402 | debug2('< channel=%d cmd=%s len=%d' |
| 403 | % (channel, cmd_to_name.get(cmd, hex(cmd)), len(data))) |
| 404 | # debug3('<<< data: %r' % data) |
| 405 | if cmd == CMD_PING: |
| 406 | self.send(0, CMD_PONG, data) |
| 407 | elif cmd == CMD_PONG: |