diff --git a/TcpClient.php b/TcpClient.php new file mode 100644 index 0000000..11e5c1e --- /dev/null +++ b/TcpClient.php @@ -0,0 +1,87 @@ +reconnect(); + } + + + /** + * @return void + * @throws Exception + */ + public function reconnect(): void + { + $this->client?->close(); + if (Context::inCoroutine()) { + $this->client = new CoroutineClient($this->socket); + } else { + $this->client = new AsyncClient($this->socket); + } + if (!$this->client->connect($this->host, $this->port, 1)) { + throw new Exception('Connect ' . $this->host . '::' . $this->port . ' fail'); + } + } + + + /** + * @param string $data + * @return int|bool + */ + public function send(string $data): int|bool + { + return $this->client->send($data); + } + + + /** + * @return bool|string + */ + public function read(): bool|string + { + return $this->client->recv(); + } + + + /** + * @return bool + */ + public function isConnected(): bool + { + return $this->client->isConnected(); + } + + + /** + * @return bool + */ + public function close(): bool + { + return $this->client->close(); + } + +} \ No newline at end of file