This commit is contained in:
taynpg 2025-02-05 16:57:04 +08:00
commit c19b25073f
13 changed files with 864 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

14
.idea/ftrans.iml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/client/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/futil/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/server/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ftrans.iml" filepath="$PROJECT_DIR$/.idea/ftrans.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

547
Cargo.lock generated Normal file
View File

@ -0,0 +1,547 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "addr2line"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1"
dependencies = [
"gimli",
]
[[package]]
name = "adler2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "backtrace"
version = "0.3.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"windows-targets",
]
[[package]]
name = "bitflags"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "bytes"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9"
[[package]]
name = "cc"
version = "1.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755717a7de9ec452bf7f3f1a3099085deabd7f2962b861dae91ecd7a365903d2"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets",
]
[[package]]
name = "colored"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e"
dependencies = [
"windows-sys",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "futil"
version = "0.1.0"
dependencies = [
"chrono",
"colored",
]
[[package]]
name = "gimli"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "iana-time-zone"
version = "0.1.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "lock_api"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "miniz_oxide"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924"
dependencies = [
"adler2",
]
[[package]]
name = "mio"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
dependencies = [
"libc",
"wasi",
"windows-sys",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.36.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "parking_lot"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "pin-project-lite"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "proc-macro2"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
dependencies = [
"bitflags",
]
[[package]]
name = "rustc-demangle"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "rustversion"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
]
[[package]]
name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "socket2"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "syn"
version = "2.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tokio"
version = "1.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
]
[[package]]
name = "tokio-macros"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tsc"
version = "0.1.0"
dependencies = [
"futil",
"tokio",
]
[[package]]
name = "tss"
version = "0.1.0"
dependencies = [
"tokio",
]
[[package]]
name = "unicode-ident"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

7
Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[workspace]
members = [
"client",
"server",
"futil"
]
resolver = "2" # 启用 Edition 2021 的解析器

8
client/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "tsc"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1.43.0", features = ["full"] }
futil = { path = "../futil"}

8
client/src/main.rs Normal file
View File

@ -0,0 +1,8 @@
use futil::*;
fn main() {
let name = "Rust";
let version = 1.56;
printed!("{} version {} is awesome!", name, version);
printer!("{} version {} is awesome!", name, version);
}

8
futil/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "futil"
version = "0.1.0"
edition = "2021"
[dependencies]
colored = "3.0.0"
chrono = "0.4.39"

239
futil/src/lib.rs Normal file
View File

