Пример #1
0
 /**
  * 处理请求
  * @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();
 }
Пример #2
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);
 }