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
8 changes: 4 additions & 4 deletions compio-py-dynamic-openssl/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,12 @@ mod error {
pub struct ErrorCode(c_int);

impl ErrorCode {
pub const ZERO_RETURN: ErrorCode = ErrorCode(ffi::SSL_ERROR_ZERO_RETURN);
pub const WANT_READ: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_READ);
pub const WANT_WRITE: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_WRITE);
pub const SYSCALL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SYSCALL);
pub const SSL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SSL);
pub const SYSCALL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SYSCALL);
pub const WANT_CLIENT_HELLO_CB: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_CLIENT_HELLO_CB);
pub const WANT_READ: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_READ);
pub const WANT_WRITE: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_WRITE);
pub const ZERO_RETURN: ErrorCode = ErrorCode(ffi::SSL_ERROR_ZERO_RETURN);

pub fn from_raw(raw: c_int) -> ErrorCode {
ErrorCode(raw)
Expand Down
13 changes: 6 additions & 7 deletions compio-py/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ use once_cell::sync::OnceCell;
use pyo3::{
IntoPyObjectExt,
buffer::PyBuffer,
exceptions::PyRuntimeError,
exceptions::PyValueError,
exceptions::{PyRuntimeError, PyValueError},
ffi::c_str,
prelude::*,
types::{PyDict, PyMapping, PyTuple, PyWeakrefReference},
};

use crate::{
handle::{Handle, TimerHandle},
import,
import, net,
owned::{self, OwnedRefCell},
runtime::{self, Runtime},
socket,
};

static COMPIO_FUTURE: OnceCell<Py<PyAny>> = OnceCell::new();
Expand Down Expand Up @@ -268,7 +266,7 @@ impl CompioLoop {
};
let pyloop = slf.clone().unbind();
slf.borrow()
.spawn_py(py, socket::PySocket::new(pyloop, domain, ty, protocol))
.spawn_py(py, net::PySocket::new(pyloop, domain, ty, protocol))
}
}

Expand All @@ -284,8 +282,9 @@ impl CompioLoop {
}
}

/// Spawn a Rust Future onto the event loop, returning a connected Python Future.
/// When the Python Future is cancelled, the Rust Future is also cancelled.
/// Spawn a Rust Future onto the event loop, returning a connected Python
/// Future. When the Python Future is cancelled, the Rust Future is also
/// cancelled.
pub fn spawn_py<'py, F>(&self, py: Python<'py>, fut: F) -> PyResult<Bound<'py, PyAny>>
where
F: Future<Output = PyResult<Py<PyAny>>> + 'static,
Expand Down
37 changes: 21 additions & 16 deletions compio-py/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@

//! Python module import utilities with caching.
//!
//! This module provides macros and functions for efficiently importing Python modules
//! and accessing their attributes with automatic caching using `OnceCell`.
//! This module provides macros and functions for efficiently importing Python
//! modules and accessing their attributes with automatic caching using
//! `OnceCell`.
//!
//! # Thread Safety
//!
//! All cached imports and attributes use `OnceCell::sync`, making them safe to access
//! from multiple threads. The first thread to access will perform the import/getattr,
//! and subsequent accesses will use the cached value.
//! All cached imports and attributes use `OnceCell::sync`, making them safe to
//! access from multiple threads. The first thread to access will perform the
//! import/getattr, and subsequent accesses will use the cached value.
//!
//! # Performance
//!
//! By caching module imports and attribute lookups, this module avoids the overhead
//! of repeated Python import statements and attribute resolution, which can be
//! significant in performance-critical code paths.
//! By caching module imports and attribute lookups, this module avoids the
//! overhead of repeated Python import statements and attribute resolution,
//! which can be significant in performance-critical code paths.
//!
//! # Example
//!
Expand All @@ -34,8 +35,9 @@ use pyo3::prelude::*;

