1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slince/spike package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Spike\Server; |
13
|
|
|
|
14
|
|
|
use React\Socket\ConnectionInterface; |
15
|
|
|
use Spike\Client\ClientInterface; |
16
|
|
|
|
17
|
|
|
class Client implements ClientInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* id. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $id; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Client information. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $info; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \DateTimeInterface |
35
|
|
|
*/ |
36
|
|
|
protected $activeAt; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ConnectionInterface |
40
|
|
|
*/ |
41
|
|
|
protected $controlConnection; |
42
|
|
|
|
43
|
|
|
public function __construct($info, ConnectionInterface $controlConnection) |
44
|
|
|
{ |
45
|
|
|
$this->info = $info; |
46
|
|
|
$this->controlConnection = $controlConnection; |
47
|
|
|
$this->activeAt = new \DateTime(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Sets the control connection for the client. |
52
|
|
|
* |
53
|
|
|
* @param ConnectionInterface $controlConnection |
54
|
|
|
*/ |
55
|
|
|
public function setControlConnection($controlConnection) |
56
|
|
|
{ |
57
|
|
|
$this->controlConnection = $controlConnection; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets the control connection of the client. |
62
|
|
|
* |
63
|
|
|
* @return ConnectionInterface |
64
|
|
|
*/ |
65
|
|
|
public function getControlConnection() |
66
|
|
|
{ |
67
|
|
|
return $this->controlConnection; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getId() |
74
|
|
|
{ |
75
|
|
|
return $this->id ?: ($this->id = spl_object_hash($this)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function setId($id) |
82
|
|
|
{ |
83
|
|
|
$this->id = $id; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function setActiveAt($activeAt) |
90
|
|
|
{ |
91
|
|
|
$this->activeAt = $activeAt; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getActiveAt() |
98
|
|
|
{ |
99
|
|
|
return $this->activeAt; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets the client information. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function toArray() |
108
|
|
|
{ |
109
|
|
|
return array_replace($this->info, [ |
110
|
|
|
'id' => $this->getId(), |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function start() |
118
|
|
|
{ |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Close the client. |
123
|
|
|
*/ |
124
|
|
|
public function close() |
125
|
|
|
{ |
126
|
|
|
$this->controlConnection->end(); |
127
|
|
|
} |
128
|
|
|
} |