ts3phpframework
Loading...
Searching...
No Matches
Transport.php
Go to the documentation of this file.
1<?php
2
3namespace PlanetTeamSpeak\TeamSpeak3Framework\Transport;
4
5use PlanetTeamSpeak\TeamSpeak3Framework\Adapter\Adapter;
6use PlanetTeamSpeak\TeamSpeak3Framework\Exception\TransportException;
7use PlanetTeamSpeak\TeamSpeak3Framework\Helper\Signal;
8use PlanetTeamSpeak\TeamSpeak3Framework\Helper\StringHelper;
9
16abstract class Transport
17{
23 protected array $config;
24
30 protected $stream = null;
31
37 protected $session = null;
38
44 protected ?Adapter $adapter = null;
45
53 public function __construct(array $config)
54 {
55 if (!array_key_exists("host", $config)) {
56 throw new TransportException("config must have a key for 'host' which specifies the server host name");
57 }
58
59 if (!array_key_exists("port", $config)) {
60 throw new TransportException("config must have a key for 'port' which specifies the server port number");
61 }
62
63 if (!array_key_exists("timeout", $config)) {
64 $config["timeout"] = 10;
65 }
66
67 if (!array_key_exists("blocking", $config)) {
68 $config["blocking"] = 1;
69 }
70
71 $this->config = $config;
72 return $this;
73 }
74
80 public function __sleep()
81 {
82 return ["config"];
83 }
84
91 public function __wakeup()
92 {
93 $this->connect();
94 }
95
101 public function __destruct()
102 {
103 if ($this->adapter instanceof Adapter) {
104 $this->adapter->__destruct();
105 }
106
107 $this->disconnect();
108 }
109
116 abstract public function connect(): void;
117
123 abstract public function disconnect(): void;
124
132 abstract public function read(int $length = 4096): StringHelper;
133
140 abstract public function send(string $data): void;
141
147 public function getStream()
148 {
149 return $this->stream;
150 }
151
159 public function getConfig(string $key = null, mixed $default = null): array|string|int
160 {
161 if ($key !== null) {
162 return array_key_exists($key, $this->config) ? $this->config[$key] : $default;
163 }
164
165 return $this->config;
166 }
167
174 public function setAdapter(Adapter $adapter): void
175 {
176 $this->adapter = $adapter;
177 }
178
184 public function getAdapter(): ?Adapter
185 {
186 return $this->adapter;
187 }
188
194 public function getAdapterType(): string
195 {
196 if ($this->adapter instanceof Adapter) {
197 $string = StringHelper::factory(get_class($this->adapter));
198
199 return $string->substr($string->findLast("\\"))->replace(["\\", " "], "")->toString();
200 }
201
202 return "Unknown";
203 }
204
211 public function getMetaData(): array
212 {
213 if ($this->stream === null) {
214 throw new TransportException("unable to retrieve header/meta data from stream pointer");
215 }
216
217 return stream_get_meta_data($this->stream);
218 }
219
225 public function isConnected(): bool
226 {
227 return ($this->getStream() === null) ? false : true;
228 }
229
237 protected function waitForReadyRead(int $time = 0): void
238 {
239 if (!$this->isConnected() || $this->config["blocking"]) {
240 return;
241 }
242
243 do {
244 $read = [$this->stream];
245 $null = null;
246
247 if ($time) {
248 Signal::getInstance()
249 ->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter());
250 }
251
252 $time = $time + $this->config["timeout"];
253 } while (@stream_select($read, $null, $null, $this->config["timeout"]) == 0);
254 }
255}
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Transport\Transport objects.
Abstract class for connecting to a TeamSpeak 3 Server through different ways of transport.
Definition Transport.php:17
getConfig(string $key=null, mixed $default=null)