Skip to content

Commit b76df13

Browse files
committed
add TcpConnection::$id and add Gateway::$router
1 parent 2fc5c82 commit b76df13

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

GatewayWorker/Gateway.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class Gateway extends Worker
5454
*/
5555
public $pingData = '';
5656

57+
/**
58+
* 路由函数
59+
* @var callback
60+
*/
61+
public $router = null;
62+
5763
/**
5864
* 保存客户端的所有connection对象
5965
* @var array
@@ -123,6 +129,8 @@ public function __construct($socket_name, $context_option = array())
123129
{
124130
parent::__construct($socket_name, $context_option);
125131

132+
$this->router = array("\\GatewayWorker\\Gateway", 'routerRand');
133+
126134
$backrace = debug_backtrace();
127135
$this->_appInitPath = dirname($backrace[0]['file']);
128136
}
@@ -219,11 +227,11 @@ protected function sendToWorker($cmd, $connection, $body = '')
219227
$gateway_data['cmd'] = $cmd;
220228
$gateway_data['body'] = $body;
221229
$gateway_data['ext_data'] = $connection->session;
222-
// 随机选择一个worker处理
223-
$key = array_rand($this->_workerConnections);
224-
if($key)
230+
if($this->_workerConnections)
225231
{
226-
if(false === $this->_workerConnections[$key]->send($gateway_data))
232+
// 调用路由函数,选择一个worker把请求转发给它
233+
$worker_connection = call_user_func($this->router, $this->_workerConnections, $connection, $cmd, $body);
234+
if(false === $worker_connection->send($gateway_data))
227235
{
228236
$msg = "SendBufferToWorker fail. May be the send buffer are overflow";
229237
$this->log($msg);
@@ -246,6 +254,19 @@ protected function sendToWorker($cmd, $connection, $body = '')
246254
return true;
247255
}
248256

257+
/**
258+
* 随机路由,返回worker connection对象
259+
* @param array $worker_connections
260+
* @param TcpConnection $client_connection
261+
* @param int $cmd
262+
* @param mixed $buffer
263+
* @return TcpConnection
264+
*/
265+
public static function routerRand($worker_connections, $client_connection, $cmd, $buffer)
266+
{
267+
return $worker_connections[array_rand($worker_connections)];
268+
}
269+
249270
/**
250271
* 保存客户端连接的gateway通讯地址
251272
* @param int $global_client_id

Workerman/Connection/AsyncTcpConnection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function __construct($remote_address)
4949
}
5050
}
5151
$this->_remoteAddress = substr($address, 2);
52+
$this->id = self::$_idRecorder++;
5253
}
5354

5455
public function connect()

Workerman/Connection/TcpConnection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class TcpConnection extends ConnectionInterface
8787
*/
8888
public $worker = null;
8989

90+
/**
91+
* 连接的id,一个自增整数
92+
* @var int
93+
*/
94+
public $id = 0;
95+
9096
/**
9197
* 发送缓冲区大小,当发送缓冲区满时,会尝试触发onBufferFull回调(如果有设置的话)
9298
* 如果没设置onBufferFull回调,由于发送缓冲区满,则后续发送的数据将被丢弃,
@@ -105,6 +111,12 @@ class TcpConnection extends ConnectionInterface
105111
*/
106112
public static $maxPackageSize = 10485760;
107113

114+
/**
115+
* id 记录器
116+
* @var int
117+
*/
118+
protected static $_idRecorder = 1;
119+
108120
/**
109121
* 实际的socket资源
110122
* @var resource
@@ -167,6 +179,7 @@ class TcpConnection extends ConnectionInterface
167179
*/
168180
public function __construct($socket)
169181
{
182+
$this->id = self::$_idRecorder++;
170183
$this->_socket = $socket;
171184
stream_set_blocking($this->_socket, 0);
172185
Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));

0 commit comments

Comments
 (0)