Gateway.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. *
  4. * 数据发送相关
  5. * sendToAll sendToUid
  6. * @author walkor <workerman.net>
  7. *
  8. */
  9. if(!defined('ROOT_DIR'))
  10. {
  11. define('ROOT_DIR', __DIR__."/../");
  12. }
  13. require_once ROOT_DIR . '/Lib/Store.php';
  14. require_once ROOT_DIR . '/Lib/Context.php';
  15. require_once ROOT_DIR . '/Lib/APLog.php';
  16. require_once ROOT_DIR . '/Protocols/GatewayProtocol.php';
  17. require_once ROOT_DIR . '/Protocols/JsonProtocol.php';
  18. class GateWay
  19. {
  20. /**
  21. * gateway实例
  22. * @var object
  23. */
  24. protected static $businessWorker = null;
  25. /**
  26. * 设置gateway实例,用于与
  27. * @param unknown_type $gateway_instance
  28. */
  29. public static function setBusinessWorker($business_worker_instance)
  30. {
  31. self::$businessWorker = $business_worker_instance;
  32. }
  33. /**
  34. * 向所有客户端广播消息
  35. * @param string $message
  36. */
  37. public static function sendToAll($message)
  38. {
  39. $pack = new GatewayProtocol();
  40. $pack->header['cmd'] = GatewayProtocol::CMD_SEND_TO_ALL;
  41. $pack->header['series_id'] = 0;
  42. $pack->header['local_ip'] = Context::$local_ip;
  43. $pack->header['local_port'] = Context::$local_port;
  44. $pack->header['socket_id'] = Context::$socket_id;
  45. $pack->header['client_ip'] = Context::$client_ip;
  46. $pack->header['client_port'] = Context::$client_port;
  47. $pack->header['uid'] = Context::$uid;
  48. $pack->body = (string)$message;
  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::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. * @param string $message
  89. */
  90. public static function kickUid($uid, $message)
  91. {
  92. if($uid === Context::$uid)
  93. {
  94. return self::kickCurrentUser($message);
  95. }
  96. // 不是发给当前用户则使用存储中的地址
  97. else
  98. {
  99. $address = self::getAddressByUid($uid);
  100. if(!$address)
  101. {
  102. return false;
  103. }
  104. return self::kickAddress($address['local_ip'], $address['local_port'], $address['socket_id'], $message);
  105. }
  106. }
  107. /**
  108. * 踢掉当前用户
  109. * @param string $message
  110. */
  111. public static function kickCurrentUser($message)
  112. {
  113. return self::kickAddress(Context::$local_ip, Context::$local_port, Context::$socket_id, $message);
  114. }
  115. /**
  116. * 想某个用户网关发送命令和消息
  117. * @param int $uid
  118. * @param int $cmd
  119. * @param string $message
  120. * @return boolean
  121. */
  122. public static function sendCmdAndMessageToUid($uid, $cmd , $message)
  123. {
  124. $pack = new GatewayProtocol();
  125. $pack->header['cmd'] = $cmd;
  126. $pack->header['series_id'] = Context::$series_id > 0 ? Context::$series_id : 0;
  127. // 如果是发给当前用户则直接获取上下文中的地址
  128. if($uid === Context::$uid || $uid === null)
  129. {
  130. $pack->header['local_ip'] = Context::$local_ip;
  131. $pack->header['local_port'] = Context::$local_port;
  132. $pack->header['socket_id'] = Context::$socket_id;
  133. }
  134. // 不是发给当前用户则使用存储中的地址
  135. else
  136. {
  137. $address = self::getAddressByUid($uid);
  138. if(!$address)
  139. {
  140. return false;
  141. }
  142. $pack->header['local_ip'] = $address['local_ip'];
  143. $pack->header['local_port'] = $address['local_port'];
  144. $pack->header['socket_id'] = $address['socket_id'];
  145. }
  146. $pack->header['client_ip'] = Context::$client_ip;
  147. $pack->header['client_port'] = Context::$client_port;
  148. $pack->header['uid'] = empty($uid) ? 0 : $uid;
  149. $pack->body = (string)$message;
  150. return self::sendToGateway("{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer());
  151. }
  152. /**
  153. * 踢掉某个网关的socket
  154. * @param string $local_ip
  155. * @param int $local_port
  156. * @param int $socket_id
  157. * @param string $message
  158. * @param int $uid
  159. */
  160. public static function kickAddress($local_ip, $local_port, $socket_id, $message, $uid = null)
  161. {
  162. $pack = new GatewayProtocol();
  163. $pack->header['cmd'] = GatewayProtocol::CMD_KICK;
  164. $pack->header['series_id'] = Context::$series_id > 0 ? Context::$series_id : 0;
  165. $pack->header['local_ip'] = $local_ip;
  166. $pack->header['local_port'] = $local_port;
  167. $pack->header['socket_id'] = $socket_id;
  168. if(null !== Context::$client_ip)
  169. {
  170. $pack->header['client_ip'] = Context::$client_ip;
  171. $pack->header['client_port'] = Context::$client_port;
  172. }
  173. $pack->header['uid'] = $uid ? $uid : 0;
  174. $pack->body = (string)$message;
  175. return self::sendToGateway("{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer());
  176. }
  177. /**
  178. * 存储uid的网关地址
  179. * @param int $uid
  180. */
  181. public static function storeUid($uid)
  182. {
  183. $address = array('local_ip'=>Context::$local_ip, 'local_port'=>Context::$local_port, 'socket_id'=>Context::$socket_id);
  184. Store::set($uid, $address);
  185. }
  186. /**
  187. * 获取用户的网关地址
  188. * @param int $uid
  189. */
  190. public static function getAddressByUid($uid)
  191. {
  192. return Store::get($uid);
  193. }
  194. /**
  195. * 删除用户的网关地址
  196. * @param int $uid
  197. */
  198. public static function deleteUidAddress($uid)
  199. {
  200. return Store::delete($uid);
  201. }
  202. /**
  203. * 通知网关uid链接成功(通过验证)
  204. * @param int $uid
  205. */
  206. public static function notifyConnectionSuccess($uid)
  207. {
  208. return self::sendCmdAndMessageToUid($uid, GatewayProtocol::CMD_CONNECT_SUCCESS, '');
  209. }
  210. /**
  211. * 发送数据到网关
  212. * @param string $address
  213. * @param string $buffer
  214. */
  215. public static function sendToGateway($address, $buffer)
  216. {
  217. // 有$businessWorker说明是workerman环境,使用$businessWorker发送数据
  218. if(self::$businessWorker)
  219. {
  220. $connections = self::$businessWorker->getGatewayConnections();
  221. if(!isset($connections[$address]))
  222. {
  223. $e = new \Exception("sendToGateway($address, $buffer) fail \$connections:".json_encode($connections));
  224. APLog::add($e->__toString());
  225. return false;
  226. }
  227. return self::$businessWorker->sendToClient($buffer, $connections[$address]);
  228. }
  229. // 非workerman环境,使用udp发送数据
  230. $client = stream_socket_client("udp://$address", $errno, $errmsg);
  231. return strlen($buffer) == stream_socket_sendto($client, $buffer);
  232. }
  233. }