/** * 踢掉某个网关的socket * @param string $local_ip * @param int $local_port * @param int $socket_id * @param string $message * @param int $client_id */ protected static function kickAddress($local_ip, $local_port, $socket_id) { $pack = new GatewayProtocol(); $pack->header['cmd'] = GatewayProtocol::CMD_KICK; $pack->header['local_ip'] = $local_ip; $pack->header['local_port'] = $local_port; $pack->header['socket_id'] = $socket_id; if (null !== Context::$client_ip) { $pack->header['client_ip'] = Context::$client_ip; $pack->header['client_port'] = Context::$client_port; } $pack->header['client_id'] = 0; $pack->body = ''; return self::sendToGateway("{$pack->header['local_ip']}:{$pack->header['local_port']}", $pack->getBuffer()); }
/** * 检查gateway转发来的用户请求是否完整 * @see Man\Core.SocketWorker::dealInput() */ public function dealInput($recv_buffer) { return GatewayProtocol::input($recv_buffer); }
/** * 随机抽取一个与BusinessWorker的长连接,将数据发给一个BusinessWorker * @param int $cmd * @param int $socket_id * @param string $body */ protected function sendToWorker($cmd, $socket_id, $body = '') { $pack = new GatewayProtocol(); $pack->header['cmd'] = $cmd; $pack->header['local_ip'] = $this->lanIp; $pack->header['local_port'] = $this->lanPort; $pack->header['socket_id'] = $socket_id; $pack->header['client_ip'] = $this->connRemoteAddressMap[$socket_id]['ip']; $pack->header['client_port'] = $this->connRemoteAddressMap[$socket_id]['port']; $pack->header['client_id'] = $this->getClientIdByFd($socket_id); $pack->body = $body; $pack->ext_data = $this->connSessionMap[$pack->header['socket_id']]; return $this->sendBufferToWorker($pack->getBuffer()); }
/** * 发送数据到网关 * @param string $address * @param string $buffer */ protected static function sendToGateway($address, $gateway_data) { // 有$businessWorker说明是workerman环境,使用$businessWorker发送数据 if (self::$businessWorker) { if (!isset(self::$businessWorker->gatewayConnections[$address])) { return false; } return self::$businessWorker->gatewayConnections[$address]->send($gateway_data); } // 非workerman环境,使用udp发送数据 $gateway_buffer = GatewayProtocol::encode($gateway_data); $client = stream_socket_client("udp://{$address}", $errno, $errmsg); return strlen($gateway_buffer) == stream_socket_sendto($client, $gateway_buffer); }
/** * 随机抽取一个与BusinessWorker的长连接,将数据发给一个BusinessWorker * @param int $cmd * @param int $socket_id * @param string $body */ protected function sendToWorker($cmd, $socket_id, $body = '') { $address = $this->getRemoteAddress($socket_id); if ($address) { list($client_ip, $client_port) = explode(':', $address, 2); } else { $client_ip = 0; $client_port = 0; } $pack = new GatewayProtocol(); $pack->header['cmd'] = $cmd; $pack->header['local_ip'] = $this->lanIp; $pack->header['local_port'] = $this->lanPort; $pack->header['socket_id'] = $socket_id; $pack->header['client_ip'] = $client_ip; $pack->header['client_port'] = $client_ip; $pack->header['uid'] = $this->getUidByFd($socket_id); $pack->body = $body; $pack->ext_data = $this->socketSessionMap[$pack->header['socket_id']]; return $this->sendBufferToWorker($pack->getBuffer()); }
/** * 更新session * @param int $uid * @param string $session_str */ public static function updateSocketSession($socket_id, $session_str) { $pack = new GatewayProtocol(); $pack->header['cmd'] = GatewayProtocol::CMD_UPDATE_SESSION; $pack->header['socket_id'] = Context::$socket_id; $pack->ext_data = (string) $session_str; return self::sendToGateway(Context::$local_ip . ':' . Context::$local_port, $pack->getBuffer()); }