ts3phpframework
Loading...
Searching...
No Matches
TCP.php
Go to the documentation of this file.
1<?php
2
3namespace PlanetTeamSpeak\TeamSpeak3Framework\Transport;
4
5use PlanetTeamSpeak\TeamSpeak3Framework\Exception\ServerQueryException;
6use PlanetTeamSpeak\TeamSpeak3Framework\Exception\TransportException;
7use PlanetTeamSpeak\TeamSpeak3Framework\Helper\Signal;
8use PlanetTeamSpeak\TeamSpeak3Framework\Helper\StringHelper;
9
16class TCP extends Transport
17{
25 public function connect(): void
26 {
27 if ($this->stream !== null) {
28 return;
29 }
30
31 $host = strval($this->config["host"]);
32 $port = strval($this->config["port"]);
33 $timeout = intval($this->config["timeout"]);
34 $blocking = intval($this->config["blocking"]);
35
36 if (empty($this->config["ssh"])) {
37 $address = "tcp://" . (str_contains($host, ":") ? "[" . $host . "]" : $host) . ":" . $port;
38 $options = empty($this->config["tls"]) ? [] : ["ssl" => ["allow_self_signed" => true, "verify_peer" => false, "verify_peer_name" => false]];
39
40 $this->stream = @stream_socket_client($address, $errno, $errstr, $this->config["timeout"], STREAM_CLIENT_CONNECT, stream_context_create($options));
41
42 if ($this->stream === false) {
43 throw new TransportException(StringHelper::factory($errstr)->toUtf8()->toString(), $errno);
44 }
45
46 if (!empty($this->config["tls"])) {
47 stream_socket_enable_crypto($this->stream, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
48 }
49 } else {
50 $this->session = @ssh2_connect($host, $port);
51
52 if ($this->session === false) {
53 throw new TransportException("failed to establish secure shell connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "'");
54 }
55
56 if (!@ssh2_auth_password($this->session, $this->config["username"], $this->config["password"])) {
57 throw new ServerQueryException("invalid loginname or password", 0x208);
58 }
59
60 $this->stream = @ssh2_shell($this->session, "raw");
61
62 if ($this->stream === false) {
63 throw new TransportException("failed to open a secure shell on server '" . $this->config["host"] . ":" . $this->config["port"] . "'");
64 }
65 }
66
67 @stream_set_timeout($this->stream, $timeout);
68 @stream_set_blocking($this->stream, $blocking ? 1 : 0);
69 }
70
76 public function disconnect(): void
77 {
78 if ($this->stream === null) {
79 return;
80 }
81
82 $this->stream = null;
83
84 if (is_resource($this->session)) {
85 @ssh2_disconnect($this->session);
86 }
87
88 Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "Disconnected");
89 }
90
99 public function read(int $length = 4096): StringHelper
100 {
101 $this->connect();
102 $this->waitForReadyRead();
103
104 $data = @stream_get_contents($this->stream, $length);
105
106 Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
107
108 if ($data === false) {
109 throw new TransportException("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
110 }
111
112 return new StringHelper($data);
113 }
114
123 public function readLine(string $token = "\n"): StringHelper
124 {
125 $this->connect();
126
127 $line = StringHelper::factory("");
128
129 while (!$line->endsWith($token)) {
130 $this->waitForReadyRead();
131
132 $data = @fgets($this->stream, 4096);
133
134 Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
135
136 if ($data === false) {
137 if ($line->count()) {
138 $line->append($token);
139 }
140 } else {
141 $line->append($data);
142 }
143 }
144
145 return $line->trim();
146 }
147
156 public function send(string $data): void
157 {
158 $this->connect();
159
160 @fwrite($this->stream, $data);
161
162 Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data);
163 }
164
174 public function sendLine(string $data, string $separator = "\n"): void
175 {
176 $size = strlen($data);
177 $pack = 4096;
178
179 for ($seek = 0; $seek < $size;) {
180 $rest = $size - $seek;
181 $pack = min($rest, $pack);
182 $buff = substr($data, $seek, $pack);
183 $seek = $seek + $pack;
184
185 if ($seek >= $size) {
186 $buff .= $separator;
187 }
188
189 $this->send($buff);
190 }
191 }
192}
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Adapter\ServerQuery objects.
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Transport\Transport objects.
Class for connecting to a remote server through TCP.
Definition TCP.php:17
sendLine(string $data, string $separator="\n")
Definition TCP.php:174
Abstract class for connecting to a TeamSpeak 3 Server through different ways of transport.
Definition Transport.php:17