… | |
… | |
200 | { |
200 | { |
201 | return (!(pkt->flags & PF_PLAYER) || pl) |
201 | return (!(pkt->flags & PF_PLAYER) || pl) |
202 | && (!(pkt->flags & PF_PLAYING) || state == ST_PLAYING); |
202 | && (!(pkt->flags & PF_PLAYING) || state == ST_PLAYING); |
203 | } |
203 | } |
204 | |
204 | |
205 | // HACK: some commands currently should be executed |
205 | // HACK: some commands currently should be executed |
206 | // even when the player is frozen. this hack detects |
206 | // even when the player is frozen. this hack detects |
207 | // those commands. it should be folded into may_execute, |
207 | // those commands. it should be folded into may_execute, |
208 | // but kept seperate to emphasise the hack aspect, i.e. |
208 | // but kept seperate to emphasise the hack aspect, i.e. |
209 | // do it better, then remove. |
209 | // do it better, then remove. |
210 | static bool |
210 | static bool |
… | |
… | |
357 | return; |
357 | return; |
358 | } |
358 | } |
359 | |
359 | |
360 | if (ws_version) |
360 | if (ws_version) |
361 | { |
361 | { |
362 | if (ws_inbuf_len + 4096 > ws_inbuf_alloc) |
362 | if (ws_inbuf_len + 2048 > ws_inbuf_alloc) |
363 | ws_inbuf = (uint8 *)realloc (ws_inbuf, ws_inbuf_alloc += 4096); |
363 | ws_inbuf = (uint8 *)realloc (ws_inbuf, ws_inbuf_alloc += 4096); |
364 | |
364 | |
365 | int len = read (fd, ws_inbuf + ws_inbuf_len, ws_inbuf_alloc - ws_inbuf_len); |
365 | int len = read (fd, ws_inbuf + ws_inbuf_len, ws_inbuf_alloc - ws_inbuf_len); |
366 | |
366 | |
367 | if (len > 0) |
367 | if (len > 0) |
… | |
… | |
421 | memmove (ws_inbuf, ws_inbuf + d + 4 + l, ws_inbuf_len); |
421 | memmove (ws_inbuf, ws_inbuf + d + 4 + l, ws_inbuf_len); |
422 | |
422 | |
423 | switch (o) |
423 | switch (o) |
424 | { |
424 | { |
425 | case 1: // utf-8 |
425 | case 1: // utf-8 |
426 | //TODO |
426 | { |
|
|
427 | uint8 *a = inbuf + inbuf_len; |
|
|
428 | uint8 *b = a; |
|
|
429 | uint8 *c = a + l; |
|
|
430 | |
|
|
431 | for (; a < c; ++a, ++b) |
|
|
432 | { |
|
|
433 | *b = *a; |
|
|
434 | |
|
|
435 | if (*a >= 0x80) |
|
|
436 | *b = (a [0] & 0x1f) << 6 | (a [1] & 0x3f), ++a; |
|
|
437 | } |
|
|
438 | |
|
|
439 | l -= a - b; |
|
|
440 | } |
427 | break; |
441 | break; |
428 | case 2: // binary |
442 | case 2: // binary |
429 | break; |
443 | break; |
430 | |
444 | |
431 | case 9: // ping |
445 | case 9: // ping |
… | |
… | |
444 | case 8: // close |
458 | case 8: // close |
445 | default: |
459 | default: |
446 | destroy (); |
460 | destroy (); |
447 | return; |
461 | return; |
448 | } |
462 | } |
449 | |
463 | |
450 | amount = l; |
464 | amount = l; |
451 | } |
465 | } |
452 | else |
466 | else |
453 | amount = -1; |
467 | amount = -1; |
454 | } |
468 | } |
… | |
… | |
554 | if (!sl.length ()) |
568 | if (!sl.length ()) |
555 | return; |
569 | return; |
556 | |
570 | |
557 | if (ws_version == 8) |
571 | if (ws_version == 8) |
558 | { |
572 | { |
559 | uint8 hdr [4] = { 0x81, 126, sl.length () >> 8, sl.length () }; |
573 | static uint8 buf [MAXSOCKBUF * 2 + 4]; |
560 | // TODO: utf-8 encoding |
574 | |
561 | send (hdr, 4); |
575 | uint8 *b = buf + 4; |
562 | send (sl.buf_ + sl.hdrlen, sl.cur - sl.buf_ - sl.hdrlen); |
576 | for (uint8 *a = sl.buf_ + sl.hdrlen; a < sl.cur; ++a) |
|
|
577 | { |
|
|
578 | if (*a < 0x80) |
|
|
579 | *b++ = *a; |
|
|
580 | else |
|
|
581 | { |
|
|
582 | *b++ = 0xc0 | ((*a >> 6) & 0x1f); |
|
|
583 | *b++ = 0x80 | ( *a & 0x3f); |
|
|
584 | } |
|
|
585 | } |
|
|
586 | |
|
|
587 | assert (b - buf < sizeof (buf)); |
|
|
588 | |
|
|
589 | int l = b - (buf + 4); |
|
|
590 | |
|
|
591 | if (l < 126) |
|
|
592 | { |
|
|
593 | buf [2] = 0x81; |
|
|
594 | buf [3] = l; |
|
|
595 | |
|
|
596 | send (buf + 2, l + 2); |
|
|
597 | } |
|
|
598 | else |
|
|
599 | { |
|
|
600 | buf [0] = 0x81; |
|
|
601 | buf [1] = 126; |
|
|
602 | buf [2] = l >> 8; |
|
|
603 | buf [3] = l; |
|
|
604 | |
|
|
605 | send (buf, l + 4); |
|
|
606 | } |
563 | } |
607 | } |
564 | else if (ws_version == 13) |
608 | else if (ws_version == 13) |
565 | { |
609 | { |
|
|
610 | int l = sl.length (); |
|
|
611 | |
|
|
612 | if (l < 126) |
|
|
613 | { |
566 | uint8 hdr [4] = { 0x82, 126, sl.length () >> 8, sl.length () }; |
614 | uint8 hdr [] = { 0x82, 126, l >> 8, l }; |
567 | send (hdr, 4); |
615 | send (hdr, sizeof (hdr)); |
568 | send (sl.buf_ + sl.hdrlen, sl.cur - sl.buf_ - sl.hdrlen); |
616 | } |
|
|
617 | else |
|
|
618 | { |
|
|
619 | uint8 hdr [] = { 0x82, l }; |
|
|
620 | send (hdr, sizeof (hdr)); |
|
|
621 | } |
|
|
622 | |
|
|
623 | send (sl.buf_ + sl.hdrlen, l); |
569 | } |
624 | } |
570 | else |
625 | else |
571 | { |
626 | { |
572 | assert (sl.hdrlen == 2); |
627 | assert (sl.hdrlen == 2); |
573 | |
628 | |