ts3phpframework
Loading...
Searching...
No Matches
MockTCP.php
Go to the documentation of this file.
1<?php
2
4
8
9class MockTCP extends TCP
10{
11 public const S_WELCOME_L0 = 'TS3';
12 public const S_WELCOME_L1 = 'Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.';
13 public const S_ERROR_OK = 'error id=0 msg=ok';
14
15 public const CMD = [
16 'login serveradmin secret' => self::S_ERROR_OK,
17 'login client_login_name=serveradmin client_login_password=secret' => self::S_ERROR_OK,
18 ];
19
20 protected mixed $reply = null;
21
22 public function connect(): void
23 {
24 if ($this->stream !== null) {
25 return;
26 }
27
28 $this->reply = sprintf("%s\n%s\n", self::S_WELCOME_L0, self::S_WELCOME_L1);
29 $this->stream = true;
30 }
31
32 public function readLine(string $token = "\n"): StringHelper
33 {
34 $line = StringHelper::factory("");
35 $this->connect();
36
37 while (!$line->endsWith($token)) {
38 // $this->waitForReadyRead();
39
40 $data = $this->fget();
41 Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
42 if (!$data) {
43 if ($line->count()) {
44 $line->append($token);
45 } else {
46 throw new TransportException("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
47 }
48 } else {
49 $line->append($data);
50 }
51 }
52
53 return $line->trim();
54 }
55
56 public function sendLine(string $data, string $separator = "\n"): void
57 {
58 $this->send($data);
59 }
60
67 public function send(string $data): void
68 {
69 $this->fwrite($data);
70 Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data);
71 }
72
73 protected function fget(): string
74 {
75 $this->reply = explode("\n", $this->reply);
76 $reply = array_shift($this->reply);
77 $this->reply = join("\n", $this->reply);
78 return $reply . "\n";
79 }
80
81 protected function fwrite($data)
82 {
83 if (!key_exists($data, self::CMD)) {
84 $this->reply = "error id=1 msg=Unkown\n";
85 return;
86 }
87
88 $this->reply = sprintf("%s\n%s\n", self::CMD[$data], self::S_ERROR_OK);
89 }
90}
Enhanced exception class for PlanetTeamSpeak\TeamSpeak3Framework\Transport\Transport objects.
sendLine(string $data, string $separator="\n")
Definition MockTCP.php:56
Class for connecting to a remote server through TCP.
Definition TCP.php:17