Gateway.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. class GateWay
  18. {
  19. /**
  20. * gateway实例
  21. * @var object
  22. */
  23. protected static $businessWorker = null;
  24. /**
  25. * 设置gateway实例,用于与
  26. * @param unknown_type $gateway_instance
  27. */
  28. public static function setBusinessWorker($business_worker_instance)
  29. {
  30. self::$businessWorker = $business_worker_instance;
  31. }
  32. /**
  33. * 向所有客户端广播消息
  34. * @param string $message
  35. */
  36. public static function sendToAll($message)
  37. {
  38. $pack = new GatewayProtocol();
  39. $pack->header['cmd'] = GatewayProtocol::CMD_SEND_TO_ALL;
  40. $pack->header['series_id'] = 0;
  41. $pack->header['local_ip'] = Context::$local_ip;
  42. $pack->header['local_port'] = Context::$local_port;
  43. $pack->header['socket_id'] = Context::$socket_id;
  44. $pack->header['client_ip'] = Context::$client_ip;
  45. $pack->header['client_port'] = Context::$client_port;
  46. $pack->header['uid'] = Context::$uid;
  47. $pack->body = (string)$message;
  48. $buffer = $pack->getBuffer();
  49. // 如果有businessWorker实例,说明运行在workerman环境中,通过businessWorker中的长连接发送数据
  50. if(self::$businessWorker)
  51. {
  52. foreach(self::$businessWorker->getGatewayConnections() as $con)
  53. {
  54. self::$businessWorker->sendToClient($buffer, $con);
  55. }
  56. }
  57. // 运行在其它环境中,使用udp向worker发送数据
  58. else
  59. {
  60. $all_addresses = Store::get('GLOBAL_GATEWAY_ADDRESS');
  61. foreach($all_addresses as $address)
  62. {
  63. self::sendToGateway($address, $buffer);
  64. }
  65. }
  66. }
  67. /**
  68. * 向某个用户发消息
  69. * @param int $uid
  70. * @param string $message
  71. */
  72. public static function sendToUid($uid, $message)
  73. {
  74. return self::sendCmdAndMessageToUid($uid, GatewayProtocol::CMD_SEND_TO_ONE, $message);
  75. }
  76. /**
  77. * 向当前用户发送消息
  78. * @param string $message
  79. */
  80. public static function sendToCurrentUid($message)
  81. {
  82. return self::sendCmdAndMessageToUid(null, GatewayProtocol::CMD_SEND_TO_ONE, $message);
  83. }
  84. /**
  85. * 将某个用户踢出
  86. * @param int $uid
  87. * @param string $message
  88. */
  89. public static function kickUid($uid, $message)
  90. {
  91. if($uid === Context::$uid)
  92. {
  93. return self::kickCurrentUser($message);
  94. }
  95. // 不是发给当前用户则使用存储中的地址
  96. else
  97. {
  98. $address = self::getAddressByUid($uid);
  99. if(!$address)
  100. {
  101. return false;
  102. }
  103. return self::kickAddress($address['local_ip'], $address['local_port'], $address['socket_id'], $message);
  104. }
  105. }
  106. /**
  107. * 踢掉当前用户
  108. * @param string $message
  109. */
  110. public static function kickCurrentUser($message)
  111. {
  112. return self::kickAddress(Context::$local_ip, Context::$local_port, Context::$socket_id, $message);
  113. }
  114. /**
  115. * 想某个用户网关发送命令和消息
  116. * @param int $uid
  117. * @param int $cmd
  118. * @param string $message
  119. * @return boolean
  120. */
  121. public static function sendCmdAndMessageToUid($uid, $cmd , $message)
  122. {
  123. $pack = new GatewayProtocol();
  124. $pack->header['cmd'] = $cmd;
  125. $pack->header['series_id'] = Context::$series_id > 0 ? Context::$series_id : 0;
  126. // 如果是发给当前用户则直接获取上下文中的地址
  127. if($uid === Context::$uid || $uid === null)
  128. {
  129. $pack->header['local_ip'] = Context::$local_ip;
  130. $pack->header['local_port'] = Context::$local_port;
  131. $pack->header['socket_id'] = Context::$socket_id;
  132. }
  133. // 不是发给当前用户则使用存储中的地址
  134. else
  135. {
  136. $address = self::getAddressByUid($uid);
  137. if(!$address)
  138. {
  139. return false;
  140. }
  141. $pack->header['local_ip'] = $address['local_ip'];
  142. $pack->header['local_port'] = $address['local_port'];
  143. $pack->header['socket_id'] = $address['socket_id'];
  144. }
  145. $pack->header['client_ip'] = Context::$client_ip;
  146. $pack->header['client_port'] = Context::$client_port;
  147. $pack->header['uid'] = empty($uid) ? 0 : $uid;
  148. $pack->body = (string)$message;
  149. return self::sendToGateway("{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer());
  150. }
  151. /**
  152. * 踢掉某个网关的socket
  153. * @param string $local_ip
  154. * @param int $local_port
  155. * @param int $socket_id
  156. * @param string $message
  157. * @param int $uid
  158. */
  159. public static function kickAddress($local_ip, $local_port, $socket_id, $message, $uid = null)
  160. {
  161. $pack = new GatewayProtocol();
  162. $pack->header['cmd'] = GatewayProtocol::CMD_KICK;
  163. $pack->header['series_id'] = Context::$series_id > 0 ? Context::$series_id : 0;
  164. $pack->header['local_ip'] = $local_ip;
  165. $pack->header['local_port'] = $local_port;
  166. $pack->header['socket_id'] = $socket_id;
  167. if(null !== Context::$client_ip)
  168. {
  169. $pack->header['client_ip'] = Context::$client_ip;
  170. $pack->header['client_port'] = Context::$client_port;
  171. }
  172. $pack->header['uid'] = $uid ? $uid : 0;
  173. $pack->body = (string)$message;
  174. return self::sendToGateway("{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer());
  175. }
  176. /**
  177. * 存储uid的网关地址
  178. * @param int $uid
  179. */
  180. public static function storeUid($uid)
  181. {
  182. $address = array('local_ip'=>Context::$local_ip, 'local_port'=>Context::$local_port, 'socket_id'=>Context::$socket_id);
  183. Store::set($uid, $address);
  184. }
  185. /**
  186. * 获取用户的网关地址
  187. * @param int $uid
  188. */
  189. public static function getAddressByUid($uid)
  190. {
  191. return Store::get($uid);
  192. }
  193. /**
  194. * 删除用户的网关地址
  195. * @param int $uid
  196. */
  197. public static function deleteUidAddress($uid)
  198. {
  199. return Store::delete($uid);
  200. }
  201. /**
  202. * 通知网关uid链接成功(通过验证)
  203. * @param int $uid
  204. */
  205. public static function notifyConnectionSuccess($uid)
  206. {
  207. return self::sendCmdAndMessageToUid($uid, GatewayProtocol::CMD_CONNECT_SUCCESS, '');
  208. }
  209. /**
  210. * 发送数据到网关
  211. * @param string $address
  212. * @param string $buffer
  213. */
  214. public static function sendToGateway($address, $buffer)
  215. {
  216. // 有$businessWorker说明是workerman环境,使用$businessWorker发送数据
  217. if(self::$businessWorker)
  218. {
  219. $connections = self::$businessWorker->getGatewayConnections();
  220. if(!isset($connections[$address]))
  221. {
  222. $e = new \Exception("sendToGateway($address, $buffer) fail \$connections:".json_encode($connections));
  223. APLog::add($e->__toString());
  224. return false;
  225. }
  226. return self::$businessWorker->sendToClient($buffer, $connections[$address]);
  227. }
  228. // 非workerman环境,使用udp发送数据
  229. $client = stream_socket_client("udp://$address", $errno, $errmsg);
  230. return strlen($buffer) == stream_socket_sendto($client, $buffer);
  231. }
  232. }