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