Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ jobs:
- name: Test
run: cargo test --target ${{ matrix.target }} --verbose ${{ matrix.profile == 'release' && '--release' || '' }}

- name: Install CMake 4
uses: step-security/get-cmake@v4
with:
cmakeVersion: "^4.0.0"

- name: Build KCP native test
run: |
cmake -S kcp -B kcp/build -DBUILD_TESTING=ON
Expand Down
2 changes: 1 addition & 1 deletion kcp
Submodule kcp updated 5 files
+62 −7 .gitignore
+1 −1 CMakeLists.txt
+2 −0 README.md
+142 −46 ikcp.c
+54 −20 ikcp.h
41 changes: 41 additions & 0 deletions src/ffi_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@ impl Kcp {
self.output_cb = Some(output_cb);
}

/// Installs a KCP 2.0 congestion control implementation.
///
/// # Safety
///
/// KCP stores the provided operations pointer and calls it from C. The caller must ensure the
/// `IKCPOPS` value outlives every `Kcp` using it, and that its callbacks preserve KCP's aliasing
/// and thread-safety requirements.
pub unsafe fn set_congestion_control(
&mut self,
ops: Option<&'static IKCPOPS>,
) -> Result<(), Error> {
let ops = ops.map_or(std::ptr::null(), |ops| ops as *const IKCPOPS);
let ret = unsafe { ikcp_setcc(self.kcp, ops) };
if ret < 0 {
Err(anyhow::anyhow!("setcc failed, return: {}", ret).into())
} else {
Ok(())
}
}

pub fn reset_congestion_control(&mut self) -> Result<(), Error> {
let ret = unsafe { ikcp_setcc(self.kcp, std::ptr::null()) };
if ret < 0 {
Err(anyhow::anyhow!("reset setcc failed, return: {}", ret).into())
} else {
Ok(())
}
}

pub fn handle_input(&mut self, data: &[u8]) -> Result<(), Error> {
let ret = unsafe { ikcp_input(self.kcp, data.as_ptr() as *const _, data.len() as _) };
if ret < 0 {
Expand Down Expand Up @@ -220,3 +249,15 @@ impl Drop for Kcp {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn reset_congestion_control_uses_builtin_algorithm() {
let mut kcp = Kcp::new(KcpConfig::new(1)).unwrap();

kcp.reset_congestion_control().unwrap();
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod ffi;
pub mod ffi;
mod state;

pub mod endpoint;
Expand Down
Loading