/// Generates a function that imports a Python module with caching.
///
/// This macro creates a public function that imports the specified Python module
/// on first call and caches it for subsequent calls using a static `OnceCell`.
/// This macro creates a public function that imports the specified Python
/// module on first call and caches it for subsequent calls using a static
/// `OnceCell`.
///
/// # Generated Function Signature
///
Expand All @@ -45,7 +47,8 @@ use pyo3::prelude::*;
///
/// # Arguments
///
/// * `$module_name` - The name of the Python module to import (as an identifier)
/// * `$module_name` - The name of the Python module to import (as an
/// identifier)
///
/// # Example
///
Expand All @@ -60,8 +63,8 @@ use pyo3::prelude::*;
///
/// # Thread Safety
///
/// The generated function is thread-safe. Multiple threads can call it concurrently,
/// and only the first call will perform the actual import.
/// The generated function is thread-safe. Multiple threads can call it
/// concurrently, and only the first call will perform the actual import.
macro_rules! import {
($module_name:ident) => {
pub fn $module_name<'py>(py: Python<'py>) -> PyResult<&'py Bound<'py, PyModule>> {
Expand Down Expand Up @@ -105,7 +108,8 @@ macro_rules! import {
///
/// # Thread Safety
///
/// Like `import!`, this macro generates thread-safe code using static `OnceCell`.
/// Like `import!`, this macro generates thread-safe code using static
/// `OnceCell`.
///
/// # Note
///
Expand All @@ -121,8 +125,9 @@ macro_rules! getattr {

/// Creates a module wrapper with standard imports and boilerplate.
///
/// This macro combines the `import!` macro with a module definition that includes
/// the standard `use` statements needed for working with Python objects.
/// This macro combines the `import!` macro with a module definition that
/// includes the standard `use` statements needed for working with Python
/// objects.
///
/// # Arguments
///
Expand Down
6 changes: 2 additions & 4 deletions compio-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use pyo3::{buffer::PyBuffer, prelude::*};
mod event_loop;
mod handle;
mod import;
mod net;
mod owned;
mod runtime;
mod send_wrapper;
mod socket;
mod ssl;
mod thread;

/// A Python module implemented in Rust. The name of this module must match
Expand All @@ -38,8 +37,7 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> {

event_loop::register(m)?;
handle::register(m)?;
socket::register(m)?;
ssl::register(m)?;
net::register(m)?;
Ok(())
}

Expand Down
209 changes: 209 additions & 0 deletions compio-py/src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// SPDX-License-Identifier: Apache-2.0 OR MulanPSL-2.0
// Copyright 2026 Fantix King

use std::{io, net::Shutdown};

use compio::{
buf::{BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut, buf_try},
driver::{
ToSharedFd, impl_raw_fd,
op::{self, BufResultExt},
},
io::{AsyncRead, AsyncWrite},
};
use pyo3::{
exceptions::PyTypeError,
prelude::*,
types::{PyByteArray, PyBytes, PyList, PyTuple},
};
use socket2::{Domain, Protocol, SockAddr, Socket as Socket2, Type};

pub use self::socket::PySocket;
use crate::{
import,
runtime::{self, Attacher},
};

mod socket;
mod ssl;

#[derive(Debug, Clone)]
pub struct SocketStream {
inner: Socket,
}

impl AsyncRead for &SocketStream {
#[inline]
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
self.inner.recv(buf, 0).await
}
}

impl AsyncWrite for &SocketStream {
async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
self.inner.send(buf, 0).await
}

async fn flush(&mut self) -> io::Result<()> {
Ok(())
}

async fn shutdown(&mut self) -> io::Result<()> {
self.inner.shutdown(Shutdown::Write).await
}
}

impl AsyncRead for SocketStream {
#[inline]
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
(&*self).read(buf).await
}

#[inline]
async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
(&*self).read_vectored(buf).await
}
}

impl AsyncWrite for SocketStream {
async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
(&*self).write(buf).await
}

async fn flush(&mut self) -> io::Result<()> {
(&*self).flush().await
}

async fn shutdown(&mut self) -> io::Result<()> {
(&*self).shutdown().await
}
}

impl_raw_fd!(SocketStream, socket2::Socket, inner, socket);