@ -0,0 +1,239 @@
// log_crate/src/lib.rs
use chrono::Local; // 用于获取当前时间
use colored::*;
use std::cmp::min;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
// 用于终端彩色输出
/// 模拟当前时间的函数
fn now_str() -> String {
Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
}
// 通用的格式化打印函数,支持传递多个参数
pub fn printed(format: &str) {
// 打印彩色的日志信息,带时间戳
println!("{}", format!("[{}] {}", now_str(), format).green());
}
pub fn printer(format: &str) {
// 打印彩色的日志信息,带时间戳
println!("{}", format!("[{}] {}", now_str(), format).red());
}
// 这里作一些新手说明:
// ************************************************************************************************
// $crate::xxx 必须指向一个在当前 crate 中定义的 xxx 函数,或者其他合适的函数实现。
// $crate 确保了宏内部可以正确引用当前 crate 中的函数或类型定义。
// ************************************************************************************************
// #[macro_export] 才能在外部 crate 中使用
// ************************************************************************************************
// $fmt:expr:表示这个参数是一个 Rust 表达式(通常是一个字符串字面量或者 String 类型)。
// $($arg:tt)*:表示后续的多个参数可以是任意的 token(比如字符串字面量、变量、数字等),并且可以是多个(*)。
// 你可以将 fmt 改为任何合法的宏参数模式,如 ident、ty、path 等。具体来说:
// expr 是指 Rust 表达式,可以是计算得到的值。
// ident 是标识符,通常用于变量名或函数名。
// ty 是类型,通常用于类型相关的操作。
// path 是路径,可以是类似 std::fmt::Arguments 这样的路径。
#[macro_export]
macro_rules! printed {
// 支持格式化字符串和参数
($fmt:expr, $($arg:tt)*) => {
{
let formatted = format!($fmt, $($arg)*); // 格式化字符串
$crate::printed(&formatted);
}
};
}
#[macro_export]
macro_rules! printer {
// 支持格式化字符串和参数
($fmt:expr, $($arg:tt)*) => {
{
let formatted = format!($fmt, $($arg)*); // 格式化字符串
$crate::printer(&formatted);
}
};
}
pub struct MutBuffer {
buffer: Mutex<Vec<u8>>,
}
impl MutBuffer {
pub fn new() -> Self {
MutBuffer {
buffer: Mutex::new(Vec::new()),
}
}
pub fn push(&self, data: &[u8]) {
self.buffer.lock().unwrap().extend_from_slice(data);
}
pub fn index_of(&self, data: &[u8], start: usize) -> Option<usize> {
let buffer = self.buffer.lock().unwrap();
if start + data.len() > buffer.len() || data.is_empty() {
return None;
}
buffer
.windows(data.len())
.position(|window| window == data)
.map(|index| index + start)
}
pub fn remove_of(&self, start: usize, len: usize) {
let mut buffer = self.buffer.lock().unwrap();
if start >= buffer.len() || len == 0 {
return;
}
let end = min(start + len, buffer.len());
buffer.drain(start..end);
}
pub fn get_len(&self) -> usize {
let buffer = self.buffer.lock().unwrap();
buffer.len()
}
pub fn clear(&self) {
let mut buffer = self.buffer.lock().unwrap();
buffer.clear();
}
}
pub struct TransProtocol;
const MAX_FRAME_SIZE: usize = 1_048_576;
#[derive(Debug)]
pub struct FrameBuffer {
pub fid: String,
pub tid: String,
pub type_: FrameType,
pub data: Option<Vec<u8>>,
pub len: i32,
pub mark: char,
}
impl FrameBuffer {
pub fn new() -> Self {
FrameBuffer {
fid: String::new(),
tid: String::new(),
type_: FrameType::Unknown,
data: None,
len: 0,
mark: '\0',
}
}
}
// #[repr(i16)] // 显示指定枚举底层表示为 i16
#[derive(Debug)]
pub enum FrameType {
Unknown,
TypeA,
TypeB,
}
impl TransProtocol {
pub fn parse(buffer: &Mutex<Vec<u8>>) -> Option<FrameBuffer> {
let mut buffer = buffer.lock().unwrap();
let header = vec![0xFF, 0xFE];
let tail = vec![0xFF, 0xFF];
let cur_len = buffer.len();
if cur_len > MAX_FRAME_SIZE {
buffer.clear();
thread::sleep(Duration::new(600, 0));
return None;
}
if let Some(find) = buffer
.windows(header.len())
.position(|window| window == &header)
{
#[allow(unused_assignments)]
let mut ttype: i16 = 0;
#[allow(unused_assignments)]
let mut mark: char = '\0';
#[allow(unused_assignments)]
let mut len: i32 = 0;
ttype = i16::from_be_bytes(buffer[find + 2..find + 4].try_into().unwrap());
mark = buffer[find + 4] as char;
len = i32::from_be_bytes(buffer[find + 6..find + 10].try_into().unwrap());
let tail_index = find + 10 + len as usize;
if buffer.len() < tail_index + tail.len() || len < 0 {
return None;
}
if &buffer[tail_index..tail_index + tail.len()] != tail {
return None;
}
let mut frame = FrameBuffer::new();
if len > 0 {
frame.data = Some(vec![0; len as usize]);
}
frame.len = len;
frame.fid = String::from_utf8_lossy(&buffer[find + 4..find + 36]).to_string();
frame.tid = String::from_utf8_lossy(&buffer[find + 36..find + 68]).to_string();
frame.mark = mark;
frame.type_ = match ttype {
1 => FrameType::TypeA,
2 => FrameType::TypeB,
_ => FrameType::Unknown,
};
// 复制数据
if let Some(ref mut data) = frame.data {
data.copy_from_slice(&buffer[find + 68..find + 68 + len as usize]);
}
// 删除已解析的部分
buffer.drain(0..tail_index + 2);
return Some(frame);
}
None
}
pub fn pack(buf: &FrameBuffer) -> Option<Vec<u8>> {
if buf.data.is_none() {
return None;
}
let header = vec![0xFF, 0xFE];
let tail = vec![0xFF, 0xFF];
let len = buf.len + 75; // 包含 header 和 tail 的总长度
let mut out_buf = vec![0u8; len as usize];
// 填充数据
out_buf[0..2].copy_from_slice(&header);
let tt: i16 = match buf.type_ {
FrameType::TypeA => 1,
FrameType::TypeB => 2,
FrameType::Unknown => 0,
};
out_buf[2..4].copy_from_slice(&tt.to_le_bytes());
out_buf[4..5].copy_from_slice(&[buf.mark as u8]);
if !buf.fid.is_empty() {
out_buf[5..37].copy_from_slice(buf.fid.as_bytes());
}
if !buf.tid.is_empty() {
out_buf[37..69].copy_from_slice(buf.tid.as_bytes());
}
out_buf[69..73].copy_from_slice(&buf.len.to_be_bytes());
if let Some(ref data) = buf.data {
out_buf[73..73 + data.len()].copy_from_slice(data);
}
out_buf[len as usize - 2..len as usize].copy_from_slice(&tail);
Some(out_buf)
}
}

7
server/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "tss"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1.43.0", features = ["full"] }

3
server/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}