public function __construct(DuplexStreamInterface $stream, Protocol $protocol = null, PacketSplitter $splitter = null) { if ($protocol === null) { $protocol = Protocol::createFromProbe(0); } if ($splitter === null) { $splitter = new PacketSplitter(new Binary()); } $this->stream = $stream; $this->protocol = $protocol; $this->splitter = $splitter; $stream->on('data', array($this, 'handleData')); $stream->on('end', array($this, 'handleEnd')); $stream->on('error', array($this, 'handleError')); $stream->on('close', array($this, 'handleClose')); $stream->on('drain', array($this, 'handleDrain')); }
public function createClient($address) { if (strpos($address, '://') === false) { $address = 'tcp://' . $address; } $parts = parse_url($address); if (!$parts || !isset($parts['host'])) { return Promise\reject(new InvalidArgumentException('Given argument "' . $address . '" is not a valid URI')); } if (!isset($parts['port'])) { $parts['port'] = 4242; } // default to automatic probing protocol unless scheme is explicitly given $probe = 0; if (isset($parts['scheme'])) { if ($parts['scheme'] === 'legacy') { $probe = Protocol::TYPE_LEGACY; } elseif ($parts['scheme'] !== 'tcp') { return Promise\reject(new InvalidArgumentException('Given URI scheme "' . $parts['scheme'] . '" is invalid')); } } $promise = $this->connector->create($parts['host'], $parts['port']); // protocol probe not already set if ($probe === 0) { $connector = $this->connector; $prober = $this->prober; $promise = $promise->then(function (Stream $stream) use($prober, &$probe, $connector, $parts) { return $prober->probe($stream)->then(function ($ret) use(&$probe, $stream) { // probe returned successfully, create new client for this stream $probe = $ret; return $stream; }, function ($e) use($connector, $parts) { // probing failed if ($e->getCode() === Prober::ERROR_CLOSED) { // legacy servers will terminate connection while probing // let's just open a new connection and assume default probe return $connector->create($parts['host'], $parts['port']); } throw $e; }); }); } return $promise->then(function (Stream $stream) use(&$probe) { return new Client($stream, Protocol::createFromProbe($probe)); }); }
public function setUp() { $this->protocol = Protocol::createFromProbe(Protocol::TYPE_DATASTREAM); }
public function setUp() { $this->protocol = Protocol::createFromProbe(Protocol::TYPE_LEGACY); }