Initial commit
This commit is contained in:
commit
59fa0f4892
4 changed files with 1594 additions and 0 deletions
18
Cargo.toml
Normal file
18
Cargo.toml
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
[package]
|
||||||
|
name = "kcp"
|
||||||
|
edition = "2021"
|
||||||
|
version.workspace = true
|
||||||
|
|
||||||
|
[features]
|
||||||
|
fastack-conserve = []
|
||||||
|
tokio = ["dep:tokio"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
bytes = "1.7.2"
|
||||||
|
log = "0.4.22"
|
||||||
|
thiserror = "1.0.64"
|
||||||
|
tokio = { version = "1.40.0", optional = true, features = ["io-util"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
time = "0.3.36"
|
||||||
|
rand = "0.8.5"
|
54
src/error.rs
Normal file
54
src/error.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
1505
src/kcp.rs
Normal file
1505
src/kcp.rs
Normal file
File diff suppressed because it is too large
Load diff
17
src/lib.rs
Normal file
17
src/lib.rs
Normal 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>;
|
Loading…
Reference in a new issue