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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
pub mod nogamepads_client {
use std::collections::VecDeque;
use crate::pad_data::pad_messages::nogamepads_message_transfer::{read_msg, send_msg};
use crate::pad_data::pad_messages::nogamepads_messages::{ConnectionCallbackMessage, ConnectionErrorType, ConnectionMessage, ControlMessage, GameMessage, LeaveReason};
use crate::pad_data::pad_player_info::nogamepads_player_info::PlayerInfo;
use log::{error, info};
use std::net::{IpAddr, Ipv4Addr};
use std::process::exit;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use clap::CommandFactory;
use tokio::io::{AsyncReadExt, AsyncWriteExt, ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use tokio::{io, spawn};
use nogamepads::console_utils::debug_console::read_cli;
use nogamepads::convert_utils::convert_deque_to_vec;
use nogamepads::logger_utils::logger_build;
use crate::pad_io::client_debug_cli::{process_debug_cmd, Pcc};
use crate::DEFAULT_PORT;
use crate::pad_data::game_profile::game_profile::GameProfile;
use crate::pad_data::pad_messages::nogamepads_message_encoder::NgpdMessageEncoder;
type WriteList = Arc<Mutex<VecDeque<ControlMessage>>>;
type ReadList = Arc<Mutex<VecDeque<GameMessage>>>;
pub struct PadClient {
// --- 主要参数 ---
// 目标地址
target_address: IpAddr,
// 目标端口
#[allow(dead_code)]
target_port: u16,
// 绑定的玩家
bind_player: PlayerInfo,
// 调试模式
enable_console: bool,
// 保持安静,不初始化 env_logger
quiet: bool,
// --- 运行时参数 ---
// 发送信息列表
write_list: WriteList,
// 读取信息列表
read_list: ReadList,
// 是否退出
exit: AtomicBool,
}
impl Default for PadClient {
fn default() -> Self {
PadClient {
enable_console: false,
target_address: IpAddr::from(Ipv4Addr::new(127, 0, 0, 1)),
target_port: DEFAULT_PORT,
bind_player: PlayerInfo::new(),
quiet: false,
write_list: WriteList::default(),
read_list: ReadList::default(),
exit: AtomicBool::new(false)
}
}
}
// 客户端构建部分
impl PadClient {
pub fn bind_addr(address: IpAddr) -> PadClient {
PadClient {
target_address: address,
..PadClient::default()
}
}
pub fn bind_addr_with_port(address: IpAddr, port: u16) -> PadClient {
PadClient {
target_address: address,
target_port: port,
..PadClient::default()
}
}
pub fn enable_console(&mut self) {
self.enable_console = true;
}
pub fn quiet(&mut self) -> &mut PadClient {
self.quiet = true;
self
}
pub fn bind_player(&mut self, player: PlayerInfo) {
self.bind_player = player;
}
}
// 客户端消息管理
impl PadClient {
pub fn put_msg(&self, msg: ControlMessage) {
let mut guard = self.write_list.lock().unwrap();
guard.push_back(msg);
}
pub fn pop_a_msg(&self) -> Option<GameMessage> {
let mut guard = self.read_list.lock().unwrap();
if !guard.is_empty() {
guard.pop_front()
} else {
None
}
}
pub fn pop_msg_or(&self, or: GameMessage) -> GameMessage {
self.pop_a_msg().unwrap_or(or)
}
pub fn list_received(&self) -> Vec<GameMessage> {
match self.read_list.lock() {
Ok(guard) => {
convert_deque_to_vec(&guard.to_owned())
}
Err(_) => { Vec::new() }
}
}
}
// 客户端状态控制
impl PadClient {
pub fn connect(self) {
self.exit.store(false, SeqCst);
// 构建 Logger
if !self.quiet {
logger_build();
}
// 调试模式
let debug = self.enable_console;
// 客户端对象的 Arc
let arc_client = Arc::new(self);
// 部署环境
let runtime = tokio::runtime::Builder::new_multi_thread()
.thread_name("nogpad-pad_io")
.thread_stack_size(32 * 1024 * 1024)
.enable_time()
.enable_io()
.build()
.unwrap();
info!("Starting \"NoGamepads Client\".");
// 入口
let entry = async move {
let main_thread = spawn({
let client = Arc::clone(&arc_client);
async move {
Self::main_client_thread(client).await
}
});
let background_thread = spawn({
let client = Arc::clone(&arc_client);
async move {
Self::background_thread(client).await
}
});
if debug {
let debug_cli = spawn({
let client = Arc::clone(&arc_client);
async move {
Self::process_debug_cli(client).await
}
});
let _ = tokio::join!(debug_cli, main_thread, background_thread);
} else {
let _ = tokio::join!(main_thread, background_thread);
}
};
// 阻塞运行
runtime.block_on(entry);
}
pub fn exit_server(&self) {
self.exit.store(true, SeqCst);
}
async fn main_client_thread(self: Arc<Self>) {
let mut buffer : [u8; 1024] = [0; 1024];
let addr_str = format!("{}:{}", self.target_address.to_string(), DEFAULT_PORT);
info!("Connected to {}", &addr_str);
// 下载服务端配置文件
{
info!("Check: Downloaded game profile.");
let profile = self.check_server_profile(&mut buffer, addr_str.clone()).await;
if profile.is_some() {
info!("Success: Downloaded.");
let profile = profile.unwrap_or(GameProfile::default());
for line in profile.to_string().split('\n') {
info!("{}", line);
}
}
else {
error!("Failed: Can't download profile!");
self.exit_server();
}
}
// 尝试加入服务端,并建立长连接
{
if !self.try_join_game(&mut buffer, addr_str.clone()).await {
error!("Failed: Can't join the game!");
self.exit_server();
return;
}
}
}
async fn check_server_profile(self: &Arc<Self>, buffer: &mut [u8], addr_str: String) -> Option<GameProfile> {
match TcpStream::connect(&addr_str).await {
Ok(mut stream) => {
send_msg(&mut stream, ConnectionMessage::RequestProfile).await;
let callback : ConnectionCallbackMessage = read_msg(buffer, &mut stream).await;
match callback {
ConnectionCallbackMessage::Profile(profile) => {
Some(profile)
}
ConnectionCallbackMessage::Deny(err_type) => {
error!("Request failed: Server denied your request! ({:?})", err_type);
None
}
ConnectionCallbackMessage::Err => {
error!("Connection failed: Can't connect to server!");
None
}
_ => { None }
}
}
Err(_err) => {
None
}
}
}
async fn try_join_game(self: &Arc<Self>, buffer: &mut [u8], addr_str: String) -> bool {
match TcpStream::connect(&addr_str).await {
Ok(mut stream) => {
// 发送连接请求
let info = self.bind_player.clone();
send_msg(&mut stream, ConnectionMessage::Connection(info)).await;
// 读取回调
let callback : ConnectionCallbackMessage = read_msg(buffer, &mut stream).await;
match callback {
ConnectionCallbackMessage::Deny(error) => {
match error {
ConnectionErrorType::ContainSamePlayer => {
error!("Connection failed: Contains same player!");
false
}
ConnectionErrorType::PlayerBanned => {
error!("Connection failed: You are banned!");
false
}
ConnectionErrorType::Timeout => {
error!("Connection failed: Timeout!");
false
}
ConnectionErrorType::GameLocked => {
error!("Connection failed: Game was locked!");
false
}
_ => { false }
}
}
ConnectionCallbackMessage::Ok => {
// 服务端检查完毕,发送 Ready 以示加入游戏
send_msg(&mut stream, ConnectionMessage::Ready).await;
let callback : ConnectionCallbackMessage = read_msg(buffer, &mut stream).await;
match callback {
ConnectionCallbackMessage::Welcome => {
info!("Welcome!");
Self::long_connection(Arc::clone(&self), stream).await;
}
ConnectionCallbackMessage::Deny(_error) => {
error!("Request failed: Server denied your request",);
}
_ => {}
}
true
}
_ => { false }
}
}
Err(err) => {
error!("Failed to connect to server: {}", err);
false
}
}
}
async fn long_connection(self: Arc<Self>, stream: TcpStream) {
let (reader, writer) = io::split(stream);
spawn(Self::read_task(Arc::clone(&self), reader));
spawn(Self::write_task(Arc::clone(&self), writer));
}
async fn read_task(self: Arc<Self>, mut reader: ReadHalf<TcpStream>) {
let mut buf = [0u8; 1024];
loop {
match reader.read(&mut buf).await {
Ok(0) => break,
Ok(n) => {
let msg = GameMessage::de(buf[0..n].to_vec());
{
match self.read_list.lock() {
Ok(mut guard) => {
match &msg {
GameMessage::Leave(reason) => {
match reason {
LeaveReason::GameOver => {
info!("Leave Game: Game Over!");
self.exit_server();
}
LeaveReason::ServerClosed => {
info!("Leave Game: Server closed!");
self.exit_server();
}
LeaveReason::YouAreKicked => {
error!("Kick Game: You are kicked!");
self.exit_server();
}
LeaveReason::YouAreBanned => {
error!("Kick Game: You are banned!");
self.exit_server();
}
}
}
_ => {
info!("{:?}", &msg);
guard.push_back(msg);
}
}
}
Err(_) => {}
}
}
}
Err(e) => {
error!("Error reading from stream: {}", e);
self.exit_server();
break;
}
}
}
}
async fn write_task(self: Arc<Self>, mut writer: WriteHalf<TcpStream>) {
loop {
let msg : Option<ControlMessage>;
{
let lock = self.write_list.lock();
match lock {
Ok(mut guard) => {
if ! guard.is_empty() {
msg = guard.pop_front();
} else {
msg = None;
}
}
Err(_) => {
msg = None;
}
}
}
if msg.is_some() {
let msg = msg.unwrap();
match &writer.write_all(NgpdMessageEncoder::en(&msg).as_slice()).await {
Ok(_) => {
info!("Sent {:?}", msg);
}
Err(_error) => {
error!("Sent {:?} failed!", msg);
}
}
}
}
}
async fn background_thread(self: Arc<Self>) {
loop {
// 退出程序的监听
if self.exit.load(SeqCst) {
tokio::time::sleep(Duration::from_secs(1)).await;
info!("Main thread exited.");
exit(0);
}
}
}
async fn process_debug_cli(self: Arc<Self>) {
loop {
if self.exit.load(SeqCst) {
info!("Debug console exited");
break
}
tokio::time::sleep(Duration::from_secs_f64(0.2)).await;
let option: Option<Pcc> = read_cli(
format!("CLIENT {}/{}> ",
self.target_address.to_string(),
self.bind_player.account.id).as_str(),
"pcc".to_string(),
Pcc::command()
).await;
match option {
None => {}
Some(cmd) => {
process_debug_cmd(cmd, Arc::clone(&self));
}
}
}
}
}
}
|