|
| 1 | +#[allow(unused_imports)] |
| 2 | +use crate::obj::objbyteinner::PyBytesLike; |
| 3 | +#[allow(unused_imports)] |
| 4 | +use crate::pyobject::{PyObjectRef, PyResult}; |
| 5 | +use crate::VirtualMachine; |
| 6 | + |
| 7 | +#[cfg(windows)] |
| 8 | +use winapi::um::winsock2::{self, SOCKET}; |
| 9 | + |
| 10 | +#[cfg(windows)] |
| 11 | +fn multiprocessing_closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> { |
| 12 | + let res = unsafe { winsock2::closesocket(socket as SOCKET) }; |
| 13 | + if res == 0 { |
| 14 | + Err(super::os::convert_io_error( |
| 15 | + vm, |
| 16 | + std::io::Error::last_os_error(), |
| 17 | + )) |
| 18 | + } else { |
| 19 | + Ok(()) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +#[cfg(windows)] |
| 24 | +fn multiprocessing_recv(socket: usize, size: usize, vm: &VirtualMachine) -> PyResult<libc::c_int> { |
| 25 | + let mut buf = vec![0 as libc::c_char; size]; |
| 26 | + let nread = |
| 27 | + unsafe { winsock2::recv(socket as SOCKET, buf.as_mut_ptr() as *mut _, size as i32, 0) }; |
| 28 | + if nread < 0 { |
| 29 | + Err(super::os::convert_io_error( |
| 30 | + vm, |
| 31 | + std::io::Error::last_os_error(), |
| 32 | + )) |
| 33 | + } else { |
| 34 | + Ok(nread) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(windows)] |
| 39 | +fn multiprocessing_send( |
| 40 | + socket: usize, |
| 41 | + buf: PyBytesLike, |
| 42 | + vm: &VirtualMachine, |
| 43 | +) -> PyResult<libc::c_int> { |
| 44 | + let buf = buf.to_cow(); |
| 45 | + let ret = unsafe { |
| 46 | + winsock2::send( |
| 47 | + socket as SOCKET, |
| 48 | + buf.as_ptr() as *const _, |
| 49 | + buf.len() as i32, |
| 50 | + 0, |
| 51 | + ) |
| 52 | + }; |
| 53 | + if ret < 0 { |
| 54 | + Err(super::os::convert_io_error( |
| 55 | + vm, |
| 56 | + std::io::Error::last_os_error(), |
| 57 | + )) |
| 58 | + } else { |
| 59 | + Ok(ret) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { |
| 64 | + let module = py_module!(vm, "_multiprocessing", {}); |
| 65 | + extend_module_platform_specific(vm, &module); |
| 66 | + module |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(windows)] |
| 70 | +fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) { |
| 71 | + let ctx = &vm.ctx; |
| 72 | + extend_module!(vm, module, { |
| 73 | + "closesocket" => ctx.new_rustfunc(multiprocessing_closesocket), |
| 74 | + "recv" => ctx.new_rustfunc(multiprocessing_recv), |
| 75 | + "send" => ctx.new_rustfunc(multiprocessing_send), |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(not(windows))] |
| 80 | +fn extend_module_platform_specific(_vm: &VirtualMachine, _module: &PyObjectRef) {} |
0 commit comments