Gateway.php 11 KB

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