Gateway.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?php
  2. /**
  3. *
  4. * 暴露给客户端的连接网关 只负责网络io
  5. * 1、监听客户端连接
  6. * 2、监听后端回应并转发回应给前端
  7. *
  8. * @author walkor <workerman.net>
  9. *
  10. */
  11. define('ROOT_DIR', realpath(__DIR__.'/../'));
  12. require_once ROOT_DIR . '/Protocols/GatewayProtocol.php';
  13. require_once ROOT_DIR . '/Protocols/JsonProtocol.php';
  14. require_once ROOT_DIR . '/Lib/Store.php';
  15. require_once ROOT_DIR . '/Event.php';
  16. require_once ROOT_DIR . '/Lib/StatisticClient.php';
  17. class Gateway extends Man\Core\SocketWorker
  18. {
  19. /**
  20. * 内部通信socket udp
  21. * @var resouce
  22. */
  23. protected $innerMainSocketUdp = null;
  24. /**
  25. * 内部通信socket tcp
  26. * @var resouce
  27. */
  28. protected $innerMainSocketTcp = null;
  29. /**
  30. * 内网ip
  31. * @var string
  32. */
  33. protected $lanIp = '127.0.0.1';
  34. /**
  35. * 内部通信端口
  36. * @var int
  37. */
  38. protected $lanPort = 0;
  39. /**
  40. * uid到连接的映射
  41. * @var array
  42. */
  43. protected $uidConnMap = array();
  44. /**
  45. * 连接到uid的映射
  46. * @var array
  47. */
  48. protected $connUidMap = array();
  49. /**
  50. * 与worker的连接
  51. * [fd=>fd, $fd=>fd, ..]
  52. * @var array
  53. */
  54. protected $workerConnections = array();
  55. /**
  56. * gateway 发送心跳时间间隔 单位:秒 ,0表示不发送心跳,在配置中设置
  57. * @var integer
  58. */
  59. protected $pingInterval = 0;
  60. /**
  61. * 心跳数据
  62. * 可以是字符串(在配置中直接设置字符串如 ping_data=ping),
  63. * 可以是二进制数据(二进制数据保存在文件中,在配置中设置ping数据文件路径 如 ping_data=/yourpath/ping.bin)
  64. * ping数据应该是客户端能够识别的数据格式,只是检测连接的连通性,客户端收到心跳数据可以选择忽略此数据包
  65. * @var string
  66. */
  67. protected $pingData = '';
  68. /**
  69. * 由于网络延迟或者socket缓冲区大小的限制,客户端发来的数据可能不会都全部到达,需要根据协议判断数据是否完整
  70. * @see Man\Core.SocketWorker::dealInput()
  71. */
  72. public function dealInput($recv_str)
  73. {
  74. // 如果有Event::onGatewayMessage方法通过这个方法检查数据是否接收完整
  75. if(method_exists('Event','onGatewayMessage'))
  76. {
  77. return call_user_func_array(array('Event', 'onGatewayMessage'), array($recv_str));
  78. }
  79. return 0;
  80. }
  81. /**
  82. * 用户客户端发来消息时处理
  83. * @see Man\Core.SocketWorker::dealProcess()
  84. */
  85. public function dealProcess($recv_str)
  86. {
  87. // 判断用户是否认证过
  88. StatisticClient::tick();
  89. $from_uid = $this->getUidByFd($this->currentDealFd);
  90. $module = __CLASS__;
  91. $success = 1;
  92. $code = 0;
  93. $msg = '';
  94. // 触发ON_CONNECTION
  95. if(!$from_uid)
  96. {
  97. $interface = 'ON_CONNECTION';
  98. $ret = $this->sendToWorker(GatewayProtocol::CMD_ON_CONNECTION, $this->currentDealFd, $recv_str);
  99. if($ret === false)
  100. {
  101. $success = 0;
  102. $msg = 'sendToWorker(CMD_ON_CONNECTION, '.$this->currentDealFd.', strlen($recv_str) = '.strlen($recv_str).') fail ';
  103. $code = 101;
  104. }
  105. }
  106. else
  107. {
  108. // 认证过, 触发ON_MESSAGE
  109. $interface = 'CMD_ON_MESSAGE';
  110. $ret =$this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $this->currentDealFd, $recv_str);
  111. if($ret === false)
  112. {
  113. $success = 0;
  114. $msg = 'sendToWorker(CMD_ON_MESSAGE, '.$this->currentDealFd.', strlen($recv_str) = '.strlen($recv_str).') fail ';
  115. $code = 102;
  116. }
  117. }
  118. StatisticClient::report($module, $interface, $success, $code, $msg);
  119. }
  120. /**
  121. * 进程启动
  122. */
  123. public function start()
  124. {
  125. // 安装信号处理函数
  126. $this->installSignal();
  127. // 添加accept事件
  128. $ret = $this->event->add($this->mainSocket, Man\Core\Events\BaseEvent::EV_READ, array($this, 'accept'));
  129. // 创建内部通信套接字,用于与BusinessWorker通讯
  130. $start_port = Man\Core\Lib\Config::get($this->workerName.'.lan_port_start');
  131. // 计算本进程监听的ip端口
  132. $this->lanPort = $start_port - posix_getppid() + posix_getpid();
  133. $this->lanIp = Man\Core\Lib\Config::get($this->workerName.'.lan_ip');
  134. if(!$this->lanIp)
  135. {
  136. $this->notice($this->workerName.'.lan_ip not set');
  137. $this->lanIp = '127.0.0.1';
  138. }
  139. $error_no_udp = $error_no_tcp = 0;
  140. $error_msg_udp = $error_msg_tcp = '';
  141. // 执行监听
  142. $this->innerMainSocketUdp = stream_socket_server("udp://".$this->lanIp.':'.$this->lanPort, $error_no_udp, $error_msg_udp, STREAM_SERVER_BIND);
  143. $this->innerMainSocketTcp = stream_socket_server("tcp://".$this->lanIp.':'.$this->lanPort, $error_no_tcp, $error_msg_tcp, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
  144. // 出错,退出,下次会换个端口
  145. if(!$this->innerMainSocketUdp || !$this->innerMainSocketTcp)
  146. {
  147. $this->notice('create innerMainSocket udp or tcp fail and exit '.$error_msg_udp.$error_msg_tcp);
  148. $this->stop();
  149. }
  150. else
  151. {
  152. stream_set_blocking($this->innerMainSocketUdp , 0);
  153. stream_set_blocking($this->innerMainSocketTcp , 0);
  154. }
  155. // 注册套接字
  156. $this->registerAddress($this->lanIp.':'.$this->lanPort);
  157. // 添加读udp/tcp事件
  158. $this->event->add($this->innerMainSocketUdp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'recvUdp'));
  159. $this->event->add($this->innerMainSocketTcp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'acceptTcp'));
  160. // 初始化心跳包时间间隔
  161. $ping_interval = \Man\Core\Lib\Config::get($this->workerName.'.ping_interval');
  162. if((int)$ping_interval > 0)
  163. {
  164. $this->pingInterval = (int)$ping_interval;
  165. }
  166. // 获取心跳包数据
  167. $ping_data_or_path = \Man\Core\Lib\Config::get($this->workerName.'.ping_data');
  168. if(is_file($ping_data_or_path))
  169. {
  170. $this->pingData = file_get_contents($ping_data_or_path);
  171. }
  172. else
  173. {
  174. $this->pingData = $ping_data_or_path;
  175. }
  176. // 设置定时任务,发送心跳
  177. if($this->pingInterval > 0 && $this->pingData)
  178. {
  179. \Man\Core\Lib\Task::init($this->event);
  180. \Man\Core\Lib\Task::add($this->pingInterval, array($this, 'ping'));
  181. }
  182. // 主体循环,整个子进程会阻塞在这个函数上
  183. $ret = $this->event->loop();
  184. // 下面正常不会执行到
  185. $this->notice('worker loop exit');
  186. // 执行到就退出
  187. exit(0);
  188. }
  189. /**
  190. * 存储全局的通信地址
  191. * @param string $address
  192. */
  193. protected function registerAddress($address)
  194. {
  195. // 这里使用了信号量只能实现单机互斥,分布式互斥需要借助于memcache incr 或者其他分布式存储
  196. \Man\Core\Lib\Mutex::get();
  197. $key = 'GLOBAL_GATEWAY_ADDRESS';
  198. $addresses_list = Store::get($key);
  199. if(empty($addresses_list))
  200. {
  201. $addresses_list = array();
  202. }
  203. $addresses_list[$address] = $address;
  204. Store::set($key, $addresses_list);
  205. \Man\Core\Lib\Mutex::release();
  206. }
  207. /**
  208. * 删除全局的通信地址
  209. * @param string $address
  210. */
  211. protected function unregisterAddress($address)
  212. {
  213. // 这里使用了信号量只能实现单机互斥,分布式互斥需要借助于memcache incr 或者其他分布式存储
  214. \Man\Core\Lib\Mutex::get();
  215. $key = 'GLOBAL_GATEWAY_ADDRESS';
  216. $addresses_list = Store::get($key);
  217. if(empty($addresses_list))
  218. {
  219. $addresses_list = array();
  220. }
  221. unset($addresses_list[$address]);
  222. Store::set($key, $addresses_list);
  223. \Man\Core\Lib\Mutex::release();
  224. }
  225. /**
  226. * 接收Udp数据
  227. * 如果数据超过一个udp包长,需要业务自己解析包体,判断数据是否全部到达
  228. * @param resource $socket
  229. * @param $null_one $flag
  230. * @param $null_two $base
  231. * @return void
  232. */
  233. public function recvUdp($socket, $null_one = null, $null_two = null)
  234. {
  235. $data = stream_socket_recvfrom($socket , self::MAX_UDP_PACKEG_SIZE, 0, $address);
  236. // 惊群效应
  237. if(false === $data || empty($address))
  238. {
  239. return false;
  240. }
  241. $this->currentClientAddress = $address;
  242. $this->innerDealProcess($data);
  243. }
  244. /**
  245. * 内部通讯端口接受BusinessWorker连接请求,以便建立起长连接
  246. * @param resouce $socket
  247. * @param null $null_one
  248. * @param null $null_two
  249. */
  250. public function acceptTcp($socket, $null_one = null, $null_two = null)
  251. {
  252. // 获得一个连接
  253. $new_connection = @stream_socket_accept($socket, 0);
  254. if(false === $new_connection)
  255. {
  256. return false;
  257. }
  258. // 连接的fd序号
  259. $fd = (int) $new_connection;
  260. $this->connections[$fd] = $new_connection;
  261. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>GatewayProtocol::HEAD_LEN);
  262. // 非阻塞
  263. stream_set_blocking($this->connections[$fd], 0);
  264. $this->event->add($this->connections[$fd], \Man\Core\Events\BaseEvent::EV_READ , array($this, 'recvTcp'), $fd);
  265. // 标记这个连接是内部通讯长连接,区别于客户端连接
  266. $this->workerConnections[$fd] = $fd;
  267. return $new_connection;
  268. }
  269. /**
  270. * 内部通讯判断数据是否全部到达
  271. * @param string $buffer
  272. */
  273. public function dealInnerInput($buffer)
  274. {
  275. return GatewayProtocol::input($buffer);
  276. }
  277. /**
  278. * 处理受到的数据
  279. * @param event_buffer $event_buffer
  280. * @param int $fd
  281. * @return void
  282. */
  283. public function recvTcp($connection, $flag, $fd = null)
  284. {
  285. $this->currentDealFd = $fd;
  286. $buffer = stream_socket_recvfrom($connection, $this->recvBuffers[$fd]['remain_len']);
  287. // 出错了
  288. if('' == $buffer && '' == ($buffer = fread($connection, $this->recvBuffers[$fd]['remain_len'])))
  289. {
  290. if(!feof($connection))
  291. {
  292. return;
  293. }
  294. // 如果该链接对应的buffer有数据,说明发生错误
  295. if(!empty($this->recvBuffers[$fd]['buf']))
  296. {
  297. $this->statusInfo['send_fail']++;
  298. $this->notice("INNER_CLIENT_CLOSE\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  299. }
  300. // 关闭链接
  301. $this->closeInnerClient($fd);
  302. if($this->workerStatus == self::STATUS_SHUTDOWN)
  303. {
  304. $this->stop();
  305. }
  306. return;
  307. }
  308. $this->recvBuffers[$fd]['buf'] .= $buffer;
  309. $remain_len = $this->dealInnerInput($this->recvBuffers[$fd]['buf']);
  310. // 包接收完毕
  311. if(0 === $remain_len)
  312. {
  313. // 执行处理
  314. try{
  315. // 内部通讯业务处理
  316. $this->innerDealProcess($this->recvBuffers[$fd]['buf']);
  317. }
  318. catch(\Exception $e)
  319. {
  320. $this->notice('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage()."\n".$e->getTraceAsString()."\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  321. $this->statusInfo['throw_exception'] ++;
  322. }
  323. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>GatewayProtocol::HEAD_LEN);
  324. }
  325. // 出错
  326. else if(false === $remain_len)
  327. {
  328. // 出错
  329. $this->statusInfo['packet_err']++;
  330. $this->notice("INNER_PACKET_ERROR\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  331. $this->closeInnerClient($fd);
  332. }
  333. else
  334. {
  335. $this->recvBuffers[$fd]['remain_len'] = $remain_len;
  336. }
  337. // 检查是否是关闭状态或者是否到达请求上限
  338. if($this->workerStatus == self::STATUS_SHUTDOWN )
  339. {
  340. // 停止服务
  341. $this->stop();
  342. // EXIT_WAIT_TIME秒后退出进程
  343. pcntl_alarm(self::EXIT_WAIT_TIME);
  344. }
  345. }
  346. /**
  347. * 内部通讯处理
  348. * @param string $recv_str
  349. */
  350. public function innerDealProcess($recv_str)
  351. {
  352. $pack = new GatewayProtocol($recv_str);
  353. $interface_map = array(
  354. GatewayProtocol::CMD_SEND_TO_ONE => 'CMD_SEND_TO_ONE',
  355. GatewayProtocol::CMD_KICK => 'CMD_KICK',
  356. GatewayProtocol::CMD_SEND_TO_ALL => 'CMD_SEND_TO_ALL',
  357. GatewayProtocol::CMD_CONNECT_SUCCESS => 'CMD_CONNECT_SUCCESS',
  358. );
  359. $cmd = $pack->header['cmd'];
  360. StatisticClient::tick();
  361. $module = __CLASS__;
  362. $interface = isset($interface_map[$cmd]) ? $interface_map[$cmd] : 'null';
  363. $success = 1;
  364. $code = 0;
  365. $msg = '';
  366. try
  367. {
  368. switch($cmd)
  369. {
  370. case GatewayProtocol::CMD_SEND_TO_ONE:
  371. $this->sendToSocketId($pack->header['socket_id'], $pack->body);
  372. break;
  373. case GatewayProtocol::CMD_KICK:
  374. if($pack->body)
  375. {
  376. $this->sendToSocketId($pack->header['socket_id'], $pack->body);
  377. }
  378. $this->closeClientBySocketId($pack->header['socket_id']);
  379. break;
  380. case GatewayProtocol::CMD_SEND_TO_ALL:
  381. $this->broadCast($pack->body);
  382. break;
  383. case GatewayProtocol::CMD_CONNECT_SUCCESS:
  384. $this->connectSuccess($pack->header['socket_id'], $pack->header['uid']);
  385. break;
  386. default :
  387. $this->notice('gateway inner pack cmd err data:' .$recv_str );
  388. }
  389. }
  390. catch(\Exception $e)
  391. {
  392. $success = 0;
  393. $code = $e->getCode() > 0 ? $e->getCode() : 500;
  394. $msg = $e->__toString();
  395. }
  396. StatisticClient::report($module, $interface, $success, $code, $msg);
  397. }
  398. /**
  399. * 广播数据
  400. * @param string $bin_data
  401. */
  402. protected function broadCast($bin_data)
  403. {
  404. foreach($this->uidConnMap as $uid=>$conn)
  405. {
  406. $this->sendToSocketId($conn, $bin_data);
  407. }
  408. }
  409. /**
  410. * 根据socket_id关闭与客户端的连接,实际上是踢人操作
  411. * @param int $socket_id
  412. */
  413. protected function closeClientBySocketId($socket_id)
  414. {
  415. if($uid = $this->getUidByFd($socket_id))
  416. {
  417. unset($this->uidConnMap[$uid], $this->connUidMap[$socket_id]);
  418. }
  419. parent::closeClient($socket_id);
  420. }
  421. /**
  422. * 根据uid获取uid对应连接的id
  423. * @param int $uid
  424. */
  425. protected function getFdByUid($uid)
  426. {
  427. if(isset($this->uidConnMap[$uid]))
  428. {
  429. return $this->uidConnMap[$uid];
  430. }
  431. return 0;
  432. }
  433. /**
  434. * 根据连接id获取用户uid
  435. * @param int $fd
  436. */
  437. protected function getUidByFd($fd)
  438. {
  439. if(isset($this->connUidMap[$fd]))
  440. {
  441. return $this->connUidMap[$fd];
  442. }
  443. return 0;
  444. }
  445. /**
  446. * BusinessWorker通知本Gateway进程$uid用户合法,绑定到$socket_id
  447. * 后面这个socketid再有消息传来,会自动带上uid传递给BusinessWorker
  448. * @param int $socket_id
  449. * @param int $uid
  450. */
  451. protected function connectSuccess($socket_id, $uid)
  452. {
  453. $binded_uid = $this->getUidByFd($socket_id);
  454. if($binded_uid)
  455. {
  456. $this->notice('notify connection success fail ' . $socket_id . ' already binded ');
  457. return;
  458. }
  459. $this->uidConnMap[$uid] = $socket_id;
  460. $this->connUidMap[$socket_id] = $uid;
  461. }
  462. /**
  463. * 向某个socketId的连接发送消息
  464. * @param int $socket_id
  465. * @param string $bin_data
  466. */
  467. public function sendToSocketId($socket_id, $bin_data)
  468. {
  469. if(!isset($this->connections[$socket_id]))
  470. {
  471. return false;
  472. }
  473. $this->currentDealFd = $socket_id;
  474. return $this->sendToClient($bin_data);
  475. }
  476. /**
  477. * 用户客户端主动关闭连接触发
  478. * @see Man\Core.SocketWorker::closeClient()
  479. */
  480. protected function closeClient($fd)
  481. {
  482. StatisticClient::tick();
  483. if($uid = $this->getUidByFd($fd))
  484. {
  485. $this->sendToWorker(GatewayProtocol::CMD_ON_CLOSE, $fd);
  486. unset($this->uidConnMap[$uid], $this->connUidMap[$fd]);
  487. }
  488. parent::closeClient($fd);
  489. StatisticClient::report(__CLASS__, 'CMD_ON_CLOSE', 1, 0, '');
  490. }
  491. /**
  492. * 内部通讯socket在BusinessWorker主动关闭连接时触发
  493. * @param int $fd
  494. */
  495. protected function closeInnerClient($fd)
  496. {
  497. unset($this->workerConnections[$fd]);
  498. parent::closeClient($fd);
  499. }
  500. /**
  501. * 随机抽取一个与BusinessWorker的长连接,将数据发给一个BusinessWorker
  502. * @param int $cmd
  503. * @param int $socket_id
  504. * @param string $body
  505. */
  506. protected function sendToWorker($cmd, $socket_id, $body = '')
  507. {
  508. $address= $this->getRemoteAddress($socket_id);
  509. list($client_ip, $client_port) = explode(':', $address, 2);
  510. $pack = new GatewayProtocol();
  511. $pack->header['cmd'] = $cmd;
  512. $pack->header['local_ip'] = $this->lanIp;
  513. $pack->header['local_port'] = $this->lanPort;
  514. $pack->header['socket_id'] = $socket_id;
  515. $pack->header['client_ip'] = $client_ip;
  516. $pack->header['client_port'] = $client_ip;
  517. $pack->header['uid'] = $this->getUidByFd($socket_id);
  518. $pack->body = $body;
  519. return $this->sendBufferToWorker($pack->getBuffer());
  520. }
  521. /**
  522. * 随机抽取一个与BusinessWorker的长连接,将数据发给一个BusinessWorker
  523. * @param string $bin_data
  524. */
  525. protected function sendBufferToWorker($bin_data)
  526. {
  527. if($this->currentDealFd = array_rand($this->workerConnections))
  528. {
  529. $this->sendToClient($bin_data);
  530. }
  531. }
  532. /**
  533. * 打印日志
  534. * @see Man\Core.AbstractWorker::notice()
  535. */
  536. protected function notice($str, $display=true)
  537. {
  538. $str = 'Worker['.get_class($this).']:'."$str ip:".$this->getRemoteIp();
  539. Man\Core\Lib\Log::add($str);
  540. if($display && Man\Core\Lib\Config::get('workerman.debug') == 1)
  541. {
  542. echo $str."\n";
  543. }
  544. }
  545. /**
  546. * 进程停止时,清除一些数据
  547. * @see Man\Core.SocketWorker::onStop()
  548. */
  549. public function onStop()
  550. {
  551. $this->unregisterAddress($this->lanIp.':'.$this->lanPort);
  552. foreach($this->connUidMap as $uid)
  553. {
  554. Store::delete($uid);
  555. }
  556. }
  557. /**
  558. * 向认证的用户发送心跳数据
  559. */
  560. public function ping()
  561. {
  562. $this->broadCast($this->pingData);
  563. }
  564. }