Skip to content
Draft
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
78 changes: 78 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ compio = { version = "0.19.0-rc.1", default-features = false }
compio-executor = "0.1.0-rc.1"
compio-log = "0.1.0"

[workspace.dependencies."compio-ktls"]
git = "https://github.com/compio-rs/compio-ktls.git"
branch = "alpn"

[patch.crates-io]
compio-py-dynamic-openssl = { path = "compio-py-dynamic-openssl" }
5 changes: 4 additions & 1 deletion compio-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ socket2 = "0.6.2"
rustls-webpki = "0"
pem = "3.0.6"
pkcs8 = { version = "0.10.2", features = ["encryption"] }
compio = { workspace = true, features = ["io-uring", "polling", "io", "py-dynamic-openssl", "rustls", "ring"] }
compio = { workspace = true, features = ["io-uring", "polling", "io", "io-ancillary", "py-dynamic-openssl", "rustls", "ring"] }
compio-executor = { workspace = true }
compio-log = { workspace = true }

[target."cfg(target_os = \"linux\")".dependencies]
compio-ktls = { workspace = true }
41 changes: 41 additions & 0 deletions compio-py/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,27 @@ use crate::{

static COMPIO_FUTURE: OnceCell<Py<PyAny>> = OnceCell::new();

#[derive(Debug, Clone, Copy, Default)]
pub enum KtlsMode {
#[default]
Prefer,
Require,
Disabled,
}

impl KtlsMode {
pub fn enabled(self) -> bool {
matches!(self, KtlsMode::Prefer | KtlsMode::Require)
}
}

#[pyclass(subclass)]
pub struct CompioLoop {
runtime: OwnedRefCell<Runtime>,
stopping: Arc<AtomicBool>,
debug: Arc<AtomicBool>,
registered: Py<PyMapping>,
ktls_mode: KtlsMode,
}

#[pymethods]
Expand All @@ -45,6 +60,7 @@ impl CompioLoop {
stopping: Default::default(),
debug: Default::default(),
registered: import::weakref::weak_key_dict(py)?.cast_into()?.unbind(),
ktls_mode: KtlsMode::default(),
})
}

Expand Down Expand Up @@ -268,6 +284,27 @@ impl CompioLoop {
slf.borrow()
.spawn_py(py, net::PySocket::new(pyloop, domain, ty, protocol))
}

#[getter]
fn ktls_mode(&self) -> String {
format!("{:?}", self.ktls_mode)
}

#[setter]
fn set_ktls_mode(&mut self, mode: &Bound<PyAny>) -> PyResult<()> {
self.ktls_mode = match mode.str()?.to_str()? {
"Prefer" => KtlsMode::Prefer,
"Require" => KtlsMode::Require,
"Disabled" => KtlsMode::Disabled,
other => {
return Err(PyValueError::new_err(format!(
"unknown KTLS mode: {}",
other
)));
}
};
Ok(())
}
}

impl CompioLoop {
Expand Down Expand Up @@ -332,6 +369,10 @@ impl CompioLoop {
Ok(rv)
}

pub fn get_ktls_mode(&self) -> KtlsMode {
self.ktls_mode
}

fn socket_to_fd(&self, py: Python, sock: &Py<PyAny>) -> PyResult<SharedFd<BorrowedFd>> {
let registered = self.registered.bind(py);
let sock = sock.bind(py);
Expand Down
9 changes: 9 additions & 0 deletions compio-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ enum Either<L, R> {
Right(R),
}

impl<L, R> Either<L, R> {
fn into<T: From<L> + From<R>>(self) -> T {
match self {
Self::Left(l) => T::from(l),
Self::Right(r) => T::from(r),
}
}
}

impl<L: IoBuf, R: IoBuf> IoBuf for Either<L, R> {
fn as_init(&self) -> &[u8] {
match self {
Expand Down
Loading
Loading