Skip to content

Commit 12ab226

Browse files
author
fan3750060
committed
优化结构及角色加载地图
1 parent fde2f91 commit 12ab226

File tree

17 files changed

+439
-323
lines changed

17 files changed

+439
-323
lines changed

app/Auth/AuthServer.php

Lines changed: 23 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
use app\Auth\Connection;
55
use app\Auth\Message;
66
use app\Common\Checksystem;
7-
use core\Work;
7+
use app\Socket\SwooleTcp;
88
use core\lib\Cache;
9+
use core\Work;
910

1011
/**
1112
* auth server
@@ -17,15 +18,8 @@ class AuthServer
1718
public $active;
1819
public $ServerConfig;
1920

20-
/**
21-
* [start 开始]
22-
* ------------------------------------------------------------------------------
23-
* @author by.fan <[email protected]>
24-
* ------------------------------------------------------------------------------
25-
* @version date:2019-04-19
26-
* ------------------------------------------------------------------------------
27-
* @return [type] [description]
28-
*/
21+
public $serv;
22+
2923
public function start()
3024
{
3125
Checksystem::check();
@@ -60,117 +54,45 @@ public function start()
6054
Work::run($param);
6155
}
6256

63-
//开启socket
6457
$this->runAuthServer();
6558
}
6659

67-
/**
68-
* [runAuthServer 运行服务器]
69-
* ------------------------------------------------------------------------------
70-
* @author by.fan <[email protected]>
71-
* ------------------------------------------------------------------------------
72-
* @version date:2019-04-19
73-
* ------------------------------------------------------------------------------
74-
* @return [type] [description]
75-
*/
7660
public function runAuthServer()
7761
{
7862
if ($this->active) {
79-
$this->listen('LogonServer'); //开启监听
63+
64+
$this->ServerConfig = config('LogonServer');
65+
66+
$this->serv = SwooleTcp::Listen('0.0.0.0', $this->ServerConfig['Port'], new self());
67+
68+
//清空待连接池
69+
Cache::drive('redis')->delete('auth_checkconnector');
8070
} else {
8171
AUTH_LOG('Error: Did not start the service according to the process...');
8272
}
8373
}
8474

85-
/**
86-
* [listen 开启服务]
87-
* ------------------------------------------------------------------------------
88-
* @author by.fan <[email protected]>
89-
* ------------------------------------------------------------------------------
90-
* @version date:2019-04-19
91-
* ------------------------------------------------------------------------------
92-
* @return [type] [description]
93-
*/
94-
public function listen($config = null)
95-
{
96-
97-
$this->ServerConfig = config($config);
98-
99-
$this->serv = new \swoole_server("0.0.0.0", $this->ServerConfig['Port']);
100-
101-
$this->serv->set(array(
102-
'worker_num' => 4,
103-
// 'daemonize' => true, // 是否作为守护进程
104-
'max_request' => 10000,
105-
'heartbeat_check_interval' => 60 * 60, //每隔多少秒检测一次,单位秒,Swoole会轮询所有TCP连接,将超过心跳时间的连接关闭掉
106-
// 'log_file' => RUNTIME_PATH . 'swoole.log',
107-
// 'open_eof_check' => true, //打开EOF检测
108-
'package_eof' => "###", //设置EOF
109-
// 'open_eof_split'=>true, //是否分包
110-
'package_max_length' => 4096,
111-
));
112-
113-
$this->serv->on('Start', array(
114-
$this, 'onStart',
115-
));
116-
$this->serv->on('Connect', array(
117-
$this, 'onConnect',
118-
));
119-
$this->serv->on('Receive', array(
120-
$this, 'onReceive',
121-
));
122-
$this->serv->on('Close', array(
123-
$this, 'onClose',
124-
));
125-
$this->serv->on('WorkerStart', array(
126-
$this, 'onWorkerStart',
127-
));
128-
129-
//清空待连接池
130-
Cache::drive('redis')->delete('auth_checkconnector');
131-
132-
$this->serv->start();
133-
}
134-
135-
/**
136-
* Server启动在主进程的主线程回调此函数
137-
*
138-
* @param unknown $serv
139-
*/
14075
public function onStart($serv)
14176
{
14277
// 设置进程名称
143-
@cli_set_process_title("swoole_im_master");
78+
@cli_set_process_title("wow_auth_master");
79+
14480
AUTH_LOG("Start");
14581
}
14682

