Gateway.php 19 KB

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