async fn name_to_ip(name: Vec<u8>, family: impl Into<i32>) -> PyResult<String> {
let family = family.into();
runtime::asyncify(move || {
Python::attach(|py| {
let args = (name, py.None(), family);
let result_list: Bound<PyList> =
import::socket::getaddrinfo(py, args, None)?.cast_into()?;
let result: Bound<PyTuple> = result_list.get_item(0)?.cast_into()?;
let addr: Bound<PyTuple> = result.get_item(result.len() - 1)?.cast_into()?;
addr.get_item(0)?.extract()
})
})
.await
}

fn idna_converter<T, F>(obj: &Bound<PyAny>, f: F) -> PyResult<T>
where
F: FnOnce(&[u8]) -> PyResult<T>,
{
if let Ok(bytes) = obj.cast::<PyBytes>() {
f(bytes.as_bytes())
} else if let Ok(bytes) = obj.cast::<PyByteArray>() {
f(unsafe { bytes.as_bytes() })
} else if let Ok(str) = obj.extract::<&str>() {
if str.is_ascii() {
f(str.as_bytes())
} else {
f(obj
.call_method1("encode", ("idna",))?
.cast::<PyBytes>()?
.as_bytes())
}
} else {
Err(PyTypeError::new_err(format!(
"str, bytes or bytearray expected, not {}",
obj.get_type()
)))
}
}

pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
socket::register(m)?;
ssl::register(m)?;
Ok(())
}

// Socket is copied and modified from compio-net:
// https://github.com/compio-rs/compio/blob/8cf1b4db78c93f37d462b85bd1f445caa7fca4d5/compio-net/src/socket.rs
// Copyright (c) 2023 Berrysoft

#[derive(Debug, Clone)]
struct Socket {
socket: Attacher<Socket2>,
}

impl Socket {
fn from_socket2(socket: Socket2) -> io::Result<Self> {
let socket = Attacher::new(socket)?;
Ok(Self { socket })
}

async fn new(domain: Domain, ty: Type, protocol: Option<Protocol>) -> io::Result<Self> {
Self::from_socket2({
#[cfg(windows)]
{
runtime::asyncify(move || Socket2::new(domain, ty, protocol)).await?
}
#[cfg(unix)]
{
use compio::driver::op::CreateSocket;

let op = CreateSocket::new(
domain.into(),
ty.into(),
protocol.map(|p| p.into()).unwrap_or_default(),
);
let (_, op) = buf_try!(@try runtime::execute(op).await);
op.into_inner()
}
})
}

async fn connect_async(&self, addr: &SockAddr) -> io::Result<()> {
let op = op::Connect::new(self.to_shared_fd(), addr.clone());
let (_, _op) = buf_try!(@try runtime::execute(op).await);
#[cfg(windows)]
_op.update_context()?;
Ok(())
}

async fn recv<B: IoBufMut>(&self, buffer: B, flags: i32) -> BufResult<usize, B> {
let fd = self.to_shared_fd();
let op = op::Recv::new(fd, buffer, flags);
let res = runtime::execute(op).await.into_inner();
unsafe { res.map_advanced() }
}

async fn send<T: IoBuf>(&self, buffer: T, flags: i32) -> BufResult<usize, T> {
let fd = self.to_shared_fd();
let op = op::Send::new(fd, buffer, flags);
runtime::execute(op).await.into_inner()
}

async fn shutdown(&self, how: Shutdown) -> io::Result<()> {
#[cfg(unix)]
{
let fd = self.to_shared_fd();
let op = op::ShutdownSocket::new(fd, how);
runtime::execute(op).await.0?;
Ok(())
}
#[cfg(windows)]
self.socket.shutdown(how)
}

async fn close(self) -> io::Result<()> {
let fd = self.socket.into_inner().take().await;
if let Some(fd) = fd {
let op = op::CloseSocket::new(fd.into());
runtime::execute(op).await.0?;
}
Ok(())
}
}

impl_raw_fd!(Socket, Socket2, socket, socket);
Loading
Loading