BusinessWorker.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. *
  4. * 处理具体逻辑
  5. *
  6. * @author walkor <workerman.net>
  7. *
  8. */
  9. define('ROOT_DIR', realpath(__DIR__.'/../'));
  10. require_once ROOT_DIR . '/Protocols/GatewayProtocol.php';
  11. require_once ROOT_DIR . '/Event.php';
  12. require_once ROOT_DIR . '/Lib/APLog.php';
  13. class BusinessWorker extends Man\Core\SocketWorker
  14. {
  15. /**
  16. * BusinessWorker 实例
  17. * @var BusinessWorker
  18. */
  19. protected static $instance = null;
  20. /**
  21. * 与gateway的连接
  22. * ['ip:port' => conn, 'ip:port' => conn, ...]
  23. * @var array
  24. */
  25. protected static $gatewayConnections = array();
  26. /**
  27. * 进程启动时初始化
  28. * @see Man\Core.SocketWorker::onStart()
  29. */
  30. protected function onStart()
  31. {
  32. // 定时检查与gateway进程的连接
  33. \Man\Core\Lib\Task::init($this->event);
  34. \Man\Core\Lib\Task::add(1, array($this, 'checkGatewayConnections'));
  35. self::$instance = $this;
  36. }
  37. /**
  38. * 获取BusinessWorker实例
  39. * @return BusinessWorker
  40. */
  41. public static function instance()
  42. {
  43. return $this;
  44. }
  45. /**
  46. * 获取与gateway的连接
  47. */
  48. public static function getGatewayConnections()
  49. {
  50. return self::$gatewayConnections;
  51. }
  52. /**
  53. * 检查请求是否完整
  54. * @see Man\Core.SocketWorker::dealInput()
  55. */
  56. public function dealInput($recv_str)
  57. {
  58. return GatewayProtocol::input($recv_str);
  59. }
  60. /**
  61. * 处理请求
  62. * @see Man\Core.SocketWorker::dealProcess()
  63. */
  64. public function dealProcess($recv_str)
  65. {
  66. $pack = new GatewayProtocol($recv_str);
  67. Context::$client_ip = $pack->header['client_ip'];
  68. Context::$client_port = $pack->header['client_port'];
  69. Context::$local_ip = $pack->header['local_ip'];
  70. Context::$local_port = $pack->header['local_port'];
  71. Context::$socket_id = $pack->header['socket_id'];
  72. Context::$uid = $pack->header['uid'];
  73. switch($pack->header['cmd'])
  74. {
  75. case GatewayProtocol::CMD_ON_CONNECTION:
  76. $ret = call_user_func_array(array('Event', 'onConnect'), array($pack->body));
  77. break;
  78. case GatewayProtocol::CMD_ON_MESSAGE:
  79. $ret = call_user_func_array(array('Event', 'onMessage'), array(Context::$uid, $pack->body));
  80. break;
  81. case GatewayProtocol::CMD_ON_CLOSE:
  82. $ret = call_user_func_array(array('Event', 'onClose'), array(Context::$uid));
  83. break;
  84. }
  85. Context::clear();
  86. return $ret;
  87. }
  88. /**
  89. * 定时检查gateway通信端口
  90. */
  91. public function checkGatewayConnections()
  92. {
  93. $key = 'GLOBAL_GATEWAY_ADDRESS';
  94. $addresses_list = Store::get($key);
  95. if(empty($addresses_list))
  96. {
  97. return;
  98. }
  99. foreach($addresses_list as $addr)
  100. {
  101. if(!isset(self::$gatewayConnections[$addr]))
  102. {
  103. $conn = stream_socket_client("tcp://$addr", $errno, $errstr, 1);
  104. if(!$conn)
  105. {
  106. $this->notice($errstr);
  107. continue;
  108. }
  109. self::$gatewayConnections[$addr] = $conn;
  110. stream_set_blocking(self::$gatewayConnections[$addr], 0);
  111. $fd = (int) self::$gatewayConnections[$addr];
  112. $this->connections[$fd] = self::$gatewayConnections[$addr];
  113. $this->recvBuffers[$fd] = array('buf'=>'', 'remain_len'=>$this->prereadLength);
  114. $this->event->add($this->connections[$fd], \Man\Core\Events\BaseEvent::EV_READ , array($this, 'dealInputBase'), $fd);
  115. }
  116. }
  117. }
  118. /**
  119. * 关闭连接
  120. * @see Man\Core.SocketWorker::closeClient()
  121. */
  122. protected function closeClient($fd)
  123. {
  124. foreach(self::$gatewayConnections as $con)
  125. {
  126. $the_fd = (int) $con;
  127. if($the_fd == $fd)
  128. {
  129. unset(self::$gatewayConnections[$fd]);
  130. }
  131. }
  132. parent::closeClient($fd);
  133. }
  134. /**
  135. * 向客户端发送数据
  136. * @see Man\Core.SocketWorker::sendToClient()
  137. */
  138. public function sendToClient($buffer, $fd)
  139. {
  140. $this->currentDealFd = (int)$fd;
  141. parent::sendToClient($buffer);
  142. }
  143. }
  144. /**
  145. * 上下文 包含当前用户uid, 内部通信local_ip local_port socket_id ,以及客户端client_ip client_port
  146. * @author walkor
  147. *
  148. */
  149. class Context
  150. {
  151. public static $series_id;
  152. public static $local_ip;
  153. public static $local_port;
  154. public static $socket_id;
  155. public static $client_ip;
  156. public static $client_port;
  157. public static $uid;
  158. public static function clear()
  159. {
  160. self::$series_id = self::$local_ip = self::$local_port = self::$socket_id = self::$client_ip = self::$client_port = self::$uid = null;
  161. }
  162. }