Пример #1
0
 public static function connect($client_ip)
 {
     if (empty($client_ip)) {
         $client_ip = GATEWAY_IP;
     }
     if (empty(self::$socket[$client_ip])) {
         if (!(self::$socket[$client_ip] = socket_create(AF_INET, SOCK_STREAM, 0))) {
             return self::error("the socket {$client_ip} can\\'t create");
         }
         $timeout = array('sec' => 1, 'usec' => 500000);
         socket_set_option(self::$socket[$client_ip], SOL_SOCKET, SO_RCVTIMEO, $timeout);
         //            !socket_connect(self::$socket, GATEWAY_IP, GATEWAY_PORT)
         if (!socket_connect(self::$socket[$client_ip], $client_ip, GATEWAY_PORT)) {
             self::$socket[$client_ip] = null;
             return self::error("the socket {$client_ip} can\\'t connect server");
         }
         echo "\n==========[{$client_ip} connect suc]===========\n";
         $rdata = array(0 => SERVER_ID, 1 => SERVER_TITLE);
         //注册信息
         Protocol::make_data($rdata);
         $protocol = Protocol::output(7, 4, 418, 2, SERVER_ID);
         $length = strlen($protocol);
         $writeLen = socket_write(self::$socket[$client_ip], $protocol, $length);
         if ($writeLen === false) {
             echo "\n################[{$client_ip} register]################\n";
             echo socket_last_error(self::$socket[$client_ip]);
             echo "\n################[ {$client_ip} register]################\n";
             self::$socket[$client_ip] = null;
             return self::error("the socket {$client_ip} register error!");
         }
         echo "\n register writelen: {$writeLen}\n";
         $head = socket_read(self::$socket[$client_ip], 8);
         if ($head === false) {
             self::$socket[$client_ip] = null;
             return self::error("the socket {$client_ip} read head error!");
         }
         echo bin2hex($head), "\n";
         self::read_msg($client_ip);
     }
     return self::$socket[$client_ip];
 }
Пример #2
0
 /**
  * 
  * 返回压包后的错误提示
  * @param string $gPhraseId 错误内容
  * @param string $nPhraseId 错误标题
  */
 public static function output_error($globalError, $normalError, $errorCode = 0, $isError = true, $type = 0)
 {
     $protocol = '';
     //		$data = array(0=>$normalError, 1=>$globalError, 2=>$errorCode, 3=>$type);
     $data[0] = array('type' => 1, 'code' => 0, 'content' => $normalError, 'param' => array());
     Protocol::make_data($data);
     $protocol .= Protocol::output(3, 7, 742);
     return $protocol;
 }
Пример #3
0
 public function CMD2_529()
 {
     $data = $this->get_game('Fairy')->del_fairy($this->_authuser['player_id'], $this->_param[0]);
     $this->check_error($data);
     Protocol::make_data($data);
     Protocol::output(3, 5, 529);
 }
