aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWeicao Mao <1992414357@qq.com>2025-04-22 02:09:49 +0800
committerGitHub <noreply@github.com>2025-04-22 02:09:49 +0800
commit9a61a4f3d2520f657a864b931cf1b4f5c5451962 (patch)
tree48ea97e0c5667dd6422b03198c18fd135646fb44
parentaeff5141bb6efb0a94d74b9e852c2c10074360b2 (diff)
parenteb4d5568e5e4de227a5dcad28e9633baa33700b1 (diff)
Merge pull request #1 from CatilGrass/developHEADmain
完善 MessageEncoder
-rw-r--r--.idea/dictionaries/CatilGrass.xml7
-rw-r--r--.idea/runConfigurations/Run_Console_Client.xml3
-rw-r--r--.idea/runConfigurations/Run_Console_Server.xml2
-rw-r--r--.idea/runConfigurations/Run_Test.xml2
-rw-r--r--.idea/runConfigurations/Test_ALL.xml20
-rw-r--r--.idea/runConfigurations/Test_Message_Encoder.xml20
-rw-r--r--.idea/workspace.xml114
-rw-r--r--core/src/client/client.rs4
-rw-r--r--core/src/constants.rs12
-rw-r--r--core/src/lib.rs1
-rw-r--r--core/src/message_encoder.rs46
-rw-r--r--core/src/messages.rs30
-rw-r--r--core/src/player/player_info.rs6
-rw-r--r--core/src/server/server.rs27
-rw-r--r--core/src/test_message_encoder.rs15
-rw-r--r--core/test/src/bin/console_client.rs18
-rw-r--r--core/test/src/bin/console_server.rs9
17 files changed, 280 insertions, 56 deletions
diff --git a/.idea/dictionaries/CatilGrass.xml b/.idea/dictionaries/CatilGrass.xml
new file mode 100644
index 0000000..6fe5bae
--- /dev/null
+++ b/.idea/dictionaries/CatilGrass.xml
@@ -0,0 +1,7 @@
+<component name="ProjectDictionaryState">
+ <dictionary name="CatilGrass">
+ <words>
+ <w>bincode</w>
+ </words>
+ </dictionary>
+</component> \ No newline at end of file
diff --git a/.idea/runConfigurations/Run_Console_Client.xml b/.idea/runConfigurations/Run_Console_Client.xml
index 8dff88c..490a86d 100644
--- a/.idea/runConfigurations/Run_Console_Client.xml
+++ b/.idea/runConfigurations/Run_Console_Client.xml
@@ -1,5 +1,6 @@
<component name="ProjectRunConfigurationManager">
- <configuration default="false" name="Run Console Client" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Test">
+ <configuration default="false" name="Run Console Client" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Console">
+ <option name="buildProfileId" value="dev" />
<option name="command" value="run --package there-is-nopads-test --bin console_client" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/core/test" />
<envs />
diff --git a/.idea/runConfigurations/Run_Console_Server.xml b/.idea/runConfigurations/Run_Console_Server.xml
index c943fb8..732bf70 100644
--- a/.idea/runConfigurations/Run_Console_Server.xml
+++ b/.idea/runConfigurations/Run_Console_Server.xml
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
- <configuration default="false" name="Run Console Server" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Test">
+ <configuration default="false" name="Run Console Server" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Console">
<option name="command" value="run --package there-is-nopads-test --bin console_server" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/core/test" />
<envs />
diff --git a/.idea/runConfigurations/Run_Test.xml b/.idea/runConfigurations/Run_Test.xml
index e12228f..053cd05 100644
--- a/.idea/runConfigurations/Run_Test.xml
+++ b/.idea/runConfigurations/Run_Test.xml
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
- <configuration default="false" name="Run Test" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Test">
+ <configuration default="false" name="Run Test" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Console">
<option name="buildProfileId" value="dev" />
<option name="command" value="run --package there-is-nopads-test --bin there-is-nopads-test" />
<option name="workingDirectory" value="file://$PROJECT_DIR$/core/test" />
diff --git a/.idea/runConfigurations/Test_ALL.xml b/.idea/runConfigurations/Test_ALL.xml
new file mode 100644
index 0000000..e61d396
--- /dev/null
+++ b/.idea/runConfigurations/Test_ALL.xml
@@ -0,0 +1,20 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="Test ALL" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Unit_Tests">
+ <option name="buildProfileId" value="dev" />
+ <option name="command" value="run" />
+ <option name="workingDirectory" value="file://$PROJECT_DIR$/client" />
+ <envs />
+ <option name="emulateTerminal" value="true" />
+ <option name="channel" value="DEFAULT" />
+ <option name="requiredFeatures" value="true" />
+ <option name="allFeatures" value="false" />
+ <option name="withSudo" value="false" />
+ <option name="buildTarget" value="REMOTE" />
+ <option name="backtrace" value="SHORT" />
+ <option name="isRedirectInput" value="false" />
+ <option name="redirectInputPath" value="" />
+ <method v="2">
+ <option name="RunConfigurationTask" enabled="true" run_configuration_name="Test Message Encoder" run_configuration_type="CargoCommandRunConfiguration" />
+ </method>
+ </configuration>
+</component> \ No newline at end of file
diff --git a/.idea/runConfigurations/Test_Message_Encoder.xml b/.idea/runConfigurations/Test_Message_Encoder.xml
new file mode 100644
index 0000000..25a0d5a
--- /dev/null
+++ b/.idea/runConfigurations/Test_Message_Encoder.xml
@@ -0,0 +1,20 @@
+<component name="ProjectRunConfigurationManager">
+ <configuration default="false" name="Test Message Encoder" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Unit_Tests">
+ <option name="buildProfileId" value="test" />
+ <option name="command" value="test --package there-is-nopads-core --lib test_message_encoder::test::test_message_encoder -- --exact" />
+ <option name="workingDirectory" value="file://$PROJECT_DIR$/core" />
+ <envs />
+ <option name="emulateTerminal" value="true" />
+ <option name="channel" value="DEFAULT" />
+ <option name="requiredFeatures" value="true" />
+ <option name="allFeatures" value="false" />
+ <option name="withSudo" value="false" />
+ <option name="buildTarget" value="REMOTE" />
+ <option name="backtrace" value="SHORT" />
+ <option name="isRedirectInput" value="false" />
+ <option name="redirectInputPath" value="" />
+ <method v="2">
+ <option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
+ </method>
+ </configuration>
+</component> \ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index e053028..5fbc683 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -9,10 +9,12 @@
<cargoProject FILE="$PROJECT_DIR$/core/test/Cargo.toml" />
</component>
<component name="ChangeListManager">
- <list default="true" id="43098f42-7a3b-4fc2-ad78-a08c96f7f5db" name="Changes" comment=".">
- <change beforePath="$PROJECT_DIR$/.idea/runConfigurations/Test_Message_Encoder.xml" beforeDir="false" />
+ <list default="true" id="43098f42-7a3b-4fc2-ad78-a08c96f7f5db" name="Changes" comment="添加单元测试">
+ <change beforePath="$PROJECT_DIR$/.idea/runConfigurations/Run_Console_Client.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/runConfigurations/Run_Console_Client.xml" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/.idea/runConfigurations/Run_Console_Server.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/runConfigurations/Run_Console_Server.xml" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/.idea/runConfigurations/Run_Test.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/runConfigurations/Run_Test.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
- <change beforePath="$PROJECT_DIR$/core/src/message_encoder.rs" beforeDir="false" afterPath="$PROJECT_DIR$/core/src/message_encoder.rs" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/core/src/player/player_info.rs" beforeDir="false" afterPath="$PROJECT_DIR$/core/src/player/player_info.rs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -31,6 +33,11 @@
<option name="NEW_BASE" value="main" />
</component>
<component name="Git.Settings">
+ <option name="RECENT_BRANCH_BY_REPOSITORY">
+ <map>
+ <entry key="$PROJECT_DIR$" value="main" />
+ </map>
+ </option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="GitHubPullRequestSearchHistory"><![CDATA[{
@@ -45,6 +52,9 @@
"accountId": "34f7046b-db2c-4cf0-8894-3b6963dd549d"
}
}]]></component>
+ <component name="HighlightingSettingsPerFile">
+ <setting file="file://$USER_HOME$/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bincode-2.0.1/src/de/mod.rs" root0="SKIP_INSPECTION" />
+ </component>
<component name="MacroExpansionManager">
<option name="directoryName" value="EK7IzXE2" />
</component>
@@ -64,16 +74,19 @@
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Attach to Node.js/Chrome.Unnamed.executor": "Debug",
+ "Cargo.Run Console Client.executor": "Run",
+ "Cargo.Run Test.executor": "Run",
"Cargo.Run phantomlink-gamepad-client.executor": "Run",
"Cargo.Run phantomlink-server-test.executor": "Run",
"Cargo.Test Message Encoder.executor": "Run",
"Cargo.Test encoder_tests.executor": "Run",
"Cargo.Test message_encoder::encoder_tests::test_encode_decode.executor": "Run",
+ "Cargo.Test test_message_encoder::test::test_message_encoder.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
"RunOnceActivity.rust.reset.selective.auto.import": "true",
"Shell Script.Shell Generate Documents.executor": "Run",
- "git-widget-placeholder": "main",
+ "git-widget-placeholder": "develop",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -85,17 +98,24 @@
"vue.rearranger.settings.migration": "true"
}
}]]></component>
- <component name="RunManager" selected="Cargo.Run Test">
+ <component name="RunManager" selected="Cargo.Run Console Client">
<list>
<item itemvalue="Cargo.Run Console Client" />
<item itemvalue="Cargo.Run Console Server" />
<item itemvalue="Cargo.Run Test" />
+ <item itemvalue="Cargo.Test ALL" />
+ <item itemvalue="Cargo.Test Message Encoder" />
<item itemvalue="Cargo.Run Gamepad Client" />
<item itemvalue="Shell Script.Generate Documents (Linux)" />
<item itemvalue="Shell Script.Initialize (Linux)" />
<item itemvalue="Shell Script.Generate Documents (Windows)" />
<item itemvalue="Shell Script.Initialize (Windows)" />
</list>
+ <recent_temporary>
+ <list>
+ <item itemvalue="Cargo.Test Message Encoder" />
+ </list>
+ </recent_temporary>
</component>
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="$USER_HOME$/.cargo/bin" />
@@ -112,7 +132,7 @@
<workItem from="1745180836475" duration="8116000" />
<workItem from="1745249796881" duration="84000" />
<workItem from="1745249888324" duration="2865000" />
- <workItem from="1745253286696" duration="65000" />
+ <workItem from="1745253286696" duration="5515000" />
</task>
<task id="LOCAL-00001" summary="修改作者所有权">
<option name="closed" value="true" />
@@ -130,7 +150,79 @@
<option name="project" value="LOCAL" />
<updated>1745187955275</updated>
</task>
- <option name="localTasksCounter" value="3" />
+ <task id="LOCAL-00003" summary=".">
+ <option name="closed" value="true" />
+ <created>1745253542975</created>
+ <option name="number" value="00003" />
+ <option name="presentableId" value="LOCAL-00003" />
+ <option name="project" value="LOCAL" />
+ <updated>1745253542975</updated>
+ </task>
+ <task id="LOCAL-00004" summary="修改测试">
+ <option name="closed" value="true" />
+ <created>1745253762526</created>
+ <option name="number" value="00004" />
+ <option name="presentableId" value="LOCAL-00004" />
+ <option name="project" value="LOCAL" />
+ <updated>1745253762526</updated>
+ </task>
+ <task id="LOCAL-00005" summary="添加单元测试配置文件">
+ <option name="closed" value="true" />
+ <created>1745258666544</created>
+ <option name="number" value="00005" />
+ <option name="presentableId" value="LOCAL-00005" />
+ <option name="project" value="LOCAL" />
+ <updated>1745258666544</updated>
+ </task>
+ <task id="LOCAL-00006" summary=".">
+ <option name="closed" value="true" />
+ <created>1745258672581</created>
+ <option name="number" value="00006" />
+ <option name="presentableId" value="LOCAL-00006" />
+ <option name="project" value="LOCAL" />
+ <updated>1745258672581</updated>
+ </task>
+ <task id="LOCAL-00007" summary="默认端口号常量命名变更">
+ <option name="closed" value="true" />
+ <created>1745258744905</created>
+ <option name="number" value="00007" />
+ <option name="presentableId" value="LOCAL-00007" />
+ <option name="project" value="LOCAL" />
+ <updated>1745258744905</updated>
+ </task>
+ <task id="LOCAL-00008" summary="客户端和服务端控制台的完善">
+ <option name="closed" value="true" />
+ <created>1745258772264</created>
+ <option name="number" value="00008" />
+ <option name="presentableId" value="LOCAL-00008" />
+ <option name="project" value="LOCAL" />
+ <updated>1745258772264</updated>
+ </task>
+ <task id="LOCAL-00009" summary="添加单元测试">
+ <option name="closed" value="true" />
+ <created>1745258789452</created>
+ <option name="number" value="00009" />
+ <option name="presentableId" value="LOCAL-00009" />
+ <option name="project" value="LOCAL" />
+ <updated>1745258789452</updated>
+ </task>
+ <task id="LOCAL-00010" summary="完善消息编码器">
+ <option name="closed" value="true" />
+ <created>1745258815713</created>
+ <option name="number" value="00010" />
+ <option name="presentableId" value="LOCAL-00010" />
+ <option name="project" value="LOCAL" />
+ <updated>1745258815713</updated>
+ </task>
+ <task id="LOCAL-00011" summary="添加单元测试">
+ <option name="closed" value="true" />
+ <created>1745258824923</created>
+ <option name="number" value="00011" />
+ <option name="presentableId" value="LOCAL-00011" />
+ <option name="project" value="LOCAL" />
+ <updated>1745258824923</updated>
+ </task>
+ <option name="localTasksCounter" value="12" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@@ -138,8 +230,14 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="修改作者所有权" />
+ <MESSAGE value="修改测试" />
+ <MESSAGE value="添加单元测试配置文件" />
<MESSAGE value="." />
- <option name="LAST_COMMIT_MESSAGE" value="." />
+ <MESSAGE value="默认端口号常量命名变更" />
+ <MESSAGE value="客户端和服务端控制台的完善" />
+ <MESSAGE value="完善消息编码器" />
+ <MESSAGE value="添加单元测试" />
+ <option name="LAST_COMMIT_MESSAGE" value="添加单元测试" />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />
diff --git a/core/src/client/client.rs b/core/src/client/client.rs
index c11bb61..e861168 100644
--- a/core/src/client/client.rs
+++ b/core/src/client/client.rs
@@ -1,4 +1,4 @@
-use crate::constants::DEFAULT_PORT;
+use crate::constants::DEFAULT_PORT_UDP;
use crate::player::player_info::PlayerInfo;
use std::net::SocketAddr;
@@ -14,7 +14,7 @@ impl Default for PadClient {
fn default() -> Self {
PadClient {
player_info: PlayerInfo::default(),
- remote_addr: SocketAddr::from(([127, 0, 0, 1], DEFAULT_PORT)),
+ remote_addr: SocketAddr::from(([127, 0, 0, 1], DEFAULT_PORT_UDP)),
receive_skin_package: true
}
}
diff --git a/core/src/constants.rs b/core/src/constants.rs
index 6257c9d..ef5a3d6 100644
--- a/core/src/constants.rs
+++ b/core/src/constants.rs
@@ -1,8 +1,16 @@
use bincode::config;
use bincode::config::Configuration;
+// 默认 Hash 加盐
pub const SALT : &str = ":)";
-pub const DEFAULT_PORT : u16 = 8916;
+// 服务器默认端口
+pub const DEFAULT_PORT_UDP: u16 = 8916;
+pub const DEFAULT_PORT_TCP : u16 = 8916;
-pub const BINCODE_CONFIG : Configuration = config::standard(); \ No newline at end of file
+// 加密
+// BinCode 2.0 默认配置
+pub const BINCODE_CONFIG : Configuration = config::standard();
+
+// 加密失败时的值
+pub const BINCODE_CONVERT_FAILED : Vec<u8> = Vec::new(); \ No newline at end of file
diff --git a/core/src/lib.rs b/core/src/lib.rs
index d8b1df8..af919ff 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -7,3 +7,4 @@ pub mod messages;
// Private
mod constants;
mod message_encoder;
+mod test_message_encoder;
diff --git a/core/src/message_encoder.rs b/core/src/message_encoder.rs
index cda7382..0ef7fc8 100644
--- a/core/src/message_encoder.rs
+++ b/core/src/message_encoder.rs
@@ -1,7 +1,47 @@
-use crate::messages::PlayerMessage;
+use crate::constants::{BINCODE_CONFIG, BINCODE_CONVERT_FAILED};
+use crate::messages::{ClientMessage, PlayerMessage, ServerCallbackMessage, ServerMessage};
+use std::fmt::Debug;
+use bincode::{Decode, Encode};
-impl PlayerMessage {
- pub fn encode (&self) {
+pub trait LazyMessageEncoder<Message: Encode + Decode<()>> {
+ fn err_result_decode () -> Message;
+ fn err_result_encode () -> Vec<u8> {
+ BINCODE_CONVERT_FAILED
+ }
+
+ fn encode (&self) -> Vec<u8> where Self : Encode {
+ bincode::encode_to_vec(self, BINCODE_CONFIG)
+ .unwrap_or_else(|_| Self::err_result_encode())
+ }
+
+ fn decode (encoded : Vec<u8>) -> Message {
+ match bincode::decode_from_slice(&encoded[..], BINCODE_CONFIG) {
+ Ok((decoded, _)) => decoded,
+ Err(_) => Self::err_result_decode()
+ }
+ }
+}
+
+impl LazyMessageEncoder<PlayerMessage> for PlayerMessage {
+ fn err_result_decode() -> PlayerMessage {
+ PlayerMessage::Error
+ }
+}
+
+impl LazyMessageEncoder<ServerMessage> for ServerMessage {
+ fn err_result_decode() -> ServerMessage {
+ ServerMessage::Error
+ }
+}
+
+impl LazyMessageEncoder<ClientMessage> for ClientMessage {
+ fn err_result_decode() -> ClientMessage {
+ ClientMessage::Error
+ }
+}
+impl LazyMessageEncoder<ServerCallbackMessage> for ServerCallbackMessage {
+ fn err_result_decode() -> ServerCallbackMessage {
+ ServerCallbackMessage::Error
}
} \ No newline at end of file
diff --git a/core/src/messages.rs b/core/src/messages.rs
index 2ae8328..b17faeb 100644
--- a/core/src/messages.rs
+++ b/core/src/messages.rs
@@ -1,10 +1,10 @@
use std::time::Duration;
-use serde::{Deserialize, Serialize};
+use bincode::{Decode, Encode};
use crate::player::player_info::PlayerInfo;
/// # 玩家消息
/// 玩家 --> 服务器
-#[derive(Serialize, Deserialize, PartialEq)]
+#[derive(Encode, Decode, PartialEq, Debug)]
pub enum PlayerMessage {
/// 消息:发送字符串,服务端无需回复
Message(String),
@@ -22,12 +22,15 @@ pub enum PlayerMessage {
/// u8:轴向代号
/// f64:X 轴向值 (0 - 1)
/// f64:Y 轴向值 (0 - 1)
- Axis(u8, (f64, f64))
+ Axis(u8, (f64, f64)),
+
+ /// 错误消息
+ Error
}
/// # 服务器消息
/// 服务器 --> 玩家
-#[derive(Serialize, Deserialize, PartialEq)]
+#[derive(Encode, Decode, PartialEq, Debug)]
pub enum ServerMessage {
/// 命令:发送字符串,客户端处理
@@ -41,12 +44,15 @@ pub enum ServerMessage {
/// 踢出:发送踢出指令,以便让客户端明白自己的掉线是因为被踢出
/// “杀人还要诛心!!!好可怕啊!”
- Kick
+ Kick,
+
+ /// 错误消息
+ Error
}
/// # 客户端消息
/// 客户端 --> 服务器
-#[derive(Serialize, Deserialize, PartialEq)]
+#[derive(Encode, Decode, PartialEq, Debug)]
pub enum ClientMessage {
/// 连接请求,将和服务端建立连接,并通知服务器将要建立连接的玩家
@@ -59,12 +65,15 @@ pub enum ClientMessage {
RequestSkin,
/// 客户端准备就绪
- Ready
+ Ready,
+
+ /// 错误消息
+ Error
}
/// # 服务器回调消息
/// 服务器 --> 客户端
-#[derive(Serialize, Deserialize, PartialEq)]
+#[derive(Encode, Decode, PartialEq, Debug)]
pub enum ServerCallbackMessage {
/// 拒绝:发送消息已通知客户端请求被拒绝
@@ -74,5 +83,8 @@ pub enum ServerCallbackMessage {
Accept,
/// 欢迎:客户端准备完成后,服务端发送 Welcome 以允许客户端以玩家加入游戏
- Welcome
+ Welcome,
+
+ /// 错误消息
+ Error
} \ No newline at end of file
diff --git a/core/src/player/player_info.rs b/core/src/player/player_info.rs
index 6412b66..3c02889 100644
--- a/core/src/player/player_info.rs
+++ b/core/src/player/player_info.rs
@@ -1,10 +1,10 @@
+use crate::constants::SALT;
+use bincode::{Decode, Encode};
use hex::encode;
-use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1};
-use crate::constants::SALT;
/// 玩家信息
-#[derive(Serialize, Deserialize, PartialEq)]
+#[derive(Encode, Decode, PartialEq, Debug)]
pub struct PlayerInfo {
pub display_name: String,
pub name_hash: String,
diff --git a/core/src/server/server.rs b/core/src/server/server.rs
index c71892c..7f0eec3 100644
--- a/core/src/server/server.rs
+++ b/core/src/server/server.rs
@@ -1,4 +1,4 @@
-use crate::constants::DEFAULT_PORT;
+use crate::constants::{DEFAULT_PORT_TCP, DEFAULT_PORT_UDP};
use crate::server::server_event::PadServerEvent;
use std::net::{SocketAddr, UdpSocket};
@@ -6,7 +6,8 @@ use std::net::{SocketAddr, UdpSocket};
/// 监听服务器用于连接玩家设备,并接收玩家的按键输入
pub struct PadServer {
server_name: String,
- port: u16
+ port_request_server: u16,
+ port_io_server: u16
}
impl Default for PadServer {
@@ -16,7 +17,8 @@ impl Default for PadServer {
fn default() -> Self {
PadServer {
server_name: String::from("This Is No Pads"),
- port: DEFAULT_PORT
+ port_io_server: DEFAULT_PORT_UDP,
+ port_request_server: DEFAULT_PORT_TCP,
// TODO :: 补充布局文件
}
}
@@ -28,24 +30,7 @@ impl PadServer {
/// self : 服务器
/// event : 事件
pub fn start_listening(&self, event : impl PadServerEvent) {
- let socket_addr = SocketAddr::from(([127, 0, 0, 1], self.port));
- match UdpSocket::bind(socket_addr) {
- Ok(socket) => {
- let mut buf = vec![0u8; 1024];
-
- loop {
- // 接收连接请求
- match socket.recv_from(&mut buf) {
- Ok((size, src)) => {
-
- }
- Err(_) => {}
- }
- }
- }
-
- Err(_) => {}
- }
+
}
/// # 停止监听服务器
diff --git a/core/src/test_message_encoder.rs b/core/src/test_message_encoder.rs
new file mode 100644
index 0000000..a4d118c
--- /dev/null
+++ b/core/src/test_message_encoder.rs
@@ -0,0 +1,15 @@
+#[cfg(test)]
+mod test {
+ use crate::message_encoder::LazyMessageEncoder;
+ use crate::messages::PlayerMessage;
+
+ #[test]
+ fn test_message_encoder() {
+ let player_message = PlayerMessage::Message(String::from("Hello, World!"));
+ let player_message_encoded = player_message.encode();
+ let player_message_decoded = PlayerMessage::decode(player_message_encoded.clone());
+
+ assert_eq!(player_message, player_message_decoded);
+ println!("Done");
+ }
+} \ No newline at end of file
diff --git a/core/test/src/bin/console_client.rs b/core/test/src/bin/console_client.rs
index 80bf28c..578a5b5 100644
--- a/core/test/src/bin/console_client.rs
+++ b/core/test/src/bin/console_client.rs
@@ -1,11 +1,23 @@
-use there_is_nopads_core::pads_player::{PadClient, PadClientEvent, PlayerInfo};
-use there_is_nopads_core::pads_server::ServerMessage;
+use there_is_nopads_core::client::client::PadClient;
+use there_is_nopads_core::client::client_event::PadClientEvent;
+use there_is_nopads_core::messages::ServerMessage;
+use there_is_nopads_core::player::player_info::PlayerInfo;
fn main() {
+ // 客户端
let client = PadClient::default();
+
+ // 玩家
let mut player = PlayerInfo::default();
+
+ // 设置玩家信息
player.name(String::from("Player"), String::from("catilgrass"));
- println!("{}", player.name_hash);
+
+ // 输出玩家 Hash
+ println!("Hash : {}", player.name_hash);
+
+ // 连接到测试服务器
+ client.connect_server();
}
struct Event;
diff --git a/core/test/src/bin/console_server.rs b/core/test/src/bin/console_server.rs
index 4536c74..26476fa 100644
--- a/core/test/src/bin/console_server.rs
+++ b/core/test/src/bin/console_server.rs
@@ -1,8 +1,13 @@
-use there_is_nopads_core::pads_player::{PlayerInfo, PlayerMessage};
-use there_is_nopads_core::pads_server::{PadServer, PadServerEvent};
+use there_is_nopads_core::messages::PlayerMessage;
+use there_is_nopads_core::player::player_info::PlayerInfo;
+use there_is_nopads_core::server::server::PadServer;
+use there_is_nopads_core::server::server_event::PadServerEvent;
fn main() {
+ // 服务端
let server = PadServer::default();
+
+ // 开始监听
server.start_listening(Event);
}