Gateway.php 6.1 KB

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