refactor: migrate net operation to KCP

This commit is contained in:
amizing25
2024-10-23 04:49:46 +07:00
parent c4794570d1
commit f77bcb04f7
13 changed files with 1946 additions and 81 deletions
+20
View File
@@ -0,0 +1,20 @@
[package]
name = "mhy-kcp"
version.workspace = true
edition = "2021"
[features]
fastack-conserve = []
tokio = ["dep:tokio"]
[dependencies]
bytes = "1.6.0"
log = "0.4.21"
thiserror = "1.0.58"
tokio = { version = "1.37.0", optional = true, features = ["io-util"] }
[dev-dependencies]
time = "0.3.34"
rand = "0.8.5"
env_logger = "0.11.3"
+56
View File
@@ -0,0 +1,56 @@
use std::{
error::Error as StdError,
io::{self, ErrorKind},
};
/// KCP protocol errors
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("conv inconsistent, expected {0}, found {1}")]
ConvInconsistent(u32, u32),
#[error("invalid mtu {0}")]
InvalidMtu(usize),
#[error("invalid segment size {0}")]
InvalidSegmentSize(usize),
#[error("invalid segment data size, expected {0}, found {1}")]
InvalidSegmentDataSize(usize, usize),
#[error("{0}")]
IoError(
#[from]
#[source]
io::Error,
),
#[error("need to call update() once")]
NeedUpdate,
#[error("recv queue is empty")]
RecvQueueEmpty,
#[error("expecting fragment")]
ExpectingFragment,
#[error("command {0} is not supported")]
UnsupportedCmd(u8),
#[error("user's send buffer is too big")]
UserBufTooBig,
#[error("user's recv buffer is too small")]
UserBufTooSmall,
#[error("token mismatch, expected {0}, found {1}")]
TokenMismatch(u32, u32),
}
fn make_io_error<T>(kind: ErrorKind, msg: T) -> io::Error
where
T: Into<Box<dyn StdError + Send + Sync>>,
{
io::Error::new(kind, msg)
}
impl From<Error> for io::Error {
fn from(err: Error) -> Self {
let kind = match err {
Error::IoError(err) => return err,
Error::RecvQueueEmpty | Error::ExpectingFragment => ErrorKind::WouldBlock,
_ => ErrorKind::Other,
};
make_io_error(kind, err)
}
}
+1523
View File
File diff suppressed because it is too large Load Diff
+17
View File
@@ -0,0 +1,17 @@
extern crate bytes;
#[macro_use]
extern crate log;
mod error;
mod kcp;
/// The `KCP` prelude
pub mod prelude {
pub use super::{get_conv, Kcp, KCP_OVERHEAD};
}
pub use error::Error;
pub use kcp::{get_conv, get_sn, set_conv, Kcp, KCP_OVERHEAD};
/// KCP result
pub type KcpResult<T> = Result<T, Error>;