MCPcopy Index your code
hub / github.com/RustPython/RustPython / WSASendTo

Method WSASendTo

crates/stdlib/src/overlapped.rs:1233–1307  ·  view source on GitHub ↗
(
            zelf: &Py<Self>,
            handle: isize,
            buf: PyBuffer,
            flags: u32,
            address: PyTupleRef,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

1231 // WSASendTo
1232 #[pymethod]
1233 fn WSASendTo(
1234 zelf: &Py<Self>,
1235 handle: isize,
1236 buf: PyBuffer,
1237 flags: u32,
1238 address: PyTupleRef,
1239 vm: &VirtualMachine,
1240 ) -> PyResult {
1241 use windows_sys::Win32::Foundation::{ERROR_IO_PENDING, ERROR_SUCCESS};
1242 use windows_sys::Win32::Networking::WinSock::{WSABUF, WSAGetLastError, WSASendTo};
1243
1244 let mut inner = zelf.inner.lock();
1245 if !matches!(inner.data, OverlappedData::None) {
1246 return Err(vm.new_value_error("operation already attempted"));
1247 }
1248
1249 let (addr_bytes, addr_len) = parse_address(&address, vm)?;
1250
1251 inner.handle = handle as HANDLE;
1252 let buf_len = buf.desc.len;
1253 if buf_len > u32::MAX as usize {
1254 return Err(vm.new_value_error("buffer too large"));
1255 }
1256
1257 let Some(contiguous) = buf.as_contiguous() else {
1258 return Err(vm.new_buffer_error("buffer is not contiguous"));
1259 };
1260
1261 // Store both buffer and address in OverlappedData to keep them alive
1262 inner.data = OverlappedData::WriteTo(OverlappedWriteTo {
1263 buf: buf.clone(),
1264 address: addr_bytes,
1265 });
1266
1267 let wsabuf = WSABUF {
1268 buf: contiguous.as_ptr() as *mut _,
1269 len: buf_len as u32,
1270 };
1271 let mut written: u32 = 0;
1272
1273 // Get pointer to the stored address data
1274 let addr_ptr = match &inner.data {
1275 OverlappedData::WriteTo(wt) => wt.address.as_ptr(),
1276 _ => unreachable!(),
1277 };
1278
1279 let ret = unsafe {
1280 WSASendTo(
1281 handle as _,
1282 &wsabuf,
1283 1,
1284 &mut written,
1285 flags,
1286 addr_ptr as *const SOCKADDR,
1287 addr_len,
1288 &mut inner.overlapped,
1289 None,
1290 )

Callers 1

sendtoMethod · 0.95

Calls 8

parse_addressFunction · 0.85
set_from_windows_errFunction · 0.85
noneMethod · 0.80
ErrClass · 0.50
lockMethod · 0.45
as_contiguousMethod · 0.45
cloneMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected