hxp
2025-06-04 f4a514d5ac952110da846636ecbb9de951eaf3d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
 
#创建日期:2012-5-15 下午4:26:32
#作者:zhouliang
include_once '/Common/Logging.php';
include_once '/ProjComm/CommonOperate.php';
include_once '/Common/CommFunc.php';
include_once '/Common/Socket.php';
include_once '/Common/BufferOperate.php';
include_once 'AuthorityInfo.php';
include_once '/Common/MongoCommon.php';
 
#打包GM命令数据
function PackGMCommand( $strGuid, $type, $toServerID, $CmdLen, $Cmd, $UserDataLen, $UserData )
{
    // 兼容MMO服务器及卡牌指定目标ServerID的服务器,方便内网游戏服务器部署开发环境测试
    if ($toServerID > 0) {
        return pack( 'C1', $type ).pack( 'V1', $toServerID ).pack( 'C1', strlen($strGuid) ).$strGuid.pack( 'V1', $CmdLen ).$Cmd.pack( 'V1', $UserDataLen ).$UserData;
    }
    return pack( 'C1', $type ).pack( 'C1', strlen($strGuid) ).$strGuid.pack( 'V1', $CmdLen ).$Cmd.pack( 'V1', $UserDataLen ).$UserData;
}
 
#解包GM命令数据
function UnPackGMCommand( $buffer, &$type, &$ResultLen, &$Result, &$UserDataLen, &$UserData, &$strGUID )
{
    \Logging\LogDebug( "接收的buffer内容为:".$buffer );
    $nPos=0;
    
    $type=\BufferOperate\ReadBYTE( $buffer, $nPos );
    \Logging\LogDebug( 'pos='.$nPos.' type='.$type );
    
    $nGUIDLen=\BufferOperate\ReadBYTE( $buffer, $nPos );
    \Logging\LogDebug( 'pos='.$nPos.' $nGUIDLen='.$nGUIDLen );
    
    $strGUID=\BufferOperate\ReadString( $buffer , $nGUIDLen, $nPos );
    \Logging\LogDebug( 'pos='.$nPos.' $strGUID='.$strGUID );
    
    $ResultLen=\BufferOperate\ReadDWORD( $buffer, $nPos );
    \Logging\LogDebug( 'pos='.$nPos.' $ResultLen='.$ResultLen );
    
    $Result=\BufferOperate\ReadString( $buffer , $ResultLen, $nPos );
    \Logging\LogDebug( 'pos='.$nPos.' $Result='.$Result );
    
    $UserDataLen=\BufferOperate\ReadDWORD( $buffer, $nPos );
    \Logging\LogDebug( 'pos='.$nPos.' $UserDataLen='.$UserDataLen );
    
    $UserData=\BufferOperate\ReadString( $buffer , $UserDataLen, $nPos );
    \Logging\LogDebug( 'pos='.$nPos.' $UserData='.$UserData );
    
}
 
#从配置载入验证数据
function LoadAuthorityData()
{
    if( !\CommFunc\GetKeyFromConfig( 'config.ini', 'Popedom', 'Popedom_count', $strPopedomCount, 'Read Popedom_count error!' ) )
    {
        exit();
    }
    
    $nCount=intval( $strPopedomCount );
    
    $info=new AuthorityInfo();
    
    for ( $nIndex=1; $nIndex<=$nCount; $nIndex++)
    {
        $strSection='Popedom_'.$nIndex;
    
        \CommFunc\GetKeyFromConfig( 'config.ini', $strSection, 'key', $strKey, "Read $strSection key error!" );
        \CommFunc\GetKeyFromConfig( 'config.ini', $strSection, 'pack_types', $strPackTypes, "Read $strSection pack_types error!" );
        $info->AddInfo( $strKey, $strPackTypes );
    }
    
    return $info;
}
 