Пример #4
0
 public static function auth()
 {
     //	if(isset($_REQUEST['sid']) && is_numeric($_REQUEST['sid']) && SERVER_ID == 1 && !isset($_SESSION['player_id'])){
     if (isset($_REQUEST['loginid']) && isset($_REQUEST['sid'])) {
         $objPlayerData = TenYear::getInstance('Data_Player');
         $arrUser = $objPlayerData->get_player_info($_REQUEST['loginid']);
         if (empty($arrUser)) {
             $objSession = TenYear::getInstance('session');
             $objSession->set_is_write(false);
             $arrLanguage = Language_Error::get_language_error_assoc(array('10221', 'LOGIN_MSG_21'));
             $content = $arrLanguage['LOGIN_MSG_21'];
             $title = $arrLanguage['10221'];
             $url = MAIN_DOMAIN_URL;
             $data = array();
             $data['content'] = $content;
             $data['title'] = $title;
             $data['code'] = 'LOGIN_MSG_21';
             $data['url'] = $url;
             Protocol::make_data($data);
             Protocol::output(4, 2);
         } else {
             $_SESSION['sid'] = session_id();
             $_SESSION["account"] = $arrUser['user_account'];
             $_SESSION["player_id"] = $arrUser["player_id"];
             $_SESSION["player_name"] = $arrUser["name"];
             $_SESSION["player_sex"] = $arrUser["sex"];
             $_SESSION["player_level"] = $arrUser["level"];
             $_SESSION["vip_level"] = $arrUser["vip_level"];
             $_SESSION["avatar"] = $arrUser["avatar"];
             $_SESSION["career_type"] = $arrUser["career_type"];
             $_SESSION["city_id"] = $arrUser["city_id"];
             $_SESSION["active_time"] = time();
             $_SESSION["logintime"] = time();
             $_SESSION["adult_flag"] = 0;
             $_SESSION["adult_game_time"] = 0;
             $_SESSION['expire'] = $time;
             $_SESSION["gm"] = $arrUser["gm"];
             $objOnline = TenYear::getInstance('Game_Online');
             $objOnline->write_session();
             //	$objOnline->check_online_data($arrUser["player_id"], true);
         }
     }
     //当前session已经被污染成新playerid
     if (isset($_REQUEST['loginid']) && $_REQUEST['loginid'] != $_SESSION['player_id']) {
         $_SESSION['player_id'] = 0;
     }
     if (!isset($_SESSION['player_id']) || !$_SESSION['player_id']) {
         //	$objSession = TenYear::getInstance('session');
         //	$objSession->set_is_write(false);
         $arrLanguage = Language_Error::get_language_error_assoc(array('10221', 'LOGIN_MSG_21'));
         $content = $arrLanguage['LOGIN_MSG_21'];
         $title = $arrLanguage['10221'];
         $url = MAIN_DOMAIN_URL;
         $data = array();
         $data['title'] = $title;
         $data['content'] = $content;
         $data['code'] = 'LOGIN_MSG_21';
         $data['url'] = $url;
         Protocol::make_data($data);
         Protocol::output(4, 2);
     }
     global $arrAuthUser;
     $arrAuthUser = $_SESSION;
     /*-----------------------------[ 用户登录数据 ]-----------------------------[ begin ]---*
     		$arrAuthUser["account"] 		= $_SESSION["account"];
     		$arrAuthUser["player_id"]		= $_SESSION["player_id"];		//玩家ID
     		$arrAuthUser["player_name"]		= $_SESSION["player_name"];		//玩家昵称
     		$arrAuthUser["player_sex"]		= $_SESSION["player_sex"];		//玩家性别
     		$arrAuthUser["player_level"]	= $_SESSION["player_level"];	//玩家等级
     		$arrAuthUser["vip_level"]		= $_SESSION["vip_level"];		//vip等级
     		$arrAuthUser["avatar"]			= $_SESSION["avatar"];			//玩家avatar
     		$arrAuthUser["career_type"]		= $_SESSION["career_type"];			//玩家avatar编号
     		$arrAuthUser["city_id"]			= $_SESSION["city_id"];			//场景编号
     		$arrAuthUser["active_time"]		= $_SESSION["active_time"];		//活动时间
     		$arrAuthUser["logintime"]		= $_SESSION["logintime"];		//登录时间
     		$arrAuthUser["adult_flag"]		= $_SESSION["adult_flag"];		//防沉迷标志
     		$arrAuthUser["adult_game_time"]	= $_SESSION["adult_game_time"];	//防沉迷在线时间
     
     		/*
     		$arrAuthUser["adult_remind"]	= isset($_SESSION["adult_remind"]) ? $_SESSION["adult_remind"] : 0;//1:1小时,2:2小时,4:3小时
     		$arrAuthUser["online_reward_id"]	= $_SESSION["online_reward_id"];	//在线开始时间
     		$arrAuthUser["online_start_time"]	= $_SESSION["online_start_time"];	//在线开始时间
     		$arrAuthUser["online_reward_time"]	= $_SESSION["online_reward_time"];	//在线奖励时间
     		*/
     /*-----------------------------[ 用户登录数据 ]-----------------------------[ end ]---*/
 }
Пример #5
0
 public function flush_cache()
 {
     Com_Cache::factory('setting')->flush();
     //		Com_Cache::factory("game")->flush();
     //		Com_Queued::send("process", "restart");
     $data[0] = intval(1);
     Protocol::make_data($data);
     $raw_data = Protocol::output(7, 4, 402, 2);
     $server_ip = Com_System::get_client_ip();
     $url = "http://{$server_ip}/nginx2cppsvr.do";
     //        $url = SERVER_DOMAIN.'/nginx2cppsvr.do';
     $header = "Content-type: application/octet-stream";
     $ch = curl_init();
     //初始化curl
     curl_setopt($ch, CURLOPT_URL, $url);
     //设置链接
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     //设置是否返回信息
     curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
     //设置HTTP头
     curl_setopt($ch, CURLOPT_POST, 1);
     //设置为POST方式
     curl_setopt($ch, CURLOPT_POSTFIELDS, $raw_data);
     //POST数据
     curl_setopt($ch, CURLOPT_TIMEOUT, 1);
     curl_exec($ch);
     curl_close($ch);
 }
