Gateway.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. *
  4. * 数据发送相关
  5. * sendToAll sendToUid
  6. * @author walkor <workerman.net>
  7. *
  8. */
  9. require_once ROOT_DIR . '/Lib/Store.php';
  10. class GateWay
  11. {
  12. /**
  13. * 向所有客户端广播消息
  14. * @param string $message
  15. */
  16. public static function sendToAll($message)
  17. {
  18. $pack = new GatewayProtocol();
  19. $pack->header['cmd'] = GatewayProtocol::CMD_SEND_TO_ALL;
  20. $pack->header['series_id'] = 0;
  21. $pack->header['local_ip'] = Context::$local_ip;
  22. $pack->header['local_port'] = Context::$local_port;
  23. $pack->header['socket_id'] = Context::$socket_id;
  24. $pack->header['client_ip'] = Context::$client_ip;
  25. $pack->header['client_port'] = Context::$client_port;
  26. $pack->header['uid'] = Context::$uid;
  27. $pack->body = (string)$message;
  28. $buffer = $pack->getBuffer();
  29. if(empty(Context::$protocol))
  30. {
  31. Context::$protocol = 'tcp';
  32. }
  33. $all_addresses = Store::get('GLOBAL_GATEWAY_ADDRESS-' . Context::$protocol);
  34. foreach($all_addresses as $address)
  35. {
  36. self::sendToGateway($address, $buffer);
  37. }
  38. }
  39. /**
  40. * 向某个用户发消息
  41. * @param int $uid
  42. * @param string $message
  43. */
  44. public static function sendToUid($uid, $message)
  45. {
  46. return self::sendCmdAndMessageToUid($uid, GatewayProtocol::CMD_SEND_TO_ONE, $message);
  47. }
  48. /**
  49. * 向当前用户发送消息
  50. * @param string $message
  51. */
  52. public static function sendToCurrentUid($message)
  53. {
  54. return self::sendCmdAndMessageToUid(null, GatewayProtocol::CMD_SEND_TO_ONE, $message);
  55. }
  56. /**
  57. * 将某个用户踢出
  58. * @param int $uid
  59. * @param string $message
  60. */
  61. public static function kickUid($uid, $message)
  62. {
  63. if($uid === Context::$uid)
  64. {
  65. return self::kickCurrentUser($message);
  66. }
  67. // 不是发给当前用户则使用存储中的地址
  68. else
  69. {
  70. $address = self::getAddressByUid($uid);
  71. if(!$address)
  72. {
  73. return false;
  74. }
  75. return self::kickAddress($address['local_ip'], $address['local_port'], $address['socket_id'], $message);
  76. }
  77. }
  78. /**
  79. * 踢掉当前用户
  80. * @param string $message
  81. */
  82. public static function kickCurrentUser($message)
  83. {
  84. return self::kickAddress(Context::$local_ip, Context::$local_port, Context::$socket_id, $message);
  85. }
  86. /**
  87. * 想某个用户网关发送命令和消息
  88. * @param int $uid
  89. * @param int $cmd
  90. * @param string $message
  91. * @return boolean
  92. */
  93. public static function sendCmdAndMessageToUid($uid, $cmd , $message)
  94. {
  95. $pack = new GatewayProtocol();
  96. $pack->header['cmd'] = $cmd;
  97. $pack->header['series_id'] = Context::$series_id > 0 ? Context::$series_id : 0;
  98. // 如果是发给当前用户则直接获取上下文中的地址
  99. if($uid === Context::$uid || $uid === null)
  100. {
  101. $pack->header['local_ip'] = Context::$local_ip;
  102. $pack->header['local_port'] = Context::$local_port;
  103. $pack->header['socket_id'] = Context::$socket_id;
  104. }
  105. // 不是发给当前用户则使用存储中的地址
  106. else
  107. {
  108. $address = self::getAddressByUid($uid);
  109. if(!$address)
  110. {
  111. return false;
  112. }
  113. $pack->header['local_ip'] = $address['local_ip'];
  114. $pack->header['local_port'] = $address['local_port'];
  115. $pack->header['socket_id'] = $address['socket_id'];
  116. }
  117. $pack->header['client_ip'] = Context::$client_ip;
  118. $pack->header['client_port'] = Context::$client_port;
  119. $pack->header['uid'] = empty($uid) ? 0 : $uid;
  120. $pack->body = (string)$message;
  121. if(empty(Context::$protocol))
  122. {
  123. Context::$protocol = 'tcp';
  124. }
  125. return self::sendToGateway(Context::$protocol."://{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer());
  126. }
  127. /**
  128. * 踢掉某个网关的socket
  129. * @param string $local_ip
  130. * @param int $local_port
  131. * @param int $socket_id
  132. * @param string $message
  133. * @param int $uid
  134. */
  135. public static function kickAddress($local_ip, $local_port, $socket_id, $message, $uid = null)
  136. {
  137. $pack = new GatewayProtocol();
  138. $pack->header['cmd'] = GatewayProtocol::CMD_KICK;
  139. $pack->header['series_id'] = Context::$series_id > 0 ? Context::$series_id : 0;
  140. $pack->header['local_ip'] = $local_ip;
  141. $pack->header['local_port'] = $local_port;
  142. $pack->header['socket_id'] = $socket_id;
  143. if(null !== Context::$client_ip)
  144. {
  145. $pack->header['client_ip'] = Context::$client_ip;
  146. $pack->header['client_port'] = Context::$client_port;
  147. }
  148. $pack->header['uid'] = $uid ? $uid : 0;
  149. $pack->body = (string)$message;
  150. if(empty(Context::$protocol))
  151. {
  152. Context::$protocol = 'tcp';
  153. }
  154. return self::sendToGateway(Context::$protocol."://{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer());
  155. }
  156. /**
  157. * 存储uid的网关地址
  158. * @param int $uid
  159. */
  160. public static function storeUid($uid)
  161. {
  162. $address = array('local_ip'=>Context::$local_ip, 'local_port'=>Context::$local_port, 'socket_id'=>Context::$socket_id);
  163. Store::set($uid, $address);
  164. }
  165. /**
  166. * 获取用户的网关地址
  167. * @param int $uid
  168. */
  169. public static function getAddressByUid($uid)
  170. {
  171. return Store::get($uid);
  172. }
  173. /**
  174. * 删除用户的网关地址
  175. * @param int $uid
  176. */
  177. public static function deleteUidAddress($uid)
  178. {
  179. return Store::delete($uid);
  180. }
  181. /**
  182. * 通知网关uid链接成功(通过验证)
  183. * @param int $uid
  184. */
  185. public static function notifyConnectionSuccess($uid)
  186. {
  187. return self::sendCmdAndMessageToUid($uid, GatewayProtocol::CMD_CONNECT_SUCCESS, '');
  188. }
  189. /**
  190. * 发送数据到网关
  191. * @param string $address
  192. * @param string $buffer
  193. */
  194. public static function sendToGateway($address, $buffer)
  195. {
  196. $client = stream_socket_client($address);
  197. $len = stream_socket_sendto($client, $buffer);
  198. return $len == strlen($buffer);
  199. }
  200. }