function SendToDBServer($strOrderID)
{
    $strDataServerIP='';
    if( !\CommFunc\GetKeyFromConfig( 'config.ini', 'Server', 'DataServerIP', $strDataServerIP,
        '{"ErrorNo":"5","ErrorStr":"Read dataserver ip error"}' ) )
    {
        exit();
    }
 
    $strDataServerPort='';
    if( !\CommFunc\GetKeyFromConfig( 'config.ini', 'Server', 'DataServerPort', $strDataServerPort,
        '{"ErrorNo":"6","ErrorStr":"Read dataserver port error"}' ) )
    {
        exit();
    }
 
    //与DataServer进行交互,获得DataServer的返回值
 
    $sock=new \Socket\Socket();
 
    if( !$sock->Init() )
    {
        echo '{"ErrorNo":"7","ErrorStr":"Socket init error"}';
        exit();
    }
 
    if( !$sock->Connect( $strDataServerIP, $strDataServerPort ) )
    {
        echo '{"ErrorNo":"8","ErrorStr":"Connect data server error"}';
        exit();
    }
 
    $bufferSend='';
 
    $toServerID = intval($_POST['toServerID']);
    \Logging\LogInfo( 'toServerID:'. $toServerID);
    $bufferGmCmd=PackGMCommand( $strOrderID, 113, $toServerID, strlen($_POST['pack']), $_POST['pack'], 0, '' );
 
    \Logging\LogDebug( \CommFunc\BinToStr($bufferGmCmd) );
    #三个类型数字是由服务端给出的,需要变动也会由服务端给出改法,
    #意义大概是包类型是内部通讯封包,源是GM工具,目标是DATASERVER
    $bufferSend=\CommonOperate\PackData( $bufferGmCmd, 2, 1, 11 );
 
 
    if( !$sock->Write( $bufferSend ) )
    {
        echo '{"ErrorNo":"9","ErrorStr":"Send data to data server error"}';
        exit();
    }
 
    \Logging\LogInfo( '开始接收数据' );
 
    $nSingleRecvMaxLen=1000;
 
    $strRecv='';
 
    #开始循环接收
 
    $type=0;
    $ResultLen=0;
    $Result='';
    $UserDataLen=0;
    $UserData='';
    while(TRUE)
    {
        //接收数据,进行判断成功与否,然后进行解析
        \Logging\LogDebug( "socket开始接收数据" );
        if( !$sock->Read( $strRead ) )
        {
            echo '{"ErrorNo":"10","ErrorStr":"Recv data error"}';
            exit();
        }
        \Logging\LogDebug( "socket接收数据完毕" );
        $strRecv=$strRecv.$strRead;
         
        $customBodyBuffer='';
        if( \CommonOperate\UnPackData($strRecv, $customBodyBuffer) )
        {
            #判断是不是GM工具的包类型114
            if( \CommonOperate\GetPackType($customBodyBuffer)==114 )
            {
                $strRecvOrderID='';
                UnPackGMCommand( $customBodyBuffer, $type, $ResultLen, $Result, $UserDataLen, $UserData, $strRecvOrderID );
                if( $strRecvOrderID==$strOrderID )
                {
                    //解析成功,则不再进行接收,并且接收的数据是给自己的
                    break;
                }
                else
                {
                    \Logging\LogError( "接收的数据不是自己的!原strOderID是:$strOrderID,新的是:$strRecvOrderID" );
                }
            }
 
        }
    }
     
 
    \Logging\LogInfo( '数据获得并解析完毕' );
 
    //返回结果给请求者
    echo  $Result.$UserData;
}
 
 
\Logging\CreateLogging( 'GMTool' );
 
$strOrderID=\CommFunc\guid();
\Logging\LogAddObj(\Logging\strPhpDir, 'GMTool', "GUID:$strOrderID");
 
\Logging\LogInfo( '开始处理...' );
 
 
if( !\CommFunc\CheckPostParam("pack", '{"ErrorNo":"1","ErrorStr":"Param pack error"}') )
{
    exit();
}
 
