|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Igni\Network\Server; |
|
4
|
|
|
|
|
5
|
|
|
use Igni\Network\Exception\ClientException; |
|
6
|
|
|
use Swoole\Server as SwooleServer; |
|
7
|
|
|
|
|
8
|
|
|
class Client |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var int |
|
12
|
|
|
*/ |
|
13
|
|
|
private $id; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var SwooleServer |
|
17
|
|
|
*/ |
|
18
|
|
|
private $handler; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param SwooleServer $handler |
|
22
|
|
|
* @param int $id |
|
23
|
|
|
*/ |
|
24
|
16 |
|
public function __construct($handler, int $id) |
|
25
|
|
|
{ |
|
26
|
16 |
|
$this->handler = $handler; |
|
27
|
16 |
|
$this->id = $id; |
|
28
|
16 |
|
} |
|
29
|
|
|
|
|
30
|
4 |
|
public function getId(): int |
|
31
|
|
|
{ |
|
32
|
4 |
|
return $this->id; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function getInfo(): ClientInfo |
|
36
|
|
|
{ |
|
37
|
1 |
|
return new ClientInfo($this->handler->getClientInfo($this->id)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function isActive(): bool |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this->handler->exist($this->id); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function pause(): void |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->handler->pause($this->id); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function resume(): void |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->handler->resume($this->id); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $data |
|
57
|
|
|
* @throws ClientException |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function send(string $data = null): void |
|
60
|
|
|
{ |
|
61
|
2 |
|
if (!$this->handler->send($this->id, $data)) { |
|
62
|
1 |
|
throw ClientException::forSendFailure($this, $data); |
|
63
|
|
|
} |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function protect(): void |
|
67
|
|
|
{ |
|
68
|
1 |
|
$this->handler->protect($this->id); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function wait(string $data = null): void |
|
72
|
|
|
{ |
|
73
|
|
|
if (!$this->handler->sendwait($this->id, $data)) { |
|
74
|
|
|
throw ClientException::forWaitFailure($this); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
public function confirm(): void |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->handler->confirm($this->id); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function close(): void |
|
84
|
|
|
{ |
|
85
|
1 |
|
$this->handler->close($this->id); |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function __toString(): string |
|
89
|
|
|
{ |
|
90
|
1 |
|
return self::class . "[{$this->id}]"; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|