Gateway.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. <?php
  2. /**
  3. *
  4. * 暴露给客户端的连接网关 只负责网络io
  5. * 1、监听客户端连接
  6. * 2、监听后端回应并转发回应给前端
  7. *
  8. * @author walkor <workerman.net>
  9. *
  10. */
  11. require_once __DIR__ . '/../Lib/Autoloader.php';
  12. use \Protocols\GatewayProtocol;
  13. use \Lib\Store;
  14. use \Lib\StatisticClient;
  15. class Gateway extends Man\Core\SocketWorker
  16. {
  17. /**
  18. * 内部通信socket udp
  19. * @var resouce
  20. */
  21. protected $innerMainSocketUdp = null;
  22. /**
  23. * 内部通信socket tcp
  24. * @var resouce
  25. */
  26. protected $innerMainSocketTcp = null;
  27. /**
  28. * 内网ip
  29. * @var string
  30. */
  31. protected $lanIp = '127.0.0.1';
  32. /**
  33. * 内部通信端口
  34. * @var int
  35. */
  36. protected $lanPort = 0;
  37. /**
  38. * client_id到连接的映射
  39. * @var array
  40. */
  41. protected $clientConnMap = array();
  42. /**
  43. * 连接到client_id的映射
  44. * @var array
  45. */
  46. protected $connClientMap = array();
  47. /**
  48. * 客户端链接和客户端远程地址映射
  49. * @var array
  50. */
  51. protected $connRemoteAddressMap = array();
  52. /**
  53. * client_id到session的映射
  54. * @var array
  55. */
  56. protected $connSessionMap = array();
  57. /**
  58. * 与worker的连接
  59. * [fd=>fd, $fd=>fd, ..]
  60. * @var array
  61. */
  62. protected $workerConnections = array();
  63. /**
  64. * 客户端心跳信息
  65. * [client_id=>not_response_count, client_id=>not_response_count, ..]
  66. */
  67. protected $pingInfo = array();
  68. /**
  69. * gateway 发送心跳时间间隔 单位:秒 ,0表示不发送心跳,在配置中设置
  70. * @var integer
  71. */
  72. protected $pingInterval = 0;
  73. /**
  74. * 心跳数据
  75. * 可以是二进制数据(二进制数据保存在文件中,在配置中设置ping数据文件路径 如 ping_data=/yourpath/ping.bin)
  76. * ping数据应该是客户端能够识别的数据格式,客户端必须回复心跳,不然链接会断开
  77. * @var string
  78. */
  79. protected $pingData = '';
  80. /**
  81. * 客户端连续$pingNotResponseLimit次不回应心跳则断开链接
  82. */
  83. protected $pingNotResponseLimit = 0;
  84. /**
  85. * 命令字,统计用到
  86. * @var array
  87. */
  88. protected static $interfaceMap = array(
  89. GatewayProtocol::CMD_SEND_TO_ONE => 'CMD_SEND_TO_ONE',
  90. GatewayProtocol::CMD_SEND_TO_ALL => 'CMD_SEND_TO_ALL',
  91. GatewayProtocol::CMD_KICK => 'CMD_KICK',
  92. GatewayProtocol::CMD_UPDATE_SESSION => 'CMD_UPDATE_SESSION',
  93. GatewayProtocol::CMD_GET_ONLINE_STATUS => 'CMD_GET_ONLINE_STATUS',
  94. GatewayProtocol::CMD_IS_ONLINE => 'CMD_IS_ONLINE',
  95. );
  96. /**
  97. * 由于网络延迟或者socket缓冲区大小的限制,客户端发来的数据可能不会都全部到达,需要根据协议判断数据是否完整
  98. * @see Man\Core.SocketWorker::dealInput()
  99. */
  100. public function dealInput($recv_buffer)
  101. {
  102. // 处理粘包
  103. return Event::onGatewayMessage($recv_buffer);
  104. }
  105. /**
  106. * 用户客户端发来消息时处理
  107. * @see Man\Core.SocketWorker::dealProcess()
  108. */
  109. public function dealProcess($recv_buffer)
  110. {
  111. // 客户端发来任何一个完整的包都视为回应了服务端发的心跳
  112. if(!empty($this->pingInfo[$this->connClientMap[$this->currentDealFd]]))
  113. {
  114. $this->pingInfo[$this->connClientMap[$this->currentDealFd]] = 0;
  115. }
  116. // 统计打点
  117. StatisticClient::tick(__CLASS__, 'CMD_ON_MESSAGE');
  118. // 触发ON_MESSAGE
  119. if(false === $this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $this->currentDealFd, $recv_buffer))
  120. {
  121. return StatisticClient::report(__CLASS__, 'CMD_ON_MESSAGE', 0, 132, __CLASS__.'::dealProcess()->sendToWorker() fail');
  122. }
  123. StatisticClient::report(__CLASS__, 'CMD_ON_MESSAGE', 1, 0, '');
  124. }
  125. /**
  126. * 进程启动
  127. */
  128. public function start()
  129. {
  130. // 安装信号处理函数
  131. $this->installSignal();
  132. // 添加accept事件
  133. $ret = $this->event->add($this->mainSocket, Man\Core\Events\BaseEvent::EV_READ, array($this, 'accept'));
  134. // 创建内部通信套接字,用于与BusinessWorker通讯
  135. $start_port = Man\Core\Lib\Config::get($this->workerName.'.lan_port_start');
  136. // 计算本进程监听的ip端口
  137. $this->lanPort = $start_port - posix_getppid() + posix_getpid();
  138. // 如果端口不在合法范围
  139. if($this->lanPort<0 || $this->lanPort >=65535)
  140. {
  141. $this->lanPort = rand($start_port, 65535);
  142. }
  143. // 如果
  144. $this->lanIp = Man\Core\Lib\Config::get($this->workerName.'.lan_ip');
  145. if(!$this->lanIp)
  146. {
  147. $this->notice($this->workerName.'.lan_ip not set');
  148. $this->lanIp = '127.0.0.1';
  149. }
  150. $error_no_udp = $error_no_tcp = 0;
  151. $error_msg_udp = $error_msg_tcp = '';
  152. // 执行监听
  153. $this->innerMainSocketUdp = stream_socket_server("udp://".$this->lanIp.':'.$this->lanPort, $error_no_udp, $error_msg_udp, STREAM_SERVER_BIND);
  154. $this->innerMainSocketTcp = stream_socket_server("tcp://".$this->lanIp.':'.$this->lanPort, $error_no_tcp, $error_msg_tcp, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
  155. // 出错,退出,下次会换个端口
  156. if(!$this->innerMainSocketUdp || !$this->innerMainSocketTcp)
  157. {
  158. $this->notice('create innerMainSocket udp or tcp fail and exit '.$error_msg_udp.$error_msg_tcp);
  159. $this->stop();
  160. }
  161. else
  162. {
  163. stream_set_blocking($this->innerMainSocketUdp , 0);
  164. stream_set_blocking($this->innerMainSocketTcp , 0);
  165. }
  166. // 注册套接字
  167. if(!$this->registerAddress($this->lanIp.':'.$this->lanPort))
  168. {
  169. $this->notice('registerAddress fail and exit');
  170. $this->stop();
  171. }
  172. // 添加读udp/tcp事件
  173. $this->event->add($this->innerMainSocketUdp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'recvInnerUdp'));
  174. $this->event->add($this->innerMainSocketTcp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'acceptInnerTcp'));
  175. // 初始化心跳包时间间隔
  176. $ping_interval = \Man\Core\Lib\Config::get($this->workerName.'.ping_interval');
  177. if((int)$ping_interval > 0)
  178. {
  179. $this->pingInterval = (int)$ping_interval;
  180. }
  181. // 获取心跳包数据
  182. $ping_data_or_path = \Man\Core\Lib\Config::get($this->workerName.'.ping_data');
  183. if(is_file($ping_data_or_path))
  184. {
  185. $this->pingData = file_get_contents($ping_data_or_path);
  186. }
  187. else
  188. {
  189. $this->pingData = $ping_data_or_path;
  190. }
  191. // 不返回心跳回应(客户端发来的任何数据都算是回应)的限定值
  192. $this->pingNotResponseLimit = (int)\Man\Core\Lib\Config::get($this->workerName.'.ping_not_response_limit');
  193. // 设置定时任务,发送心跳
  194. if($this->pingInterval > 0)
  195. {
  196. \Man\Core\Lib\Task::init($this->event);
  197. \Man\Core\Lib\Task::add($this->pingInterval, array($this, 'ping'));
  198. }
  199. // 主体循环,整个子进程会阻塞在这个函数上
  200. $ret = $this->event->loop();
  201. // 下面正常不会执行到
  202. $this->notice('worker loop exit');
  203. // 执行到就退出
  204. exit(0);
  205. }
  206. /**
  207. * 接受一个链接
  208. * @param resource $socket
  209. * @param $null_one $flag
  210. * @param $null_two $base
  211. * @return void
  212. */
  213. public function accept($socket, $null_one = null, $null_two = null)
  214. {
  215. // 获得一个连接
  216. $new_connection = @stream_socket_accept($socket, 0);
  217. // 可能是惊群效应
  218. if(false === $new_connection)
  219. {
  220. $this->statusInfo['thunder_herd']++;
  221. return false;
  222. }
  223. // 连接的fd序号
  224. $fd = (int) $new_connection;
  225. $this->connections[$fd] = $new_connection;
  226. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>$this->prereadLength);
  227. $this->connSessionMap[$fd] = '';
  228. // 非阻塞
  229. stream_set_blocking($this->connections[$fd], 0);
  230. $this->event->add($this->connections[$fd], Man\Core\Events\BaseEvent::EV_READ , array($this, 'dealInputBase'), $fd);
  231. // 全局唯一client_id
  232. $global_client_id = $this->createGlobalClientId();
  233. $this->clientConnMap[$global_client_id] = $fd;
  234. $this->connClientMap[$fd] = $global_client_id;
  235. $address = array('local_ip'=>$this->lanIp, 'local_port'=>$this->lanPort, 'socket_id'=>$fd);
  236. // 保存客户端内部通讯地址
  237. $this->storeClientAddress($global_client_id, $address);
  238. // 客户端保存 ip:port
  239. $address= $this->getRemoteAddress($fd);
  240. if($address)
  241. {
  242. list($client_ip, $client_port) = explode(':', $address, 2);
  243. }
  244. else
  245. {
  246. $client_ip = 0;
  247. $client_port = 0;
  248. }
  249. $this->connRemoteAddressMap[$fd] = array('ip'=>$client_ip, 'port'=>$client_port);
  250. // 触发GatewayOnConnection事件
  251. if(method_exists('Event','onGatewayConnect'))
  252. {
  253. // 统计打点
  254. StatisticClient::tick(__CLASS__, 'CMD_ON_GATEWAY_CONNECTION');
  255. if(false === $this->sendToWorker(GatewayProtocol::CMD_ON_GATEWAY_CONNECTION, $fd))
  256. {
  257. StatisticClient::report(__CLASS__, 'CMD_ON_GATEWAY_CONNECTION', 0, 131, __CLASS__.'::accept()->sendToWorker() fail');
  258. }
  259. else
  260. {
  261. StatisticClient::report(__CLASS__, 'CMD_ON_GATEWAY_CONNECTION', 1, 0, '');
  262. }
  263. }
  264. return $new_connection;
  265. }
  266. /**
  267. * 存储全局的通信地址
  268. * @param string $address
  269. */
  270. protected function registerAddress($address)
  271. {
  272. // 统计打点
  273. StatisticClient::tick(__CLASS__, 'registerAddress');
  274. // 这里使用了信号量只能实现单机互斥,分布式互斥需要借助于memcached incr cas 或者其他分布式存储
  275. \Man\Core\Lib\Mutex::get();
  276. // key
  277. $key = 'GLOBAL_GATEWAY_ADDRESS';
  278. // 获取实例
  279. try
  280. {
  281. $store = Store::instance('gateway');
  282. }
  283. catch(\Exception $msg)
  284. {
  285. StatisticClient::report(__CLASS__, 'registerAddress', 0, 107, $msg);
  286. \Man\Core\Lib\Mutex::release();
  287. return false;
  288. }
  289. // 获取数据
  290. $addresses_list = $store->get($key);
  291. if(empty($addresses_list))
  292. {
  293. $addresses_list = array();
  294. }
  295. // 添加数据
  296. $addresses_list[$address] = $address;
  297. // 存储
  298. if(!$store->set($key, $addresses_list))
  299. {
  300. // 存储失败
  301. \Man\Core\Lib\Mutex::release();
  302. $msg = "注册gateway通信地址出错";
  303. if(get_class($store) == 'Memcached')
  304. {
  305. $msg .= " 原因:".$store->getResultMessage();
  306. }
  307. $this->notice($msg);
  308. StatisticClient::report(__CLASS__, 'registerAddress', 0, 107, new \Exception($msg));
  309. return false;
  310. }
  311. // 存储成功
  312. \Man\Core\Lib\Mutex::release();
  313. StatisticClient::report(__CLASS__, 'registerAddress', 1, 0, '');
  314. return true;
  315. }
  316. /**
  317. * 删除全局的通信地址
  318. * @param string $address
  319. */
  320. protected function unregisterAddress($address)
  321. {
  322. // 统计打点
  323. StatisticClient::tick(__CLASS__, 'unregisterAddress');
  324. // 这里使用了信号量只能实现单机互斥,分布式互斥需要借助于memcached incr cas或者其他分布式存储
  325. \Man\Core\Lib\Mutex::get();
  326. // key
  327. $key = 'GLOBAL_GATEWAY_ADDRESS';
  328. // 获取存储实例
  329. try
  330. {
  331. $store = Store::instance('gateway');
  332. }
  333. catch (\Exception $msg)
  334. {
  335. StatisticClient::report(__CLASS__, 'unregisterAddress', 0, 108, $msg);
  336. \Man\Core\Lib\Mutex::release();
  337. return false;
  338. }
  339. // 获取数据
  340. $addresses_list = $store->get($key);
  341. if(empty($addresses_list))
  342. {
  343. $addresses_list = array();
  344. }
  345. // 去掉要删除的数据
  346. unset($addresses_list[$address]);
  347. // 保存数据
  348. if(!$store->set($key, $addresses_list))
  349. {
  350. \Man\Core\Lib\Mutex::release();
  351. $msg = "删除gateway通信地址出错";
  352. if(get_class($store) == 'Memcached')
  353. {
  354. $msg .= " 原因:".$store->getResultMessage();
  355. }
  356. $this->notice($msg);
  357. StatisticClient::report(__CLASS__, 'unregisterAddress', 0, 108, new \Exception($msg));
  358. return;
  359. }
  360. // 存储成功
  361. \Man\Core\Lib\Mutex::release();
  362. StatisticClient::report(__CLASS__, 'unregisterAddress', 1, 0, '');
  363. }
  364. /**
  365. * 接收Udp数据
  366. * 如果数据超过一个udp包长,需要业务自己解析包体,判断数据是否全部到达
  367. * @param resource $socket
  368. * @param $null_one $flag
  369. * @param $null_two $base
  370. * @return void
  371. */
  372. public function recvInnerUdp($socket, $null_one = null, $null_two = null)
  373. {
  374. $data = stream_socket_recvfrom($socket , self::MAX_UDP_PACKEG_SIZE, 0, $address);
  375. // 惊群效应
  376. if(false === $data || empty($address))
  377. {
  378. return false;
  379. }
  380. $this->currentClientAddress = $address;
  381. $this->innerDealProcess($data);
  382. }
  383. /**
  384. * 内部通讯端口接受BusinessWorker连接请求,以便建立起长连接
  385. * @param resouce $socket
  386. * @param null $null_one
  387. * @param null $null_two
  388. */
  389. public function acceptInnerTcp($socket, $null_one = null, $null_two = null)
  390. {
  391. // 获得一个连接
  392. $new_connection = @stream_socket_accept($socket, 0);
  393. if(false === $new_connection)
  394. {
  395. return false;
  396. }
  397. // 连接的fd序号
  398. $fd = (int) $new_connection;
  399. $this->connections[$fd] = $new_connection;
  400. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>GatewayProtocol::HEAD_LEN);
  401. // 非阻塞
  402. stream_set_blocking($this->connections[$fd], 0);
  403. $this->event->add($this->connections[$fd], \Man\Core\Events\BaseEvent::EV_READ , array($this, 'recvInnerTcp'), $fd);
  404. // 标记这个连接是内部通讯长连接,区别于客户端连接
  405. $this->workerConnections[$fd] = $fd;
  406. return $new_connection;
  407. }
  408. /**
  409. * 内部通讯判断数据是否全部到达
  410. * @param string $buffer
  411. */
  412. public function dealInnerInput($buffer)
  413. {
  414. return GatewayProtocol::input($buffer);
  415. }
  416. /**
  417. * 处理内部通讯收到的数据
  418. * @param event_buffer $event_buffer
  419. * @param int $fd
  420. * @return void
  421. */
  422. public function recvInnerTcp($connection, $flag, $fd = null)
  423. {
  424. $this->currentDealFd = $fd;
  425. $buffer = stream_socket_recvfrom($connection, $this->recvBuffers[$fd]['remain_len']);
  426. // 出错了
  427. if('' == $buffer && '' == ($buffer = fread($connection, $this->recvBuffers[$fd]['remain_len'])))
  428. {
  429. // 判断是否是链接断开
  430. if(!feof($connection))
  431. {
  432. return;
  433. }
  434. // 如果该链接对应的buffer有数据,说明发生错误
  435. if(!empty($this->recvBuffers[$fd]['buf']))
  436. {
  437. $this->statusInfo['send_fail']++;
  438. }
  439. // 关闭链接
  440. $this->closeInnerClient($fd);
  441. $this->notice("CLIENT:".$this->getRemoteIp()." CLOSE INNER_CONNECTION\n");
  442. if($this->workerStatus == self::STATUS_SHUTDOWN)
  443. {
  444. $this->stop();
  445. }
  446. return;
  447. }
  448. $this->recvBuffers[$fd]['buf'] .= $buffer;
  449. $remain_len = $this->dealInnerInput($this->recvBuffers[$fd]['buf']);
  450. // 包接收完毕
  451. if(0 === $remain_len)
  452. {
  453. // 内部通讯业务处理
  454. $this->innerDealProcess($this->recvBuffers[$fd]['buf']);
  455. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>GatewayProtocol::HEAD_LEN);
  456. }
  457. // 出错
  458. else if(false === $remain_len)
  459. {
  460. // 出错
  461. $this->statusInfo['packet_err']++;
  462. $this->notice("INNER_PACKET_ERROR and CLOSE_INNER_CONNECTION\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".bin2hex($this->recvBuffers[$fd]['buf'])."]\n");
  463. $this->closeInnerClient($fd);
  464. }
  465. else
  466. {
  467. $this->recvBuffers[$fd]['remain_len'] = $remain_len;
  468. }
  469. // 检查是否是关闭状态或者是否到达请求上限
  470. if($this->workerStatus == self::STATUS_SHUTDOWN )
  471. {
  472. // 停止服务
  473. $this->stop();
  474. // EXIT_WAIT_TIME秒后退出进程
  475. pcntl_alarm(self::EXIT_WAIT_TIME);
  476. }
  477. }
  478. /**
  479. * 内部通讯处理
  480. * @param string $recv_buffer
  481. */
  482. public function innerDealProcess($recv_buffer)
  483. {
  484. $pack = new GatewayProtocol($recv_buffer);
  485. $cmd = $pack->header['cmd'];
  486. $interface = isset(self::$interfaceMap[$cmd]) ? self::$interfaceMap[$cmd] : $cmd;
  487. StatisticClient::tick(__CLASS__, $interface);
  488. try
  489. {
  490. switch($cmd)
  491. {
  492. // 向某客户端发送数据
  493. case GatewayProtocol::CMD_SEND_TO_ONE:
  494. if(false === $this->sendToSocketId($pack->header['socket_id'], $pack->body))
  495. {
  496. throw new \Exception('发送数据到客户端失败,可能是该客户端的发送缓冲区已满,或者客户端已经下线', 121);
  497. }
  498. break;
  499. // 踢掉客户端
  500. case GatewayProtocol::CMD_KICK:
  501. $this->closeClient($pack->header['socket_id']);
  502. break;
  503. // 向所客户端发送数据
  504. case GatewayProtocol::CMD_SEND_TO_ALL:
  505. if($pack->ext_data)
  506. {
  507. $client_id_array = unpack('N*', $pack->ext_data);
  508. foreach($client_id_array as $client_id)
  509. {
  510. if(isset($this->clientConnMap[$client_id]))
  511. {
  512. $this->sendToSocketId($this->clientConnMap[$client_id], $pack->body);
  513. }
  514. }
  515. }
  516. else
  517. {
  518. $this->broadCast($pack->body);
  519. }
  520. break;
  521. // 更新某个客户端的session
  522. case GatewayProtocol::CMD_UPDATE_SESSION:
  523. if(isset($this->connSessionMap[$pack->header['socket_id']]))
  524. {
  525. $this->connSessionMap[$pack->header['socket_id']] = $pack->ext_data;
  526. }
  527. break;
  528. // 获得在线状态
  529. case GatewayProtocol::CMD_GET_ONLINE_STATUS:
  530. $online_status = json_encode(array_values($this->connClientMap));
  531. stream_socket_sendto($this->innerMainSocketUdp, $online_status, 0, $this->currentClientAddress);
  532. break;
  533. // 判断某个客户端id是否在线
  534. case GatewayProtocol::CMD_IS_ONLINE:
  535. stream_socket_sendto($this->innerMainSocketUdp, (int)isset($this->clientConnMap[$pack->header['client_id']]), 0, $this->currentClientAddress);
  536. break;
  537. // 未知的命令
  538. default :
  539. $err_msg = "gateway inner pack err cmd=$cmd";
  540. $this->notice($err_msg);
  541. throw new \Exception($err_msg, 110);
  542. }
  543. }
  544. catch(\Exception $msg)
  545. {
  546. StatisticClient::report(__CLASS__, $interface, 0, $msg->getCode() > 0 ? $msg->getCode() : 111, $msg);
  547. return;
  548. }
  549. StatisticClient::report(__CLASS__, $interface, 1, 0, '');
  550. }
  551. /**
  552. * 广播数据
  553. * @param string $bin_data
  554. */
  555. protected function broadCast($bin_data)
  556. {
  557. foreach($this->clientConnMap as $client_id=>$conn)
  558. {
  559. $this->sendToSocketId($conn, $bin_data);
  560. }
  561. }
  562. /**
  563. * 根据client_id获取client_id对应连接的socket_id
  564. * @param int $client_id
  565. */
  566. protected function getFdByClientId($client_id)
  567. {
  568. if(isset($this->clientConnMap[$client_id]))
  569. {
  570. return $this->clientConnMap[$client_id];
  571. }
  572. return 0;
  573. }
  574. /**
  575. * 根据连接socket_id获取client_id
  576. * @param int $fd
  577. */
  578. protected function getClientIdByFd($fd)
  579. {
  580. if(isset($this->connClientMap[$fd]))
  581. {
  582. return $this->connClientMap[$fd];
  583. }
  584. return 0;
  585. }
  586. /**
  587. * 向某个socket_id的连接发送消息
  588. * @param int $socket_id
  589. * @param string $bin_data
  590. */
  591. public function sendToSocketId($socket_id, $bin_data)
  592. {
  593. if(!isset($this->connections[$socket_id]))
  594. {
  595. return null;
  596. }
  597. $this->currentDealFd = $socket_id;
  598. return $this->sendToClient($bin_data);
  599. }
  600. /**
  601. * 用户客户端关闭连接时触发
  602. * @see Man\Core.SocketWorker::closeClient()
  603. */
  604. protected function closeClient($fd)
  605. {
  606. if($client_id = $this->getClientIdByFd($fd))
  607. {
  608. $this->sendToWorker(GatewayProtocol::CMD_ON_CLOSE, $fd);
  609. $this->deleteClientAddress($client_id);
  610. unset($this->clientConnMap[$client_id]);
  611. }
  612. unset($this->connClientMap[$fd], $this->connSessionMap[$fd], $this->connRemoteAddressMap[$fd]);
  613. parent::closeClient($fd);
  614. }
  615. /**
  616. * 内部通讯socket在BusinessWorker主动关闭连接时触发
  617. * @param int $fd
  618. */
  619. protected function closeInnerClient($fd)
  620. {
  621. unset($this->workerConnections[$fd]);
  622. parent::closeClient($fd);
  623. }
  624. /**
  625. * 随机抽取一个与BusinessWorker的长连接,将数据发给一个BusinessWorker
  626. * @param int $cmd
  627. * @param int $socket_id
  628. * @param string $body
  629. */
  630. protected function sendToWorker($cmd, $socket_id, $body = '')
  631. {
  632. $pack = new GatewayProtocol();
  633. $pack->header['cmd'] = $cmd;
  634. $pack->header['local_ip'] = $this->lanIp;
  635. $pack->header['local_port'] = $this->lanPort;
  636. $pack->header['socket_id'] = $socket_id;
  637. $pack->header['client_ip'] = $this->connRemoteAddressMap[$socket_id]['ip'];
  638. $pack->header['client_port'] = $this->connRemoteAddressMap[$socket_id]['port'];
  639. $pack->header['client_id'] = $this->getClientIdByFd($socket_id);
  640. $pack->body = $body;
  641. $pack->ext_data = $this->connSessionMap[$pack->header['socket_id']];
  642. return $this->sendBufferToWorker($pack->getBuffer());
  643. }
  644. /**
  645. * 随机抽取一个与BusinessWorker的长连接,将数据发给一个BusinessWorker
  646. * @param string $bin_data
  647. */
  648. protected function sendBufferToWorker($bin_data)
  649. {
  650. if($this->currentDealFd = array_rand($this->workerConnections))
  651. {
  652. if(false === $this->sendToClient($bin_data))
  653. {
  654. $msg = "sendBufferToWorker fail. May be the send buffer are overflow";
  655. $this->notice($msg);
  656. StatisticClient::report(__CLASS__, 'sendBufferToWorker', 0, 101, new \Exception($msg));
  657. return false;
  658. }
  659. }
  660. else
  661. {
  662. $msg = "sendBufferToWorker fail. the Connections between Gateway and BusinessWorker are not ready";
  663. $this->notice($msg);
  664. StatisticClient::report(__CLASS__, 'sendBufferToWorker', 0, 102, new \Exception($msg));
  665. return false;
  666. }
  667. StatisticClient::report(__CLASS__, 'sendBufferToWorker', 1, 0, '');
  668. }
  669. /**
  670. * 打印日志
  671. * @see Man\Core.AbstractWorker::notice()
  672. */
  673. protected function notice($str, $display=true)
  674. {
  675. $str = 'Worker['.get_class($this).']:'."$str";
  676. Man\Core\Lib\Log::add($str);
  677. if($display && Man\Core\Lib\Config::get('workerman.debug') == 1)
  678. {
  679. echo $str."\n";
  680. }
  681. }
  682. /**
  683. * 进程停止时,清除一些数据,忽略错误
  684. * @see Man\Core.SocketWorker::onStop()
  685. */
  686. public function onStop()
  687. {
  688. $this->unregisterAddress($this->lanIp.':'.$this->lanPort);
  689. foreach($this->connClientMap as $client_id)
  690. {
  691. Store::instance('gateway')->delete($client_id);
  692. }
  693. }
  694. /**
  695. * 创建全局唯一的id
  696. */
  697. protected function createGlobalClientId()
  698. {
  699. StatisticClient::tick(__CLASS__, 'createGlobalClientId');
  700. $global_socket_key = 'GLOBAL_SOCKET_ID_KEY';
  701. $store = Store::instance('gateway');
  702. $global_client_id = $store->increment($global_socket_key);
  703. if(!$global_client_id || $global_client_id > 2147483646)
  704. {
  705. $store->set($global_socket_key, 0);
  706. $global_client_id = $store->increment($global_socket_key);
  707. }
  708. if(!$global_client_id)
  709. {
  710. $msg = "生成全局client_id出错";
  711. if(get_class($store) == 'Memcached')
  712. {
  713. $msg .= " 原因:".$store->getResultMessage();
  714. }
  715. $this->notice($msg);
  716. StatisticClient::report(__CLASS__, 'createGlobalClientId', 0, 104, new \Exception($msg));
  717. return $global_client_id;
  718. }
  719. StatisticClient::report(__CLASS__, 'createGlobalClientId', 1, 0, '');
  720. return $global_client_id;
  721. }
  722. /**
  723. * 保存客户端内部通讯地址
  724. * @param int $global_client_id
  725. * @param string $address
  726. */
  727. protected function storeClientAddress($global_client_id, $address)
  728. {
  729. StatisticClient::tick(__CLASS__, 'storeClientAddress');
  730. if(!Store::instance('gateway')->set($global_client_id, $address))
  731. {
  732. $msg = "保存客户端通讯地址出错";
  733. if(get_class(Store::instance('gateway')) == 'Memcached')
  734. {
  735. $msg .= " 原因:".Store::instance('gateway')->getResultMessage();
  736. }
  737. $this->notice($msg);
  738. return StatisticClient::report(__CLASS__, 'storeClientAddress', 0, 105, new \Exception($msg));
  739. }
  740. StatisticClient::report(__CLASS__, 'storeClientAddress', 1, 0, '');
  741. }
  742. /**
  743. * 删除客户端内部通讯地址
  744. * @param int $global_client_id
  745. * @param string $address
  746. */
  747. protected function deleteClientAddress($global_client_id)
  748. {
  749. StatisticClient::tick(__CLASS__, 'deleteClientAddress');
  750. if(!Store::instance('gateway')->delete($global_client_id))
  751. {
  752. $msg = "删除客户端通讯地址出错";
  753. if(get_class(Store::instance('gateway')) == 'Memcached')
  754. {
  755. $msg .= " 原因:".Store::instance('gateway')->getResultMessage();
  756. }
  757. $this->notice($msg);
  758. return StatisticClient::report(__CLASS__, 'deleteClientAddress', 0, 106, new \Exception($msg));
  759. }
  760. StatisticClient::report(__CLASS__, 'deleteClientAddress', 1, 0, '');
  761. }
  762. /**
  763. * 向客户端发送心跳数据
  764. * 并把规定时间内没回应(客户端发来的任何数据都算是回应)的客户端踢掉
  765. */
  766. public function ping()
  767. {
  768. // 清理下线的链接
  769. foreach($this->pingInfo as $client_id=>$not_response_count)
  770. {
  771. // 已经下线的忽略
  772. if(!isset($this->clientConnMap[$client_id]))
  773. {
  774. unset($this->pingInfo[$client_id]);
  775. continue;
  776. }
  777. // 上次发送的心跳还没有回复次数大于限定值就断开
  778. if($this->pingNotResponseLimit > 0 && $not_response_count >= $this->pingNotResponseLimit)
  779. {
  780. $this->closeClient($this->clientConnMap[$client_id]);
  781. }
  782. }
  783. // 向所有链接发送心跳数据
  784. foreach($this->clientConnMap as $client_id=>$conn)
  785. {
  786. // 如果没设置ping的数据,则不发送心跳给客户端,但是要求客户端在规定的时间内(ping_interval*ping_not_response_limit)有请求发来,不然会断开
  787. if($this->pingData)
  788. {
  789. $this->sendToSocketId($conn, $this->pingData);
  790. }
  791. if(isset($this->pingInfo[$client_id]))
  792. {
  793. $this->pingInfo[$client_id]++;
  794. }
  795. else
  796. {
  797. $this->pingInfo[$client_id] = 1;
  798. }
  799. }
  800. }
  801. }