Пример #6
0
 /**
  * 后端进程推送消息
  * @param $param
  * @return bool
  */
 public function receive($param)
 {
     if (empty($param)) {
         return false;
     }
     $param = json_decode($param, true);
     if (empty($param['data'])) {
         return false;
     }
     $client_ip = $param['client_ip'];
     if (isset($param['cmd0']) && isset($param['cmd1']) && isset($param['cmd2'])) {
         echo "\n===============[AsyncProtocol:cmd0={$cmd0}|cmd1={$cmd1}|cmd2={$cmd2}|player_id={$player_id}]==============\n";
         $cmd0 = $param['cmd0'];
         $cmd1 = $param['cmd1'];
         $cmd2 = $param['cmd2'];
         $player_id = $param['player_id'];
         Protocol::make_data($param['data']);
         $protocol = Protocol::output($cmd0, $cmd1, $cmd2, 2, $player_id);
     } else {
         //直接推送协议 不用封装
         $protocol = base64_decode($param['data']);
     }
     echo "\n===============[receive]==============\n";
     var_dump($client_ip);
     if (DEBUG != 0) {
         $protocol_copy = $protocol;
         # 协议数据副本
         $used_bytes = 0;
         # 已读取字节数
         while ($remain_data = substr($protocol_copy, $used_bytes, 12)) {
             $used_bytes += 12;
             $head = unpack("Vlen/Vcmd/Vplayer_id", $remain_data);
             $cmd = $head['cmd'];
             $cmd0 = ($cmd & 0xff000000) >> 24;
             $cmd1 = ($cmd & 0xff0000) >> 16;
             $cmd2 = $cmd & 0xffff;
             $player_id = $head['player_id'];
             $rlen = $head['len'] - 12;
             $body_bin = substr($protocol_copy, $used_bytes, $rlen);
             # 实际协议体,msgpack_pack打包的二进制流
             $used_bytes += $rlen;
             if (!empty($body_bin)) {
                 $body_real = msgpack_unpack($body_bin);
                 $json = json_encode($body_real);
                 $date = date('Y-m-d H:i:s');
                 echo "\n===============[AsyncProtocol:date={$date}|cmd0={$cmd0}|cmd1={$cmd1}|cmd2={$cmd2}|player_id={$player_id}]==============\n";
                 echo "{$json}\n";
             } else {
             }
         }
     }
     echo "\n===============[receive]==============\n";
     //        $clients = array('192.168.1.228','192.168.1.136','192.168.1.165','192.168.1.155','192.168.1.25','192.168.1.24','192.168.1.224','192.168.1.33','192.168.1.121');
     #$clients = array(GATEWAY_IP,'192.168.1.24','192.168.1.33','192.168.1.136','192.168.1.92','192.168.1.224','192.168.1.41');
     $res = Com_GatewayClient::send($protocol, GATEWAY_IP);
     //        $clients = array(GATEWAY_IP);
     //        foreach($clients as $client_ip){
     //
     //
     //        }
     //        foreach($clients as $client_ip){
     #Com_GatewayClient::send($protocol,$client_ip);
     //Com_GatewayClient::send($protocol,GATEWAY_IP);
     //        if($client_ip == GATEWAY_IP){
     //            Com_GatewayClient::send($protocol,$client_ip);
     //        }
     //        if($client_ip != '192.168.1.41'){
     //            Com_GatewayClient::send($protocol,'192.168.1.41');
     //        }
     //
     //        if($client_ip != '192.168.1.228'){
     //            Com_GatewayClient::send($protocol,'192.168.1.228');
     //        }
     //            Com_GatewayClient::send($protocol,'192.168.1.165');
     //            Com_GatewayClient::send($protocol,'192.168.1.155');
     //        }
     //        Com_GatewayClient::send($protocol,$client_ip);
     //        Com_Curl::send($protocol,$client_ip);
 }