147-
/**
148-
* 有新的连接进入时,在worker进程中回调
149-
*
150-
* @param swoole_server $serv
151-
* @param int $fd
152-
* @param int $from_id
153-
*/
15483
public function onConnect($serv, $fd, $from_id)
15584
{
15685
$this->clearcache($fd);
157-
86+
15887
AUTH_LOG("Client {$fd} connect");
15988

16089
//初始化auth状态 0
16190
AuthServer::$clientparam[$fd]['state'] = Clientstate::Init;
16291

163-
Connection::saveCheckConnector($fd); //保存连接到待检池
92+
//保存连接到待检池
93+
Connection::saveCheckConnector($fd);
16494
}
16595

166-
/**
167-
* 接收到数据时回调此函数,发生在worker进程中
168-
*
169-
* @param swoole_server $serv
170-
* @param int $fd
171-
* @param int $from_id
172-
* @param var $data
173-
*/
17496
public function onReceive($serv, $fd, $from_id, $data)
17597
{
17698
AUTH_LOG("Get Message From Client {$fd}");
@@ -184,38 +106,26 @@ public function onReceive($serv, $fd, $from_id, $data)
184106
AUTH_LOG("Continue Handle Worker");
185107
}
186108

187-
/**
188-
* TCP客户端连接关闭后,在worker进程中回调此函数
189-
*
190-
* @param swoole_server $serv
191-
* @param int $fd
192-
* @param int $from_id
193-
*/
194109
public function onClose($serv, $fd, $from_id)
195110
{
196-
//清空用户信息
197111
$this->clearcache($fd);
198112

199-
// 将连接从连接池中移除
200113
Connection::removeConnector($fd);
114+
201115
AUTH_LOG("Client {$fd} close connection\n");
202116
}
203117

204-
/**
205-
* 此事件在worker进程/task进程启动时发生
206-
*
207-
* @param swoole_server $serv
208-
* @param int $worker_id
209-
*/
210118
public function onWorkerStart($serv, $worker_id)
211119
{
212120
AUTH_LOG("onWorkerStart");
213121

214122
if ($worker_id == 0) {
215123
if (!$serv->taskworker) {
216-
$serv->tick(5000, function ($id) {
217-
$this->tickerEvent($this->serv);
124+
125+
$serv->tick(5000, function ($id) use ($serv) {
126+
$this->tickerEvent($serv);
218127
});
128+
219129
} else {
220130
$serv->addtimer(5000);
221131
}
@@ -224,21 +134,17 @@ public function onWorkerStart($serv, $worker_id)
224134
}
225135
}
226136

227-
/**
228-
* 定时任务
229-
*
230-
* @param swoole_server $serv
231-
*/
232137
private function tickerEvent($serv)
233138
{
234139
Connection::clearInvalidConnection($serv);
235140
}
236141

237-
//清空redis
238142
private function clearcache($fd)
239143
{
240144
AUTH_LOG("Clear Cache");
145+
241146
AuthServer::$clientparam[$fd] = [];
147+
242148
unset(AuthServer::$clientparam[$fd]);
243149
}
244150
}

app/Auth/Challenge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getinfo_ClientLogonChallenge($data)
4242
$info['user_lenth'] = $data[33]; //用户名长度
4343
$info['username'] = array_slice($data, 34, $info['user_lenth']); //截取用户名
4444
$info['username'] = ToStr($info['username']);
45-
45+
4646
return $info;
4747
}
4848

app/Socket/SwooleTcp.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace app\Socket;
3+
4+
/**
5+
* tcp server
6+
*/
7+
class SwooleTcp
8+
{
9+
public static function Listen($addr, $port, $object)
10+
{
11+
$serv = new \Swoole\Server($addr, $port, SWOOLE_BASE, SWOOLE_SOCK_TCP);
12+
13+
$serv->set([
14+
'worker_num' => 4,
15+
//'daemonize' => true, // 是否作为守护进程
16+
'max_request' => 10000,
17+
'heartbeat_check_interval' => 60 * 60, //每隔多少秒检测一次,单位秒,Swoole会轮询所有TCP连接,将超过心跳时间的连接关闭掉
18+
// 'log_file' => RUNTIME_PATH . 'swoole.log',
19+
// 'open_eof_check' => true, //打开EOF检测
20+
// 'package_eof' => "###", //设置EOF
21+
// 'open_eof_split'=>true, //是否分包
22+
'package_max_length' => 4096,
23+
]);
24+
25+
$serv->on('Start', [$object, 'onStart']);
26+
27+
$serv->on('Connect', [$object, 'onConnect']);
28+
29+
$serv->on('Receive', [$object, 'onReceive']);
30+
31+
$serv->on('Close', [$object, 'onClose']);
32+
33+
$serv->on('WorkerStart', [$object, 'onWorkerStart']);
34+
35+
$serv->start();
36+
37+
return $serv;
38+
}
39+
}

