Repository navigation
Expand file tree
/
Copy pathRPC.php
More file actions
182 lines (159 loc) · 3.87 KB
/
Copy pathRPC.php
File metadata and controls
182 lines (159 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace MyQEE\Server;
/**
* RPC 基础类
*
* 在客户端使用 `\RPC::Client()` 来获取对象, 不要使用 `new RPC()` 方式来构造对象
*
* 用于提供暴露出来的方法, 对象参数
*
* @package MyQEE\Server
*/
class RPC
{
/**
* 当前连接FD
*
* @var int
*/
private $__fd = null;
/**
* 当前连接所在线程ID
*
* @var int
*/
private $__fromId = 0;
private $__isClosed = false;
/**
* 此RPC通讯密钥
*
* @var string
*/
public static $RPC_KEY = __FILE__;
public static $__instance = [];
/**
* @param $fd
* @param int $fromId
*/
public function __construct($fd, $fromId = 0)
{
$this->__fd = $fd;
$this->__fromId = $fromId;
}
/**
* @param $fd
* @param null $fromId
* @return static
*/
public static function factory($fd, $fromId = null)
{
$class = static::class;
$obj = new $class($fd, $fromId);
return $obj;
}
/**
* 设置此对象为可复用
*
* 系统会为每个客户端创建一个对象RPC的对象, 调用完毕后销毁, 设置后只要客户端没有断开则不会销毁
*
* 可以使用 `$this->release()` 释放对象
*
* @return $this
*/
protected function enableReuse()
{
$rpc = get_class($this);
self::$__instance[$rpc][$this->__fd] = $this;
return $this;
}
/**
* 取消可复用
*/
protected function disableReuse()
{
$rpc = get_class($this);
if (!isset(self::$__instance[$rpc]))return;
unset(self::$__instance[$rpc][$this->__fd]);
if (!self::$__instance[$rpc])
{
unset(self::$__instance[$rpc]);
}
}
/**
* 服务器上触发客户端一个事件
*
* @param $event
* @param null $arg1
* @param null $arg2
* @return bool
*/
public function trigger($event, $arg1 = null, $arg2 = null)
{
if (null === $this->__fd)return false;
$args = func_get_args();
array_shift($args);
$obj = new \stdClass();
$obj->type = 'on';
$obj->event = $event;
$obj->args = $args;
$string = RPC\Server::encrypt($obj, static::_getRpcKey()) . RPC\Server::$EOF;
return Server::$instance->server->send($this->__fd, $string, $this->__fromId);
}
/**
* 返回当前RPC连接的信息
*
* 额外提供 fd 和 from_id
*
* @return array|bool
*/
protected function connectionInfo()
{
$rs = Server::$instance->server->connection_info($this->__fd);
if (!$rs)return false;
$rs['fd'] = $this->__fd;
$rs['from_id'] = $this->__fromId;
return $rs;
}
/**
* 关闭当前RPC客户端连接
*/
protected function closeClient($msg = null)
{
$data = [
'type' => 'close',
'msg' => $msg,
'code' => 0,
];
$this->__isClosed = true;
Server::$instance->server->send($this->__fd, json_encode($data, JSON_UNESCAPED_UNICODE) . RPC\Server::$EOF, $this->__fromId);
Server::$instance->server->close($this->__fd, $this->__fromId);
$this->disableReuse();
}
/**
* 是否被服务器强制关闭连接的
*
* @return bool
*/
public function isClosedByServer()
{
return $this->__isClosed;
}
/**
* 返回PRC调用的客户端, 客户端使用
*
* @return RPC\Client
*/
public static function Client()
{
return new RPC\Client(static::class);
}
/**
* 返回当前RPC的通讯密钥
*
* @return string
*/
public static function _getRpcKey()
{
return static::$RPC_KEY;
}
}