Gateway.php 6.0 KB

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