app/Testsrp.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,54 @@ public function charenum()
182182

183183
public function run()
184184
{
185+
$field = ['character_spell.*'];
186+
187+
$where = [
188+
'characters.guid' => 62,
189+
];
190+
191+
$join = [
192+
['character_spell','character_spell.guid = characters.guid','inner'],
193+
];
194+
195+
$character_spell = DB::table('characters', 'characters')->field($field)->join($join)->where($where)->select();
196+
197+
$packdata = '';
198+
$spall_len = count($character_spell);
199+
$packdata .= pack('cv', 0, $spall_len);
200+
201+
$spellCount = 0;
202+
203+
foreach ($character_spell as $k => $v) {
204+
if(!$v['active'] || $v['disabled'])
205+
{
206+
continue;
207+
}
208+
209+
$packdata .= pack('v2', $v['spell'], 0);
210+
211+
$spellCount += 1;
212+
}
213+
214+
$packdata .= pack('v2',$spellCount,0);
215+
216+
$Srp6 = new Srp6();
217+
$packdata = $Srp6->BigInteger($packdata, 256)->toHex();
218+
219+
var_dump($packdata);die;
220+
221+
222+
223+
224+
225+
226+
227+
$data = '';
228+
for ($i = 0; $i < 128; $i++) {
229+
$data .= pack('c', 0);
230+
}
231+
$data = GetBytes($data);
232+
var_dump($data);die;
185233
$Srp6 = new Srp6();
186234
$sessionkey = 'F5AFC49E1798090EAD1BB1BAE68B8BFDD38BA36B8DB0803B2DEE094FC3D235501D4FB99289D7586D';
187235
$sessionkey = $Srp6->BigInteger($sessionkey, 16)->toBytes();

app/World/Addon/AddonHandler.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
namespace app\World\Addon;
3+
4+
use app\World\OpCode;
5+
use app\World\Packet\Packetmanager;
6+
use app\World\WorldServer;
7+
8+
/**
9+
* 插件程序
10+
*/
11+
class AddonHandler
12+
{
13+
//加载插件处理程序 TODO 实际获取插件
14+
public static function LoadAddonHandler($serv, $fd, $data = null)
15+
{
16+
WORLD_LOG('[SMSG_ADDON_INFO] Client : ' . $fd, 'warning');
17+
18+
$data = '';
19+
for ($i = 0; $i < 16; $i++) {
20+
$data .= pack('V2', 258, 0);
21+
}
22+
$data = GetBytes($data);
23+
24+
$encodeheader = Packetmanager::Worldpacket_encrypter($fd, [OpCode::SMSG_ADDON_INFO, $data, WorldServer::$clientparam[$fd]['sessionkey']]);
25+
$packdata = array_merge($encodeheader, $data);
26+
27+
return $packdata;
28+
}
29+
}

app/World/Challenge/AuthResponse.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
namespace app\World\Challenge;
3+
4+
use app\World\OpCode;
5+
use app\World\Packet\Packetmanager;
6+
use app\World\WorldServer;
7+
use app\Common\Srp6;
8+
9+
/**
10+
* 验证响应
11+
*/
12+
class AuthResponse
13+
{
14+
//验证响应
15+
public static function LoadAuthResponse($serv, $fd, $data = null)
16+
{
17+
WORLD_LOG('[SMSG_AUTH_RESPONSE] Client : ' . $fd, 'warning');
18+
19+
$Srp6 = new Srp6();
20+
$AUTH_OK = $Srp6->BigInteger(OpCode::AUTH_OK, 16)->toString();
21+
$BillingTimeRemaining = PackInt(0, 32);
22+
$BillingPlanFlags = PackInt(0, 8);
23+
$BillingTimeRested = PackInt(0, 32);
24+
$expansion = [(int) $data['data']['expansion']];
25+
$packdata = array_merge([(int) $AUTH_OK], $BillingTimeRemaining, $BillingPlanFlags, $BillingTimeRested, $expansion);
26+
$encodeheader = Packetmanager::Worldpacket_encrypter($fd, [OpCode::SMSG_AUTH_RESPONSE, $packdata, WorldServer::$clientparam[$fd]['sessionkey']]);
27+
$packdata = array_merge($encodeheader, $packdata);
28+
29+
return $packdata;
30+
}
31+
}

0 commit comments

Comments
 (0)