forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiprocessing.rs
More file actions
46 lines (41 loc) · 1.27 KB
/
multiprocessing.rs
File metadata and controls
46 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
pub(crate) use _multiprocessing::make_module;
#[cfg(windows)]
#[pymodule]
mod _multiprocessing {
use crate::vm::{function::ArgBytesLike, stdlib::os, PyResult, VirtualMachine};
use winapi::um::winsock2::{self, SOCKET};
#[pyfunction]
fn closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> {
let res = unsafe { winsock2::closesocket(socket as SOCKET) };
if res == 0 {
Err(os::errno_err(vm))
} else {
Ok(())
}
}
#[pyfunction]
fn recv(socket: usize, size: usize, vm: &VirtualMachine) -> PyResult<libc::c_int> {
let mut buf = vec![0; size];
let nread =
unsafe { winsock2::recv(socket as SOCKET, buf.as_mut_ptr() as *mut _, size as i32, 0) };
if nread < 0 {
Err(os::errno_err(vm))
} else {
Ok(nread)
}
}
#[pyfunction]
fn send(socket: usize, buf: ArgBytesLike, vm: &VirtualMachine) -> PyResult<libc::c_int> {
let ret = buf.with_ref(|b| unsafe {
winsock2::send(socket as SOCKET, b.as_ptr() as *const _, b.len() as i32, 0)
});
if ret < 0 {
Err(os::errno_err(vm))
} else {
Ok(ret)
}
}
}
#[cfg(not(windows))]
#[pymodule]
mod _multiprocessing {}