|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\Docker\Io; |
| 4 | + |
| 5 | +/** |
| 6 | + * Parser for Docker's own frame format used for bidrectional frames |
| 7 | + * |
| 8 | + * Each frame consists of a simple header containing the stream identifier and the payload length |
| 9 | + * plus the actual payload string. |
| 10 | + * |
| 11 | + * @internal |
| 12 | + * @link https://docs.docker.com/engine/reference/api/docker_remote_api_v1.15/#attach-to-a-container |
| 13 | + */ |
| 14 | +class MultiplexStreamParser |
| 15 | +{ |
| 16 | + private $buffer = ''; |
| 17 | + |
| 18 | + /** |
| 19 | + * push the given stream chunk into the parser buffer and try to extract all frames |
| 20 | + * |
| 21 | + * The given $callback parameter will be invoked for each individual frame |
| 22 | + * with the following signature: $callback($stream, $payload) |
| 23 | + * |
| 24 | + * @param string $chunk |
| 25 | + * @param callable $callback |
| 26 | + */ |
| 27 | + public function push($chunk, $callback) |
| 28 | + { |
| 29 | + $this->buffer .= $chunk; |
| 30 | + |
| 31 | + while ($this->buffer !== '') { |
| 32 | + if (!isset($this->buffer[7])) { |
| 33 | + // last header byte not set => no complete header in buffer |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + $header = unpack('Cstream/x/x/x/Nlength', substr($this->buffer, 0, 8)); |
| 38 | + |
| 39 | + if (!isset($this->buffer[7 + $header['length']])) { |
| 40 | + // last payload byte not set => message payload is incomplete |
| 41 | + break; |
| 42 | + } |
| 43 | + |
| 44 | + $payload = substr($this->buffer, 8, $header['length']); |
| 45 | + $this->buffer = (string)substr($this->buffer, 8 + $header['length']); |
| 46 | + |
| 47 | + $callback($header['stream'], $payload); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * checks whether the incoming frame buffer is empty |
| 53 | + * |
| 54 | + * @return boolean |
| 55 | + */ |
| 56 | + public function isEmpty() |
| 57 | + { |
| 58 | + return ($this->buffer === ''); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * creates a new outgoing frame |
| 63 | + * |
| 64 | + * @param int $stream |
| 65 | + * @param string $payload |
| 66 | + * @return string |
| 67 | + */ |
| 68 | + public function createFrame($stream, $payload) |
| 69 | + { |
| 70 | + return pack('CxxxN', $stream, strlen($payload)) . $payload; |
| 71 | + } |
| 72 | +} |
0 commit comments