if( !\CommFunc\CheckPostParam("sign", '{"ErrorNo":"2","ErrorStr":"Param sign error"}') )
{
    exit();
}
 
//验证信息是否是可允许的
$authorityInfo=LoadAuthorityData();
 
 
$arrayPack = json_decode( $_POST['pack'] , true );
 
\Logging\LogInfo( "json_decode的结果是".var_export( $arrayPack, True ) );
 
$arrayPack['pack_type']=iconv( 'utf-8' , 'gbk', $arrayPack['pack_type'] );
 
 
 
if( !array_key_exists( 'pack_type', $arrayPack ) )
{
    echo '{"ErrorNo":"3","ErrorStr":"Can\'t find pack_type in jsondata"}';
    $strPackRecv=$_POST['pack'];
    \Logging\LogError( "收到的信息错误,收到的数据是:$strPackRecv" );
    exit();
}
 
$strKey=$authorityInfo->GetKeyFromValue( $arrayPack['pack_type'] );
 
if( md5( $_POST['pack'].$strKey )!=$_POST['sign'] )
{
    \CommFunc\LogErrorAndPrint( '{"ErrorNo":"4","ErrorStr":"Sign error, end"}' );
    exit();
}
 
$DBArray = array("GMTServer_Account", "GMTServer_Name");
if (in_array($arrayPack['pack_type'], $DBArray))
{
    //可以直接处理
    #开始连接数据库
    $dbOpt=new \MongoCommon\MongoDBOpt( '..\\InterfaceConfig.php', "ServerName" );
    
    if( !$dbOpt->Init() )
    {
        #echo '数据库初始化失败!原因:'.$dbOpt->GetLastError().'请检查配置';
        $resultjson['status'] = -2;
        echo json_encode($resultjson);
        exit;
    }
    
    \Logging\LogInfo( 'gmtool连接数据库成功.' );
    
    $skip=0;
    $perPage=20;
    #查找账号是否有账户可以使用
    if($arrayPack['pack_type'] == "GMTServer_Account")
    {
        
        if($arrayPack["accID"] == "")
        {
            $findArray = array();
        }
        else
        {
            $findArray=array('AccID'=>new MongoRegex($arrayPack["accID"].".*/i"));
        }
        $dbName = "tagDBPlayer";
    }
    else if ($arrayPack['pack_type'] == "GMTServer_Name")
    {
        if($arrayPack["name"] == "")
        {
            $findArray = array();
        }
        else 
        {
            $findArray=array('PlayerName'=>new MongoRegex("/.*".$arrayPack["name"].".*/i"));
        }
        $dbName = "tagDBPlayer";
    }
    
    if(isset($arrayPack["skip"]))
    {
        $skip=$arrayPack["skip"];
    }
    
    if(isset($arrayPack["perPage"]))
    {
        $perPage=$arrayPack["perPage"];
    }
    
    //新增排序
    $sortinfo = array();
    if(isset($arrayPack["sort"]))
    {
        $sortinfo = json_decode($arrayPack["sort"], true);
    }
    
    //查询玩家表
    $retArray;
    $retColumn=json_decode($arrayPack["fields"]);
    
    $result = $dbOpt->GetData($dbName, $findArray, $retArray, $retColumn, $skip, $perPage, $sortinfo);
    
    //{"ResultType": 0, "ResultMsg": {"forbidTalk": false, "forbidLogin": false}, "pack_type": "GMT_GetPlayerForbid"}
    if($result)
    {
        echo json_encode(array("ResultType"=>0, "ResultMsg"=>$retArray, "pack_type"=>$arrayPack['pack_type']));
        //\Logging\LogInfo( '数据库直接查询结果'.print_r($retArray, true) );
    }
    else
    {
        echo json_encode(array("ResultType"=>102));
    }
}
else
{
    //发送给游戏服务器DB
    SendToDBServer($strOrderID);
}
 
 
\Logging\LogInfo( '结束处理..' );