<?phpnamespace ReactphpX\Bridge;use ReactphpX\Bridge\Interface\MessageComponentInterface;use ReactphpX\Bridge\Tcp\TcpBridgeInterface;use ReactphpX\Bridge\Http\HttpBridgeInterface;use ReactphpX\Bridge\DecodeEncode\TcpDecodeEncode;use ReactphpX\Bridge\DecodeEncode\WebsocketDecodeEncode;use React\Stream\DuplexStreamInterface;use ReactphpX\Bridge\Info;class BridgeStrategy implements MessageComponentInterface{protected $components;protected $streams;public $maxHeaderSize = 8192;public function __construct($components){$this->components = $components;$this->streams = new \SplObjectStorage();}public function onOpen(DuplexStreamInterface $stream, $info = null){$this->streams->attach($stream, new Info(['local_address' => $info['local_address'],'remote_address' => $info['remote_address'],'buffer' => '','component' => null]));}public function onClose(DuplexStreamInterface $stream, $reason = null){if ($this->streams->contains($stream)) {$component = $this->streams[$stream]['component'];$this->streams->detach($stream);if ($component) {$component->onClose($stream, $reason);}}}public function onError(DuplexStreamInterface $stream, \Exception $e){if ($this->streams->contains($stream)) {if ($this->streams[$stream]['component']) {$this->streams[$stream]['component']->onError($stream, $e);}}}public function onMessage(DuplexStreamInterface $stream, $msg){if ($this->streams->contains($stream)) {if ($this->streams[$stream]['component']) {$this->streams[$stream]['component']->onMessage($stream, $msg);} else {$this->streams[$stream]['buffer'] .= $msg;$buffer = $this->streams[$stream]['buffer'];if (strlen($buffer) > $this->maxHeaderSize) {$stream->end();return;}if (strpos($buffer, "\r\n") !== false && strpos($buffer, "HTTP") !== false) {$component = $this->getComponent(HttpBridgeInterface::class);if (!$component) {$stream->end();return;}} else {$component = $this->getComponent(TcpBridgeInterface::class);if (!$component) {$stream->end();return;}}$this->streams[$stream]['component'] = $component;$this->streams[$stream]['component']->onOpen($stream, ['remote_address' => $this->streams[$stream]['remote_address'],'local_address' => $this->streams[$stream]['local_address'],]+ $this->getComponentInfo($component));unset($this->streams[$stream]['buffer']);$this->streams[$stream]['component']->onMessage($stream, $buffer);}}}protected function getComponent($class){foreach ($this->components as $component) {if ($component instanceof $class) {return $component;}}return null;}protected function getComponentInfo($component){$info = [];if ($this->components instanceof \SplObjectStorage) {$info = $this->components[$component];}if ($info) {return $info;}if ($component instanceof TcpBridgeInterface) {return ['decodeEncodeClass' => TcpDecodeEncode::class];} else if ($component instanceof HttpBridgeInterface) {return ['decodeEncodeClass' => WebsocketDecodeEncode::class];}else {throw new \InvalidArgumentException('Unknown component');}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。