/** * 有消息时触发该方法 * @param int $client_id 发消息的client_id * @param string $message 消息 * @return void */ public static function onMessage($client_id, $message) { $message = substr($message, 4); //解析传输的数据 $cmd = new PbCmd(); try { $cmd->parseFromString($message); } catch (Exception $ex) { return $ex->getMessage(); } //接收参数 $obj = $cmd->getObj(); $method = $cmd->getMethod(); $params = $cmd->getParams(); //统计数据 $class = substr($obj, 8); StatisticClient::tick($class, $method); try { //处理业务逻辑 $instance = new $obj(); $buffer = $instance->{$method}($params); StatisticClient::report($class, $method, 1, 0, 'successful'); } catch (Exception $ex) { $pbResult = new PbResult(); $pbResult->setCode($ex->getCode()); $pbResult->setMsg($ex->getMessage()); $buffer = $pbResult->SerializeToString(); $total_length = 4 + strlen($buffer); $buffer = pack('N', $total_length) . $buffer; StatisticClient::report($class, $method, 0, $ex->getCode(), $ex->getMessage()); } echo 'Login_Woker' . PHP_EOL; return Gateway::sendToCurrentClient($buffer); }
/** * 处理请求 * @see Man\Core.SocketWorker::dealProcess() */ public function dealProcess($recv_buffer) { $pack = new GatewayProtocol($recv_buffer); Context::$client_ip = $pack->header['client_ip']; Context::$client_port = $pack->header['client_port']; Context::$local_ip = $pack->header['local_ip']; Context::$local_port = $pack->header['local_port']; Context::$socket_id = $pack->header['socket_id']; Context::$client_id = $pack->header['client_id']; $_SERVER = array('REMOTE_ADDR' => Context::$client_ip, 'REMOTE_PORT' => Context::$client_port, 'GATEWAY_ADDR' => Context::$local_ip, 'GATEWAY_PORT' => Context::$local_port, 'GATEWAY_CLIENT_ID' => Context::$client_id); if ($pack->ext_data != '') { $_SESSION = Context::sessionDecode($pack->ext_data); } else { $_SESSION = null; } // 备份一次$pack->ext_data,请求处理完毕后判断session是否和备份相等,不相等就更新session $session_str_copy = $pack->ext_data; $cmd = $pack->header['cmd']; $interface = isset(self::$interfaceMap[$cmd]) ? self::$interfaceMap[$cmd] : $cmd; StatisticClient::tick(__CLASS__, $interface); try { switch ($cmd) { case GatewayProtocol::CMD_ON_GATEWAY_CONNECTION: Login::onGatewayConnect(Context::$client_id); break; case GatewayProtocol::CMD_ON_MESSAGE: Login::onMessage(Context::$client_id, $pack->body); break; case GatewayProtocol::CMD_ON_CLOSE: Login::onClose(Context::$client_id); break; } StatisticClient::report(__CLASS__, $interface, 1, 0, ''); } catch (\Exception $e) { $msg = 'client_id:' . Context::$client_id . "\tclient_ip:" . Context::$client_ip . "\n" . $e->__toString(); StatisticClient::report(__CLASS__, $interface, 0, $e->getCode() > 0 ? $e->getCode() : 201, $msg); } $session_str_now = $_SESSION !== null ? Context::sessionEncode($_SESSION) : ''; if ($session_str_copy != $session_str_now) { Gateway::updateSocketSession(Context::$socket_id, $session_str_now); } Context::clear(); }
/** * 删除客户端内部通讯地址 * @param int $global_client_id * @param string $address */ protected function deleteClientAddress($global_client_id) { StatisticClient::tick(__CLASS__, 'deleteClientAddress'); if (!Store::instance('gateway')->delete($global_client_id)) { $msg = "删除客户端通讯地址出错"; if (get_class(Store::instance('gateway')) == 'Memcached') { $msg .= " 原因:" . Store::instance('gateway')->getResultMessage(); } $this->notice($msg); return StatisticClient::report(__CLASS__, 'deleteClientAddress', 0, 106, new \Exception($msg)); } StatisticClient::report(__CLASS__, 'deleteClientAddress', 1, 0, ''); }
/** * 用户客户端主动关闭连接触发 * @see Man\Core.SocketWorker::closeClient() */ protected function closeClient($fd) { StatisticClient::tick(); if ($uid = $this->getUidByFd($fd)) { $this->sendToWorker(GatewayProtocol::CMD_ON_CLOSE, $fd); unset($this->uidConnMap[$uid]); } unset($this->connUidMap[$fd], $this->socketSessionMap[$fd]); parent::closeClient($fd); StatisticClient::report(__CLASS__, 'CMD_ON_CLOSE', 1, 0, ''); }
/** * 处理请求 * @see Man\Core.SocketWorker::dealProcess() */ public function dealProcess($recv_str) { $pack = new GatewayProtocol($recv_str); Context::$client_ip = $pack->header['client_ip']; Context::$client_port = $pack->header['client_port']; Context::$local_ip = $pack->header['local_ip']; Context::$local_port = $pack->header['local_port']; Context::$socket_id = $pack->header['socket_id']; Context::$uid = $pack->header['uid']; $_SERVER = array('REMOTE_ADDR' => Context::$client_ip, 'REMOTE_PORT' => Context::$client_port, 'GATEWAY_ADDR' => Context::$local_ip, 'GATEWAY_PORT' => Context::$local_port, 'GATEWAY_SOCKET_ID' => Context::$socket_id); if ($pack->ext_data != '') { $_SESSION = Context::sessionDecode($pack->ext_data); } else { $_SESSION = null; } // 备份一次$pack->ext_data,请求处理完毕后判断session是否和备份相等 $session_str_copy = $pack->ext_data; $cmd = $pack->header['cmd']; StatisticClient::tick(); $module = __CLASS__; $interface = isset(self::$interfaceMap[$cmd]) ? self::$interfaceMap[$cmd] : 'null'; $success = 1; $code = 0; $msg = ''; try { switch ($cmd) { case GatewayProtocol::CMD_ON_GATEWAY_CONNECTION: call_user_func_array(array('Event', 'onGatewayConnect'), array()); break; case GatewayProtocol::CMD_ON_CONNECTION: call_user_func_array(array('Event', 'onConnect'), array($pack->body)); break; case GatewayProtocol::CMD_ON_MESSAGE: call_user_func_array(array('Event', 'onMessage'), array(Context::$uid, $pack->body)); break; case GatewayProtocol::CMD_ON_CLOSE: call_user_func_array(array('Event', 'onClose'), array(Context::$uid)); break; } } catch (\Exception $e) { $success = 0; $code = $e->getCode() > 0 ? $e->getCode() : 500; $msg = 'uid:' . Context::$uid . "\tclient_ip:" . Context::$client_ip . "\n" . $e->__toString(); } $session_str_now = $_SESSION !== null ? Context::sessionEncode($_SESSION) : ''; if ($session_str_copy != $session_str_now) { Gateway::updateSocketSession(Context::$socket_id, $session_str_now); } Context::clear(); StatisticClient::report($module, $interface, $success, $code, $msg); }