1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spike library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Spike\Server\TunnelServer; |
7
|
|
|
|
8
|
|
|
use React\Socket\ConnectionInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @method write($data) |
12
|
|
|
* @method on($eventName, callable $listener) |
13
|
|
|
* @method pipe($dst, array $options = []); |
14
|
|
|
* @method removeListener($eventName, callable $listener); |
15
|
|
|
* @method removeAllListeners($eventName = null); |
16
|
|
|
* @method end($data = null); |
17
|
|
|
* @method pause(); |
18
|
|
|
* @method resume(); |
19
|
|
|
*/ |
20
|
|
|
class PublicConnection |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ConnectionInterface |
24
|
|
|
*/ |
25
|
|
|
protected $connection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $initBuffer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $id; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The create time |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $createAt; |
42
|
|
|
|
43
|
|
|
public function __construct(ConnectionInterface $connection, $initBuffer = '') |
44
|
|
|
{ |
45
|
|
|
$this->connection = $connection; |
46
|
|
|
$this->initBuffer = $initBuffer; |
47
|
|
|
$this->id = spl_object_hash($connection); |
48
|
|
|
$this->createAt = microtime(true); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __call($name, $arguments) |
52
|
|
|
{ |
53
|
|
|
return call_user_func_array([$this->connection, $name], $arguments); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getId() |
60
|
|
|
{ |
61
|
|
|
return $this->id; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return ConnectionInterface |
66
|
|
|
*/ |
67
|
|
|
public function getConnection() |
68
|
|
|
{ |
69
|
|
|
return $this->connection; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $initBuffer |
74
|
|
|
*/ |
75
|
|
|
public function setInitBuffer($initBuffer) |
76
|
|
|
{ |
77
|
|
|
$this->initBuffer = $initBuffer; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getInitBuffer() |
84
|
|
|
{ |
85
|
|
|
return $this->initBuffer; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets the waiting duration of the connection |
90
|
|
|
* @return float |
91
|
|
|
*/ |
92
|
|
|
public function getWaitingDuration() |
93
|
|
|
{ |
94
|
|
|
return microtime(true) - $this->createAt; |
95
|
|
|
} |
96
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.