Gateway.php 19 KB

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