Gateway.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. class Gateway extends Man\Core\SocketWorker
  15. {
  16. /**
  17. * 内部通信socket udp
  18. * @var resouce
  19. */
  20. protected $innerMainSocketUdp = null;
  21. /**
  22. * 内部通信socket tcp
  23. * @var resouce
  24. */
  25. protected $innerMainSocketTcp = null;
  26. /**
  27. * 内网ip
  28. * @var string
  29. */
  30. protected $lanIp = '127.0.0.1';
  31. /**
  32. * 内部通信端口
  33. * @var int
  34. */
  35. protected $lanPort = 0;
  36. /**
  37. * uid到连接的映射
  38. * @var array
  39. */
  40. protected $uidConnMap = array();
  41. /**
  42. * 连接到uid的映射
  43. * @var array
  44. */
  45. protected $connUidMap = array();
  46. /**
  47. * 到Worker的通信地址
  48. * @var array
  49. */
  50. protected $workerAddresses = array();
  51. /**
  52. * 与worker的连接
  53. * [fd=>fd, $fd=>fd, ..]
  54. * @var array
  55. */
  56. protected $workerConnections = array();
  57. /**
  58. * gateway 发送心跳时间间隔 单位:秒 ,0表示不发送心跳,在配置中设置
  59. * @var integer
  60. */
  61. protected $pingInterval = 0;
  62. /**
  63. * 心跳数据
  64. * 可以是字符串(在配置中直接设置字符串如 ping_data=ping),
  65. * 可以是二进制数据(二进制数据保存在文件中,在配置中设置ping数据文件路径 如 ping_data=/yourpath/ping.bin)
  66. * ping数据应该是客户端能够识别的数据格式,只是检测连接的连通性,客户端收到心跳数据可以选择忽略此数据包
  67. * @var string
  68. */
  69. protected $pingData = '';
  70. /**
  71. * 进程启动
  72. */
  73. public function start()
  74. {
  75. // 安装信号处理函数
  76. $this->installSignal();
  77. // 添加accept事件
  78. $ret = $this->event->add($this->mainSocket, Man\Core\Events\BaseEvent::EV_READ, array($this, 'accept'));
  79. // 创建内部通信套接字
  80. $start_port = Man\Core\Lib\Config::get($this->workerName.'.lan_port_start');
  81. $this->lanPort = $start_port - posix_getppid() + posix_getpid();
  82. $this->lanIp = Man\Core\Lib\Config::get($this->workerName.'.lan_ip');
  83. if(!$this->lanIp)
  84. {
  85. $this->notice($this->workerName.'.lan_ip not set');
  86. $this->lanIp = '127.0.0.1';
  87. }
  88. $error_no_udp = $error_no_tcp = 0;
  89. $error_msg_udp = $error_msg_tcp = '';
  90. $this->innerMainSocketUdp = stream_socket_server("udp://".$this->lanIp.':'.$this->lanPort, $error_no_udp, $error_msg_udp, STREAM_SERVER_BIND);
  91. $this->innerMainSocketTcp = stream_socket_server("tcp://".$this->lanIp.':'.$this->lanPort, $error_no_tcp, $error_msg_tcp, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
  92. if(!$this->innerMainSocketUdp || !$this->innerMainSocketTcp)
  93. {
  94. $this->notice('create innerMainSocket udp or tcp fail and exit '.$error_msg_udp.$error_msg_tcp);
  95. sleep(1);
  96. exit(0);
  97. }
  98. else
  99. {
  100. stream_set_blocking($this->innerMainSocketUdp , 0);
  101. stream_set_blocking($this->innerMainSocketTcp , 0);
  102. }
  103. // 注册套接字
  104. $this->registerAddress($this->lanIp.':'.$this->lanPort);
  105. // 添加读udp/tcp事件
  106. $this->event->add($this->innerMainSocketUdp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'recvUdp'));
  107. $this->event->add($this->innerMainSocketTcp, Man\Core\Events\BaseEvent::EV_READ, array($this, 'acceptTcp'));
  108. // 初始化到worker的通信地址
  109. $this->initWorkerAddresses();
  110. // 初始化心跳包时间间隔
  111. $ping_interval = \Man\Core\Lib\Config::get($this->workerName.'.ping_interval');
  112. if((int)$ping_interval > 0)
  113. {
  114. $this->pingInterval = (int)$ping_interval;
  115. }
  116. // 获取心跳包数据
  117. $ping_data_or_path = \Man\Core\Lib\Config::get($this->workerName.'.ping_data');
  118. if(is_file($ping_data_or_path))
  119. {
  120. $this->pingData = file_get_contents($ping_data_or_path);
  121. }
  122. else
  123. {
  124. $this->pingData = $ping_data_or_path;
  125. }
  126. // 设置定时任务,发送心跳
  127. if($this->pingInterval > 0 && $this->pingData)
  128. {
  129. \Man\Core\Lib\Task::init($this->event);
  130. \Man\Core\Lib\Task::add($this->pingInterval, array($this, 'ping'));
  131. }
  132. // 主体循环,整个子进程会阻塞在这个函数上
  133. $ret = $this->event->loop();
  134. $this->notice('worker loop exit');
  135. exit(0);
  136. }
  137. /**
  138. * 存储全局的通信地址
  139. * @param string $address
  140. */
  141. protected function registerAddress($address)
  142. {
  143. \Man\Core\Lib\Mutex::get();
  144. $key = 'GLOBAL_GATEWAY_ADDRESS';
  145. $addresses_list = Store::get($key);
  146. if(empty($addresses_list))
  147. {
  148. $addresses_list = array();
  149. }
  150. $addresses_list[$address] = $address;
  151. Store::set($key, $addresses_list);
  152. \Man\Core\Lib\Mutex::release();
  153. }
  154. /**
  155. * 删除全局的通信地址
  156. * @param string $address
  157. */
  158. protected function unregisterAddress($address)
  159. {
  160. \Man\Core\Lib\Mutex::get();
  161. $key = 'GLOBAL_GATEWAY_ADDRESS';
  162. $addresses_list = Store::get($key);
  163. if(empty($addresses_list))
  164. {
  165. $addresses_list = array();
  166. }
  167. unset($addresses_list[$address]);
  168. Store::set($key, $addresses_list);
  169. \Man\Core\Lib\Mutex::release();
  170. }
  171. /**
  172. * 接收Udp数据
  173. * 如果数据超过一个udp包长,需要业务自己解析包体,判断数据是否全部到达
  174. * @param resource $socket
  175. * @param $null_one $flag
  176. * @param $null_two $base
  177. * @return void
  178. */
  179. public function recvUdp($socket, $null_one = null, $null_two = null)
  180. {
  181. $data = stream_socket_recvfrom($socket , self::MAX_UDP_PACKEG_SIZE, 0, $address);
  182. // 惊群效应
  183. if(false === $data || empty($address))
  184. {
  185. return false;
  186. }
  187. $this->currentClientAddress = $address;
  188. $this->innerDealProcess($data);
  189. }
  190. public function acceptTcp($socket, $null_one = null, $null_two = null)
  191. {
  192. // 获得一个连接
  193. $new_connection = @stream_socket_accept($socket, 0);
  194. // 可能是惊群效应
  195. if(false === $new_connection)
  196. {
  197. return false;
  198. }
  199. // 连接的fd序号
  200. $fd = (int) $new_connection;
  201. $this->connections[$fd] = $new_connection;
  202. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>GatewayProtocol::HEAD_LEN);
  203. // 非阻塞
  204. stream_set_blocking($this->connections[$fd], 0);
  205. $this->event->add($this->connections[$fd], \Man\Core\Events\BaseEvent::EV_READ , array($this, 'recvTcp'), $fd);
  206. $this->workerConnections[$fd] = $fd;
  207. return $new_connection;
  208. }
  209. public function dealInnerInput($buffer)
  210. {
  211. return GatewayProtocol::input($buffer);
  212. }
  213. /**
  214. * 处理受到的数据
  215. * @param event_buffer $event_buffer
  216. * @param int $fd
  217. * @return void
  218. */
  219. public function recvTcp($connection, $flag, $fd = null)
  220. {
  221. $this->currentDealFd = $fd;
  222. $buffer = stream_socket_recvfrom($connection, $this->recvBuffers[$fd]['remain_len']);
  223. // 出错了
  224. if('' == $buffer && '' == ($buffer = fread($connection, $this->recvBuffers[$fd]['remain_len'])))
  225. {
  226. if(!feof($connection))
  227. {
  228. return;
  229. }
  230. // 如果该链接对应的buffer有数据,说明发生错误
  231. if(!empty($this->recvBuffers[$fd]['buf']))
  232. {
  233. $this->statusInfo['send_fail']++;
  234. $this->notice("INNER_CLIENT_CLOSE\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  235. }
  236. // 关闭链接
  237. $this->closeInnerClient($fd);
  238. if($this->workerStatus == self::STATUS_SHUTDOWN)
  239. {
  240. $this->stop();
  241. }
  242. return;
  243. }
  244. $this->recvBuffers[$fd]['buf'] .= $buffer;
  245. $remain_len = $this->dealInnerInput($this->recvBuffers[$fd]['buf']);
  246. // 包接收完毕
  247. if(0 === $remain_len)
  248. {
  249. // 执行处理
  250. try{
  251. // 业务处理
  252. $this->innerDealProcess($this->recvBuffers[$fd]['buf']);
  253. }
  254. catch(\Exception $e)
  255. {
  256. $this->notice('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage()."\n".$e->getTraceAsString()."\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  257. $this->statusInfo['throw_exception'] ++;
  258. }
  259. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>$this->prereadLength);
  260. }
  261. // 出错
  262. else if(false === $remain_len)
  263. {
  264. // 出错
  265. $this->statusInfo['packet_err']++;
  266. $this->notice("INNER_PACKET_ERROR\nCLIENT_IP:".$this->getRemoteIp()."\nBUFFER:[".var_export($this->recvBuffers[$fd]['buf'],true)."]\n");
  267. $this->closeInnerClient($fd);
  268. }
  269. else
  270. {
  271. $this->recvBuffers[$fd]['remain_len'] = $remain_len;
  272. }
  273. // 检查是否是关闭状态或者是否到达请求上限
  274. if($this->workerStatus == self::STATUS_SHUTDOWN )
  275. {
  276. // 停止服务
  277. $this->stop();
  278. // EXIT_WAIT_TIME秒后退出进程
  279. pcntl_alarm(self::EXIT_WAIT_TIME);
  280. }
  281. }
  282. protected function initWorkerAddresses()
  283. {
  284. $this->workerAddresses = Man\Core\Lib\Config::get($this->workerName.'.business_worker');
  285. if(!$this->workerAddresses)
  286. {
  287. $this->notice($this->workerName.'business_worker not set');
  288. }
  289. }
  290. public function dealInput($recv_str)
  291. {
  292. return 0;
  293. }
  294. public function innerDealProcess($recv_str)
  295. {
  296. $pack = new GatewayProtocol($recv_str);
  297. switch($pack->header['cmd'])
  298. {
  299. case GatewayProtocol::CMD_SEND_TO_ONE:
  300. return $this->sendToSocketId($pack->header['socket_id'], $pack->body);
  301. case GatewayProtocol::CMD_KICK:
  302. if($pack->body)
  303. {
  304. $this->sendToSocketId($pack->header['socket_id'], $pack->body);
  305. }
  306. return $this->closeClientBySocketId($pack->header['socket_id']);
  307. case GatewayProtocol::CMD_SEND_TO_ALL:
  308. return $this->broadCast($pack->body);
  309. case GatewayProtocol::CMD_CONNECT_SUCCESS:
  310. return $this->connectSuccess($pack->header['socket_id'], $pack->header['uid']);
  311. default :
  312. $this->notice('gateway inner pack cmd err data:' .$recv_str );
  313. }
  314. }
  315. protected function broadCast($bin_data)
  316. {
  317. foreach($this->uidConnMap as $uid=>$conn)
  318. {
  319. $this->sendToSocketId($conn, $bin_data);
  320. }
  321. }
  322. protected function closeClientBySocketId($socket_id)
  323. {
  324. if($uid = $this->getUidByFd($socket_id))
  325. {
  326. unset($this->uidConnMap[$uid], $this->connUidMap[$socket_id]);
  327. }
  328. parent::closeClient($socket_id);
  329. }
  330. protected function getFdByUid($uid)
  331. {
  332. if(isset($this->uidConnMap[$uid]))
  333. {
  334. return $this->uidConnMap[$uid];
  335. }
  336. return 0;
  337. }
  338. protected function getUidByFd($fd)
  339. {
  340. if(isset($this->connUidMap[$fd]))
  341. {
  342. return $this->connUidMap[$fd];
  343. }
  344. return 0;
  345. }
  346. protected function connectSuccess($socket_id, $uid)
  347. {
  348. $binded_uid = $this->getUidByFd($socket_id);
  349. if($binded_uid)
  350. {
  351. $this->notice('notify connection success fail ' . $socket_id . ' already binded ');
  352. return;
  353. }
  354. $this->uidConnMap[$uid] = $socket_id;
  355. $this->connUidMap[$socket_id] = $uid;
  356. }
  357. public function sendToSocketId($socket_id, $bin_data)
  358. {
  359. if(!isset($this->connections[$socket_id]))
  360. {
  361. return false;
  362. }
  363. $this->currentDealFd = $socket_id;
  364. return $this->sendToClient($bin_data);
  365. }
  366. protected function closeClient($fd)
  367. {
  368. if($uid = $this->getUidByFd($fd))
  369. {
  370. $this->sendToWorker(GatewayProtocol::CMD_ON_CLOSE, $fd);
  371. unset($this->uidConnMap[$uid], $this->connUidMap[$fd]);
  372. }
  373. parent::closeClient($fd);
  374. }
  375. protected function closeInnerClient($fd)
  376. {
  377. unset($this->workerConnections[$fd]);
  378. parent::closeClient($fd);
  379. }
  380. public function dealProcess($recv_str)
  381. {
  382. // 判断用户是否认证过
  383. $from_uid = $this->getUidByFd($this->currentDealFd);
  384. // 触发ON_CONNECTION
  385. if(!$from_uid)
  386. {
  387. return $this->sendToWorker(GatewayProtocol::CMD_ON_CONNECTION, $this->currentDealFd, $recv_str);
  388. }
  389. // 认证过, 触发ON_MESSAGE
  390. $this->sendToWorker(GatewayProtocol::CMD_ON_MESSAGE, $this->currentDealFd, $recv_str);
  391. }
  392. protected function sendToWorker($cmd, $socket_id, $body = '')
  393. {
  394. $address= $this->getRemoteAddress($socket_id);
  395. list($client_ip, $client_port) = explode(':', $address, 2);
  396. $pack = new GatewayProtocol();
  397. $pack->header['cmd'] = $cmd;
  398. $pack->header['local_ip'] = $this->lanIp;
  399. $pack->header['local_port'] = $this->lanPort;
  400. $pack->header['socket_id'] = $socket_id;
  401. $pack->header['client_ip'] = $client_ip;
  402. $pack->header['client_port'] = $client_ip;
  403. $pack->header['uid'] = $this->getUidByFd($socket_id);
  404. $pack->body = $body;
  405. return $this->sendBufferToWorker($pack->getBuffer());
  406. }
  407. protected function sendBufferToWorker($bin_data)
  408. {
  409. $this->currentDealFd = array_rand($this->workerConnections);
  410. return $this->sendToClient($bin_data);
  411. }
  412. protected function notice($str, $display=true)
  413. {
  414. $str = 'Worker['.get_class($this).']:'."$str ip:".$this->getRemoteIp();
  415. Man\Core\Lib\Log::add($str);
  416. if($display && Man\Core\Lib\Config::get('workerman.debug') == 1)
  417. {
  418. echo $str."\n";
  419. }
  420. }
  421. public function onStop()
  422. {
  423. $this->unregisterAddress($this->lanIp.':'.$this->lanPort);
  424. foreach($this->connUidMap as $uid)
  425. {
  426. Store::delete($uid);
  427. }
  428. }
  429. public function ping()
  430. {
  431. $this->broadCast($this->pingData);
  432. }
  433. }