Gateway.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace GatewayWorker\Lib;
  3. /**
  4. *
  5. * 数据发送相关
  6. * @author walkor <walkor@workerman.net>
  7. *
  8. */
  9. use \Workerman\Protocols\GatewayProtocol;
  10. use \GatewayWorker\Lib\Store;
  11. use \GatewayWorker\Lib\Context;
  12. class Gateway
  13. {
  14. /**
  15. * gateway实例
  16. * @var object
  17. */
  18. protected static $businessWorker = null;
  19. /**
  20. * 向所有客户端(或者client_id_array指定的客户端)广播消息
  21. * @param string $message 向客户端发送的消息
  22. * @param array $client_id_array 客户端id数组
  23. */
  24. public static function sendToAll($message, $client_id_array = null)
  25. {
  26. $gateway_data = GatewayProtocol::$empty;
  27. $gateway_data['cmd'] = GatewayProtocol::CMD_SEND_TO_ALL;
  28. $gateway_data['body'] = $message;
  29. if($client_id_array)
  30. {
  31. $params = array_merge(array('N*'), $client_id_array);
  32. $gateway_data['ext_data'] = call_user_func_array('pack', $params);
  33. }
  34. elseif(empty($client_id_array) && is_array($client_id_array))
  35. {
  36. return;
  37. }
  38. // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
  39. if(self::$businessWorker)
  40. {
  41. foreach(self::$businessWorker->gatewayConnections as $gateway_connection)
  42. {
  43. $gateway_connection->send($gateway_data);
  44. }
  45. }
  46. // 运行在其它环境中,使用udp向worker发送数据
  47. else
  48. {
  49. $all_addresses = Store::instance('gateway')->get('GLOBAL_GATEWAY_ADDRESS');
  50. foreach($all_addresses as $address)
  51. {
  52. self::sendToGateway($address, $gateway_data);
  53. }
  54. }
  55. }
  56. /**
  57. * 向某个客户端发消息
  58. * @param int $client_id
  59. * @param string $message
  60. */
  61. public static function sendToClient($client_id, $message)
  62. {
  63. return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SEND_TO_ONE, $message);
  64. }
  65. /**
  66. * 向当前客户端发送消息
  67. * @param string $message
  68. */
  69. public static function sendToCurrentClient($message)
  70. {
  71. return self::sendCmdAndMessageToClient(null, GatewayProtocol::CMD_SEND_TO_ONE, $message);
  72. }
  73. /**
  74. * 判断某个客户端是否在线
  75. * @param int $client_id
  76. * @return 0/1
  77. */
  78. public static function isOnline($client_id)
  79. {
  80. $address = Store::instance('gateway')->get('gateway-'.$client_id);
  81. if(!$address)
  82. {
  83. return 0;
  84. }
  85. $gateway_data = GatewayProtocol::$empty;
  86. $gateway_data['cmd'] = GatewayProtocol::CMD_IS_ONLINE;
  87. $gateway_data['client_id'] = $client_id;
  88. return self::sendUdpAndRecv($address, $gateway_data);
  89. }
  90. /**
  91. * 获取在线状态,目前返回一个在线client_id数组
  92. * @return array
  93. */
  94. public static function getOnlineStatus()
  95. {
  96. $gateway_data = GatewayProtocol::$empty;
  97. $gateway_data['cmd'] = GatewayProtocol::CMD_GET_ONLINE_STATUS;
  98. $gateway_buffer = GatewayProtocol::encode($gateway_data);
  99. $all_addresses = Store::instance('gateway')->get('GLOBAL_GATEWAY_ADDRESS');
  100. $client_array = $status_data = array();
  101. // 批量向所有gateway进程发送CMD_GET_ONLINE_STATUS命令
  102. foreach($all_addresses as $address)
  103. {
  104. $client = stream_socket_client("udp://$address", $errno, $errmsg);
  105. if(strlen($gateway_buffer) === stream_socket_sendto($client, $gateway_buffer))
  106. {
  107. $client_id = (int) $client;
  108. $client_array[$client_id] = $client;
  109. }
  110. }
  111. // 超时1秒
  112. $time_out = 1;
  113. $time_start = microtime(true);
  114. // 批量接收请求
  115. while(count($client_array) > 0)
  116. {
  117. $write = $except = array();
  118. $read = $client_array;
  119. if(@stream_select($read, $write, $except, $time_out))
  120. {
  121. foreach($read as $client)
  122. {
  123. // udp
  124. $data = json_decode(fread($client, 65535), true);
  125. if($data)
  126. {
  127. $status_data = array_merge($status_data, $data);
  128. }
  129. unset($client_array[$client]);
  130. }
  131. }
  132. if(microtime(true) - $time_start > $time_out)
  133. {
  134. break;
  135. }
  136. }
  137. return $status_data;
  138. }
  139. /**
  140. * 关闭某个客户端
  141. * @param int $client_id
  142. * @param string $message
  143. */
  144. public static function closeClient($client_id)
  145. {
  146. if($client_id === Context::$client_id)
  147. {
  148. return self::kickCurrentClient();
  149. }
  150. // 不是发给当前用户则使用存储中的地址
  151. else
  152. {
  153. $address = Store::instance('gateway')->get('gateway-'.$client_id);
  154. if(!$address)
  155. {
  156. return false;
  157. }
  158. return self::kickAddress($address, $client_id);
  159. }
  160. }
  161. /**
  162. * 踢掉当前客户端
  163. * @param string $message
  164. */
  165. public static function closeCurrentClient()
  166. {
  167. return self::kickAddress(Context::$local_ip.':'.Context::$local_port, Context::$client_id);
  168. }
  169. /**
  170. * 更新session,框架自动调用,开发者不要调用
  171. * @param int $client_id
  172. * @param string $session_str
  173. */
  174. public static function updateSocketSession($client_id, $session_str)
  175. {
  176. $gateway_data = GatewayProtocol::$empty;
  177. $gateway_data['cmd'] = GatewayProtocol::CMD_UPDATE_SESSION;
  178. $gateway_data['client_id'] = $client_id;
  179. $gateway_data['ext_data'] = $session_str;
  180. return self::sendToGateway(Context::$local_ip . ':' . Context::$local_port, $gateway_data);
  181. }
  182. /**
  183. * 想某个用户网关发送命令和消息
  184. * @param int $client_id
  185. * @param int $cmd
  186. * @param string $message
  187. * @return boolean
  188. */
  189. protected static function sendCmdAndMessageToClient($client_id, $cmd , $message)
  190. {
  191. // 如果是发给当前用户则直接获取上下文中的地址
  192. if($client_id === Context::$client_id || $client_id === null)
  193. {
  194. $address = Context::$local_ip.':'.Context::$local_port;
  195. }
  196. else
  197. {
  198. $address = Store::instance('gateway')->get('gateway-'.$client_id);
  199. if(!$address)
  200. {
  201. return false;
  202. }
  203. }
  204. $gateway_data = GatewayProtocol::$empty;
  205. $gateway_data['cmd'] = $cmd;
  206. $gateway_data['client_id'] = $client_id ? $client_id : Context::$client_id;
  207. $gateway_data['body'] = $message;
  208. return self::sendToGateway($address, $gateway_data);
  209. }
  210. /**
  211. * 发送udp数据并返回
  212. * @param int $address
  213. * @param string $message
  214. * @return boolean
  215. */
  216. protected static function sendUdpAndRecv($address , $data)
  217. {
  218. $buffer = GatewayProtocol::encode($data);
  219. // 非workerman环境,使用udp发送数据
  220. $client = stream_socket_client("udp://$address", $errno, $errmsg);
  221. if(strlen($buffer) == stream_socket_sendto($client, $buffer))
  222. {
  223. // 阻塞读
  224. stream_set_blocking($client, 1);
  225. // 1秒超时
  226. stream_set_timeout($client, 1);
  227. // 读udp数据
  228. $data = fread($client, 655350);
  229. // 返回结果
  230. return json_decode($data, true);
  231. }
  232. else
  233. {
  234. throw new \Exception("sendUdpAndRecv($address, \$bufer) fail ! Can not send UDP data!", 502);
  235. }
  236. }
  237. /**
  238. * 发送数据到网关
  239. * @param string $address
  240. * @param string $buffer
  241. */
  242. protected static function sendToGateway($address, $gateway_data)
  243. {
  244. // 有$businessWorker说明是workerman环境,使用$businessWorker发送数据
  245. if(self::$businessWorker)
  246. {
  247. if(!isset(self::$businessWorker->gatewayConnections[$address]))
  248. {
  249. return false;
  250. }
  251. return self::$businessWorker->gatewayConnections[$address]->send($gateway_data);
  252. }
  253. // 非workerman环境,使用udp发送数据
  254. $gateway_buffer = GatewayProtocol::encode($gateway_data);
  255. $client = stream_socket_client("udp://$address", $errno, $errmsg);
  256. return strlen($gateway_buffer) == stream_socket_sendto($client, $gateway_buffer);
  257. }
  258. /**
  259. * 踢掉某个网关的socket
  260. * @param string $local_ip
  261. * @param int $local_port
  262. * @param int $client_id
  263. * @param string $message
  264. * @param int $client_id
  265. */
  266. protected static function kickAddress($address, $client_id)
  267. {
  268. $gateway_data = GatewayProtocol::$empty;
  269. $gateway_data['cmd'] = GatewayProtocol::CMD_KICK;
  270. $gateway_data['client_id'] = $client_id;
  271. return self::sendToGateway($address, $gateway_data);
  272. }
  273. /**
  274. * 设置gateway实例
  275. * @param Bootstrap/Gateway $gateway_instance
  276. */
  277. public static function setBusinessWorker($business_worker_instance)
  278. {
  279. self::$businessWorker = $business_worker_instance;
  280. }
  281. }