1 | n/a | /* |
---|
2 | n/a | * Support for overlapped IO |
---|
3 | n/a | * |
---|
4 | n/a | * Some code borrowed from Modules/_winapi.c of CPython |
---|
5 | n/a | */ |
---|
6 | n/a | |
---|
7 | n/a | /* XXX check overflow and DWORD <-> Py_ssize_t conversions |
---|
8 | n/a | Check itemsize */ |
---|
9 | n/a | |
---|
10 | n/a | #include "Python.h" |
---|
11 | n/a | #include "structmember.h" |
---|
12 | n/a | |
---|
13 | n/a | #define WINDOWS_LEAN_AND_MEAN |
---|
14 | n/a | #include <winsock2.h> |
---|
15 | n/a | #include <ws2tcpip.h> |
---|
16 | n/a | #include <mswsock.h> |
---|
17 | n/a | |
---|
18 | n/a | #if defined(MS_WIN32) && !defined(MS_WIN64) |
---|
19 | n/a | # define F_POINTER "k" |
---|
20 | n/a | # define T_POINTER T_ULONG |
---|
21 | n/a | #else |
---|
22 | n/a | # define F_POINTER "K" |
---|
23 | n/a | # define T_POINTER T_ULONGLONG |
---|
24 | n/a | #endif |
---|
25 | n/a | |
---|
26 | n/a | /* Compatibility with Python 3.3 */ |
---|
27 | n/a | #if PY_VERSION_HEX < 0x03040000 |
---|
28 | n/a | # define PyMem_RawMalloc PyMem_Malloc |
---|
29 | n/a | # define PyMem_RawFree PyMem_Free |
---|
30 | n/a | #endif |
---|
31 | n/a | |
---|
32 | n/a | #define F_HANDLE F_POINTER |
---|
33 | n/a | #define F_ULONG_PTR F_POINTER |
---|
34 | n/a | #define F_DWORD "k" |
---|
35 | n/a | #define F_BOOL "i" |
---|
36 | n/a | #define F_UINT "I" |
---|
37 | n/a | |
---|
38 | n/a | #define T_HANDLE T_POINTER |
---|
39 | n/a | |
---|
40 | n/a | enum {TYPE_NONE, TYPE_NOT_STARTED, TYPE_READ, TYPE_WRITE, TYPE_ACCEPT, |
---|
41 | n/a | TYPE_CONNECT, TYPE_DISCONNECT, TYPE_CONNECT_NAMED_PIPE, |
---|
42 | n/a | TYPE_WAIT_NAMED_PIPE_AND_CONNECT}; |
---|
43 | n/a | |
---|
44 | n/a | typedef struct { |
---|
45 | n/a | PyObject_HEAD |
---|
46 | n/a | OVERLAPPED overlapped; |
---|
47 | n/a | /* For convenience, we store the file handle too */ |
---|
48 | n/a | HANDLE handle; |
---|
49 | n/a | /* Error returned by last method call */ |
---|
50 | n/a | DWORD error; |
---|
51 | n/a | /* Type of operation */ |
---|
52 | n/a | DWORD type; |
---|
53 | n/a | union { |
---|
54 | n/a | /* Buffer used for reading: TYPE_READ and TYPE_ACCEPT */ |
---|
55 | n/a | PyObject *read_buffer; |
---|
56 | n/a | /* Buffer used for writing: TYPE_WRITE */ |
---|
57 | n/a | Py_buffer write_buffer; |
---|
58 | n/a | }; |
---|
59 | n/a | } OverlappedObject; |
---|
60 | n/a | |
---|
61 | n/a | /* |
---|
62 | n/a | * Map Windows error codes to subclasses of OSError |
---|
63 | n/a | */ |
---|
64 | n/a | |
---|
65 | n/a | static PyObject * |
---|
66 | n/a | SetFromWindowsErr(DWORD err) |
---|
67 | n/a | { |
---|
68 | n/a | PyObject *exception_type; |
---|
69 | n/a | |
---|
70 | n/a | if (err == 0) |
---|
71 | n/a | err = GetLastError(); |
---|
72 | n/a | switch (err) { |
---|
73 | n/a | case ERROR_CONNECTION_REFUSED: |
---|
74 | n/a | exception_type = PyExc_ConnectionRefusedError; |
---|
75 | n/a | break; |
---|
76 | n/a | case ERROR_CONNECTION_ABORTED: |
---|
77 | n/a | exception_type = PyExc_ConnectionAbortedError; |
---|
78 | n/a | break; |
---|
79 | n/a | default: |
---|
80 | n/a | exception_type = PyExc_OSError; |
---|
81 | n/a | } |
---|
82 | n/a | return PyErr_SetExcFromWindowsErr(exception_type, err); |
---|
83 | n/a | } |
---|
84 | n/a | |
---|
85 | n/a | /* |
---|
86 | n/a | * Some functions should be loaded at runtime |
---|
87 | n/a | */ |
---|
88 | n/a | |
---|
89 | n/a | static LPFN_ACCEPTEX Py_AcceptEx = NULL; |
---|
90 | n/a | static LPFN_CONNECTEX Py_ConnectEx = NULL; |
---|
91 | n/a | static LPFN_DISCONNECTEX Py_DisconnectEx = NULL; |
---|
92 | n/a | static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED) = NULL; |
---|
93 | n/a | |
---|
94 | n/a | #define GET_WSA_POINTER(s, x) \ |
---|
95 | n/a | (SOCKET_ERROR != WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, \ |
---|
96 | n/a | &Guid##x, sizeof(Guid##x), &Py_##x, \ |
---|
97 | n/a | sizeof(Py_##x), &dwBytes, NULL, NULL)) |
---|
98 | n/a | |
---|
99 | n/a | static int |
---|
100 | n/a | initialize_function_pointers(void) |
---|
101 | n/a | { |
---|
102 | n/a | GUID GuidAcceptEx = WSAID_ACCEPTEX; |
---|
103 | n/a | GUID GuidConnectEx = WSAID_CONNECTEX; |
---|
104 | n/a | GUID GuidDisconnectEx = WSAID_DISCONNECTEX; |
---|
105 | n/a | HINSTANCE hKernel32; |
---|
106 | n/a | SOCKET s; |
---|
107 | n/a | DWORD dwBytes; |
---|
108 | n/a | |
---|
109 | n/a | s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
---|
110 | n/a | if (s == INVALID_SOCKET) { |
---|
111 | n/a | SetFromWindowsErr(WSAGetLastError()); |
---|
112 | n/a | return -1; |
---|
113 | n/a | } |
---|
114 | n/a | |
---|
115 | n/a | if (!GET_WSA_POINTER(s, AcceptEx) || |
---|
116 | n/a | !GET_WSA_POINTER(s, ConnectEx) || |
---|
117 | n/a | !GET_WSA_POINTER(s, DisconnectEx)) |
---|
118 | n/a | { |
---|
119 | n/a | closesocket(s); |
---|
120 | n/a | SetFromWindowsErr(WSAGetLastError()); |
---|
121 | n/a | return -1; |
---|
122 | n/a | } |
---|
123 | n/a | |
---|
124 | n/a | closesocket(s); |
---|
125 | n/a | |
---|
126 | n/a | /* On WinXP we will have Py_CancelIoEx == NULL */ |
---|
127 | n/a | hKernel32 = GetModuleHandle("KERNEL32"); |
---|
128 | n/a | *(FARPROC *)&Py_CancelIoEx = GetProcAddress(hKernel32, "CancelIoEx"); |
---|
129 | n/a | return 0; |
---|
130 | n/a | } |
---|
131 | n/a | |
---|
132 | n/a | /* |
---|
133 | n/a | * Completion port stuff |
---|
134 | n/a | */ |
---|
135 | n/a | |
---|
136 | n/a | PyDoc_STRVAR( |
---|
137 | n/a | CreateIoCompletionPort_doc, |
---|
138 | n/a | "CreateIoCompletionPort(handle, port, key, concurrency) -> port\n\n" |
---|
139 | n/a | "Create a completion port or register a handle with a port."); |
---|
140 | n/a | |
---|
141 | n/a | static PyObject * |
---|
142 | n/a | overlapped_CreateIoCompletionPort(PyObject *self, PyObject *args) |
---|
143 | n/a | { |
---|
144 | n/a | HANDLE FileHandle; |
---|
145 | n/a | HANDLE ExistingCompletionPort; |
---|
146 | n/a | ULONG_PTR CompletionKey; |
---|
147 | n/a | DWORD NumberOfConcurrentThreads; |
---|
148 | n/a | HANDLE ret; |
---|
149 | n/a | |
---|
150 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE F_ULONG_PTR F_DWORD, |
---|
151 | n/a | &FileHandle, &ExistingCompletionPort, &CompletionKey, |
---|
152 | n/a | &NumberOfConcurrentThreads)) |
---|
153 | n/a | return NULL; |
---|
154 | n/a | |
---|
155 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
156 | n/a | ret = CreateIoCompletionPort(FileHandle, ExistingCompletionPort, |
---|
157 | n/a | CompletionKey, NumberOfConcurrentThreads); |
---|
158 | n/a | Py_END_ALLOW_THREADS |
---|
159 | n/a | |
---|
160 | n/a | if (ret == NULL) |
---|
161 | n/a | return SetFromWindowsErr(0); |
---|
162 | n/a | return Py_BuildValue(F_HANDLE, ret); |
---|
163 | n/a | } |
---|
164 | n/a | |
---|
165 | n/a | PyDoc_STRVAR( |
---|
166 | n/a | GetQueuedCompletionStatus_doc, |
---|
167 | n/a | "GetQueuedCompletionStatus(port, msecs) -> (err, bytes, key, address)\n\n" |
---|
168 | n/a | "Get a message from completion port. Wait for up to msecs milliseconds."); |
---|
169 | n/a | |
---|
170 | n/a | static PyObject * |
---|
171 | n/a | overlapped_GetQueuedCompletionStatus(PyObject *self, PyObject *args) |
---|
172 | n/a | { |
---|
173 | n/a | HANDLE CompletionPort = NULL; |
---|
174 | n/a | DWORD NumberOfBytes = 0; |
---|
175 | n/a | ULONG_PTR CompletionKey = 0; |
---|
176 | n/a | OVERLAPPED *Overlapped = NULL; |
---|
177 | n/a | DWORD Milliseconds; |
---|
178 | n/a | DWORD err; |
---|
179 | n/a | BOOL ret; |
---|
180 | n/a | |
---|
181 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_DWORD, |
---|
182 | n/a | &CompletionPort, &Milliseconds)) |
---|
183 | n/a | return NULL; |
---|
184 | n/a | |
---|
185 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
186 | n/a | ret = GetQueuedCompletionStatus(CompletionPort, &NumberOfBytes, |
---|
187 | n/a | &CompletionKey, &Overlapped, Milliseconds); |
---|
188 | n/a | Py_END_ALLOW_THREADS |
---|
189 | n/a | |
---|
190 | n/a | err = ret ? ERROR_SUCCESS : GetLastError(); |
---|
191 | n/a | if (Overlapped == NULL) { |
---|
192 | n/a | if (err == WAIT_TIMEOUT) |
---|
193 | n/a | Py_RETURN_NONE; |
---|
194 | n/a | else |
---|
195 | n/a | return SetFromWindowsErr(err); |
---|
196 | n/a | } |
---|
197 | n/a | return Py_BuildValue(F_DWORD F_DWORD F_ULONG_PTR F_POINTER, |
---|
198 | n/a | err, NumberOfBytes, CompletionKey, Overlapped); |
---|
199 | n/a | } |
---|
200 | n/a | |
---|
201 | n/a | PyDoc_STRVAR( |
---|
202 | n/a | PostQueuedCompletionStatus_doc, |
---|
203 | n/a | "PostQueuedCompletionStatus(port, bytes, key, address) -> None\n\n" |
---|
204 | n/a | "Post a message to completion port."); |
---|
205 | n/a | |
---|
206 | n/a | static PyObject * |
---|
207 | n/a | overlapped_PostQueuedCompletionStatus(PyObject *self, PyObject *args) |
---|
208 | n/a | { |
---|
209 | n/a | HANDLE CompletionPort; |
---|
210 | n/a | DWORD NumberOfBytes; |
---|
211 | n/a | ULONG_PTR CompletionKey; |
---|
212 | n/a | OVERLAPPED *Overlapped; |
---|
213 | n/a | BOOL ret; |
---|
214 | n/a | |
---|
215 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_DWORD F_ULONG_PTR F_POINTER, |
---|
216 | n/a | &CompletionPort, &NumberOfBytes, &CompletionKey, |
---|
217 | n/a | &Overlapped)) |
---|
218 | n/a | return NULL; |
---|
219 | n/a | |
---|
220 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
221 | n/a | ret = PostQueuedCompletionStatus(CompletionPort, NumberOfBytes, |
---|
222 | n/a | CompletionKey, Overlapped); |
---|
223 | n/a | Py_END_ALLOW_THREADS |
---|
224 | n/a | |
---|
225 | n/a | if (!ret) |
---|
226 | n/a | return SetFromWindowsErr(0); |
---|
227 | n/a | Py_RETURN_NONE; |
---|
228 | n/a | } |
---|
229 | n/a | |
---|
230 | n/a | /* |
---|
231 | n/a | * Wait for a handle |
---|
232 | n/a | */ |
---|
233 | n/a | |
---|
234 | n/a | struct PostCallbackData { |
---|
235 | n/a | HANDLE CompletionPort; |
---|
236 | n/a | LPOVERLAPPED Overlapped; |
---|
237 | n/a | }; |
---|
238 | n/a | |
---|
239 | n/a | static VOID CALLBACK |
---|
240 | n/a | PostToQueueCallback(PVOID lpParameter, BOOL TimerOrWaitFired) |
---|
241 | n/a | { |
---|
242 | n/a | struct PostCallbackData *p = (struct PostCallbackData*) lpParameter; |
---|
243 | n/a | |
---|
244 | n/a | PostQueuedCompletionStatus(p->CompletionPort, TimerOrWaitFired, |
---|
245 | n/a | 0, p->Overlapped); |
---|
246 | n/a | /* ignore possible error! */ |
---|
247 | n/a | PyMem_RawFree(p); |
---|
248 | n/a | } |
---|
249 | n/a | |
---|
250 | n/a | PyDoc_STRVAR( |
---|
251 | n/a | RegisterWaitWithQueue_doc, |
---|
252 | n/a | "RegisterWaitWithQueue(Object, CompletionPort, Overlapped, Timeout)\n" |
---|
253 | n/a | " -> WaitHandle\n\n" |
---|
254 | n/a | "Register wait for Object; when complete CompletionPort is notified.\n"); |
---|
255 | n/a | |
---|
256 | n/a | static PyObject * |
---|
257 | n/a | overlapped_RegisterWaitWithQueue(PyObject *self, PyObject *args) |
---|
258 | n/a | { |
---|
259 | n/a | HANDLE NewWaitObject; |
---|
260 | n/a | HANDLE Object; |
---|
261 | n/a | ULONG Milliseconds; |
---|
262 | n/a | struct PostCallbackData data, *pdata; |
---|
263 | n/a | |
---|
264 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE F_POINTER F_DWORD, |
---|
265 | n/a | &Object, |
---|
266 | n/a | &data.CompletionPort, |
---|
267 | n/a | &data.Overlapped, |
---|
268 | n/a | &Milliseconds)) |
---|
269 | n/a | return NULL; |
---|
270 | n/a | |
---|
271 | n/a | /* Use PyMem_RawMalloc() rather than PyMem_Malloc(), since |
---|
272 | n/a | PostToQueueCallback() will call PyMem_Free() from a new C thread |
---|
273 | n/a | which doesn't hold the GIL. */ |
---|
274 | n/a | pdata = PyMem_RawMalloc(sizeof(struct PostCallbackData)); |
---|
275 | n/a | if (pdata == NULL) |
---|
276 | n/a | return SetFromWindowsErr(0); |
---|
277 | n/a | |
---|
278 | n/a | *pdata = data; |
---|
279 | n/a | |
---|
280 | n/a | if (!RegisterWaitForSingleObject( |
---|
281 | n/a | &NewWaitObject, Object, (WAITORTIMERCALLBACK)PostToQueueCallback, |
---|
282 | n/a | pdata, Milliseconds, |
---|
283 | n/a | WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE)) |
---|
284 | n/a | { |
---|
285 | n/a | PyMem_RawFree(pdata); |
---|
286 | n/a | return SetFromWindowsErr(0); |
---|
287 | n/a | } |
---|
288 | n/a | |
---|
289 | n/a | return Py_BuildValue(F_HANDLE, NewWaitObject); |
---|
290 | n/a | } |
---|
291 | n/a | |
---|
292 | n/a | PyDoc_STRVAR( |
---|
293 | n/a | UnregisterWait_doc, |
---|
294 | n/a | "UnregisterWait(WaitHandle) -> None\n\n" |
---|
295 | n/a | "Unregister wait handle.\n"); |
---|
296 | n/a | |
---|
297 | n/a | static PyObject * |
---|
298 | n/a | overlapped_UnregisterWait(PyObject *self, PyObject *args) |
---|
299 | n/a | { |
---|
300 | n/a | HANDLE WaitHandle; |
---|
301 | n/a | BOOL ret; |
---|
302 | n/a | |
---|
303 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE, &WaitHandle)) |
---|
304 | n/a | return NULL; |
---|
305 | n/a | |
---|
306 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
307 | n/a | ret = UnregisterWait(WaitHandle); |
---|
308 | n/a | Py_END_ALLOW_THREADS |
---|
309 | n/a | |
---|
310 | n/a | if (!ret) |
---|
311 | n/a | return SetFromWindowsErr(0); |
---|
312 | n/a | Py_RETURN_NONE; |
---|
313 | n/a | } |
---|
314 | n/a | |
---|
315 | n/a | PyDoc_STRVAR( |
---|
316 | n/a | UnregisterWaitEx_doc, |
---|
317 | n/a | "UnregisterWaitEx(WaitHandle, Event) -> None\n\n" |
---|
318 | n/a | "Unregister wait handle.\n"); |
---|
319 | n/a | |
---|
320 | n/a | static PyObject * |
---|
321 | n/a | overlapped_UnregisterWaitEx(PyObject *self, PyObject *args) |
---|
322 | n/a | { |
---|
323 | n/a | HANDLE WaitHandle, Event; |
---|
324 | n/a | BOOL ret; |
---|
325 | n/a | |
---|
326 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE, &WaitHandle, &Event)) |
---|
327 | n/a | return NULL; |
---|
328 | n/a | |
---|
329 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
330 | n/a | ret = UnregisterWaitEx(WaitHandle, Event); |
---|
331 | n/a | Py_END_ALLOW_THREADS |
---|
332 | n/a | |
---|
333 | n/a | if (!ret) |
---|
334 | n/a | return SetFromWindowsErr(0); |
---|
335 | n/a | Py_RETURN_NONE; |
---|
336 | n/a | } |
---|
337 | n/a | |
---|
338 | n/a | /* |
---|
339 | n/a | * Event functions -- currently only used by tests |
---|
340 | n/a | */ |
---|
341 | n/a | |
---|
342 | n/a | PyDoc_STRVAR( |
---|
343 | n/a | CreateEvent_doc, |
---|
344 | n/a | "CreateEvent(EventAttributes, ManualReset, InitialState, Name)" |
---|
345 | n/a | " -> Handle\n\n" |
---|
346 | n/a | "Create an event. EventAttributes must be None.\n"); |
---|
347 | n/a | |
---|
348 | n/a | static PyObject * |
---|
349 | n/a | overlapped_CreateEvent(PyObject *self, PyObject *args) |
---|
350 | n/a | { |
---|
351 | n/a | PyObject *EventAttributes; |
---|
352 | n/a | BOOL ManualReset; |
---|
353 | n/a | BOOL InitialState; |
---|
354 | n/a | Py_UNICODE *Name; |
---|
355 | n/a | HANDLE Event; |
---|
356 | n/a | |
---|
357 | n/a | if (!PyArg_ParseTuple(args, "O" F_BOOL F_BOOL "Z", |
---|
358 | n/a | &EventAttributes, &ManualReset, |
---|
359 | n/a | &InitialState, &Name)) |
---|
360 | n/a | return NULL; |
---|
361 | n/a | |
---|
362 | n/a | if (EventAttributes != Py_None) { |
---|
363 | n/a | PyErr_SetString(PyExc_ValueError, "EventAttributes must be None"); |
---|
364 | n/a | return NULL; |
---|
365 | n/a | } |
---|
366 | n/a | |
---|
367 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
368 | n/a | Event = CreateEventW(NULL, ManualReset, InitialState, Name); |
---|
369 | n/a | Py_END_ALLOW_THREADS |
---|
370 | n/a | |
---|
371 | n/a | if (Event == NULL) |
---|
372 | n/a | return SetFromWindowsErr(0); |
---|
373 | n/a | return Py_BuildValue(F_HANDLE, Event); |
---|
374 | n/a | } |
---|
375 | n/a | |
---|
376 | n/a | PyDoc_STRVAR( |
---|
377 | n/a | SetEvent_doc, |
---|
378 | n/a | "SetEvent(Handle) -> None\n\n" |
---|
379 | n/a | "Set event.\n"); |
---|
380 | n/a | |
---|
381 | n/a | static PyObject * |
---|
382 | n/a | overlapped_SetEvent(PyObject *self, PyObject *args) |
---|
383 | n/a | { |
---|
384 | n/a | HANDLE Handle; |
---|
385 | n/a | BOOL ret; |
---|
386 | n/a | |
---|
387 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE, &Handle)) |
---|
388 | n/a | return NULL; |
---|
389 | n/a | |
---|
390 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
391 | n/a | ret = SetEvent(Handle); |
---|
392 | n/a | Py_END_ALLOW_THREADS |
---|
393 | n/a | |
---|
394 | n/a | if (!ret) |
---|
395 | n/a | return SetFromWindowsErr(0); |
---|
396 | n/a | Py_RETURN_NONE; |
---|
397 | n/a | } |
---|
398 | n/a | |
---|
399 | n/a | PyDoc_STRVAR( |
---|
400 | n/a | ResetEvent_doc, |
---|
401 | n/a | "ResetEvent(Handle) -> None\n\n" |
---|
402 | n/a | "Reset event.\n"); |
---|
403 | n/a | |
---|
404 | n/a | static PyObject * |
---|
405 | n/a | overlapped_ResetEvent(PyObject *self, PyObject *args) |
---|
406 | n/a | { |
---|
407 | n/a | HANDLE Handle; |
---|
408 | n/a | BOOL ret; |
---|
409 | n/a | |
---|
410 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE, &Handle)) |
---|
411 | n/a | return NULL; |
---|
412 | n/a | |
---|
413 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
414 | n/a | ret = ResetEvent(Handle); |
---|
415 | n/a | Py_END_ALLOW_THREADS |
---|
416 | n/a | |
---|
417 | n/a | if (!ret) |
---|
418 | n/a | return SetFromWindowsErr(0); |
---|
419 | n/a | Py_RETURN_NONE; |
---|
420 | n/a | } |
---|
421 | n/a | |
---|
422 | n/a | /* |
---|
423 | n/a | * Bind socket handle to local port without doing slow getaddrinfo() |
---|
424 | n/a | */ |
---|
425 | n/a | |
---|
426 | n/a | PyDoc_STRVAR( |
---|
427 | n/a | BindLocal_doc, |
---|
428 | n/a | "BindLocal(handle, family) -> None\n\n" |
---|
429 | n/a | "Bind a socket handle to an arbitrary local port.\n" |
---|
430 | n/a | "family should AF_INET or AF_INET6.\n"); |
---|
431 | n/a | |
---|
432 | n/a | static PyObject * |
---|
433 | n/a | overlapped_BindLocal(PyObject *self, PyObject *args) |
---|
434 | n/a | { |
---|
435 | n/a | SOCKET Socket; |
---|
436 | n/a | int Family; |
---|
437 | n/a | BOOL ret; |
---|
438 | n/a | |
---|
439 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE "i", &Socket, &Family)) |
---|
440 | n/a | return NULL; |
---|
441 | n/a | |
---|
442 | n/a | if (Family == AF_INET) { |
---|
443 | n/a | struct sockaddr_in addr; |
---|
444 | n/a | memset(&addr, 0, sizeof(addr)); |
---|
445 | n/a | addr.sin_family = AF_INET; |
---|
446 | n/a | addr.sin_port = 0; |
---|
447 | n/a | addr.sin_addr.S_un.S_addr = INADDR_ANY; |
---|
448 | n/a | ret = bind(Socket, (SOCKADDR*)&addr, sizeof(addr)) != SOCKET_ERROR; |
---|
449 | n/a | } else if (Family == AF_INET6) { |
---|
450 | n/a | struct sockaddr_in6 addr; |
---|
451 | n/a | memset(&addr, 0, sizeof(addr)); |
---|
452 | n/a | addr.sin6_family = AF_INET6; |
---|
453 | n/a | addr.sin6_port = 0; |
---|
454 | n/a | addr.sin6_addr = in6addr_any; |
---|
455 | n/a | ret = bind(Socket, (SOCKADDR*)&addr, sizeof(addr)) != SOCKET_ERROR; |
---|
456 | n/a | } else { |
---|
457 | n/a | PyErr_SetString(PyExc_ValueError, "expected tuple of length 2 or 4"); |
---|
458 | n/a | return NULL; |
---|
459 | n/a | } |
---|
460 | n/a | |
---|
461 | n/a | if (!ret) |
---|
462 | n/a | return SetFromWindowsErr(WSAGetLastError()); |
---|
463 | n/a | Py_RETURN_NONE; |
---|
464 | n/a | } |
---|
465 | n/a | |
---|
466 | n/a | /* |
---|
467 | n/a | * Windows equivalent of os.strerror() -- compare _ctypes/callproc.c |
---|
468 | n/a | */ |
---|
469 | n/a | |
---|
470 | n/a | PyDoc_STRVAR( |
---|
471 | n/a | FormatMessage_doc, |
---|
472 | n/a | "FormatMessage(error_code) -> error_message\n\n" |
---|
473 | n/a | "Return error message for an error code."); |
---|
474 | n/a | |
---|
475 | n/a | static PyObject * |
---|
476 | n/a | overlapped_FormatMessage(PyObject *ignore, PyObject *args) |
---|
477 | n/a | { |
---|
478 | n/a | DWORD code, n; |
---|
479 | n/a | WCHAR *lpMsgBuf; |
---|
480 | n/a | PyObject *res; |
---|
481 | n/a | |
---|
482 | n/a | if (!PyArg_ParseTuple(args, F_DWORD, &code)) |
---|
483 | n/a | return NULL; |
---|
484 | n/a | |
---|
485 | n/a | n = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
---|
486 | n/a | FORMAT_MESSAGE_FROM_SYSTEM, |
---|
487 | n/a | NULL, |
---|
488 | n/a | code, |
---|
489 | n/a | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
---|
490 | n/a | (LPWSTR) &lpMsgBuf, |
---|
491 | n/a | 0, |
---|
492 | n/a | NULL); |
---|
493 | n/a | if (n) { |
---|
494 | n/a | while (iswspace(lpMsgBuf[n-1])) |
---|
495 | n/a | --n; |
---|
496 | n/a | lpMsgBuf[n] = L'\0'; |
---|
497 | n/a | res = Py_BuildValue("u", lpMsgBuf); |
---|
498 | n/a | } else { |
---|
499 | n/a | res = PyUnicode_FromFormat("unknown error code %u", code); |
---|
500 | n/a | } |
---|
501 | n/a | LocalFree(lpMsgBuf); |
---|
502 | n/a | return res; |
---|
503 | n/a | } |
---|
504 | n/a | |
---|
505 | n/a | |
---|
506 | n/a | /* |
---|
507 | n/a | * Mark operation as completed - used when reading produces ERROR_BROKEN_PIPE |
---|
508 | n/a | */ |
---|
509 | n/a | |
---|
510 | n/a | static void |
---|
511 | n/a | mark_as_completed(OVERLAPPED *ov) |
---|
512 | n/a | { |
---|
513 | n/a | ov->Internal = 0; |
---|
514 | n/a | if (ov->hEvent != NULL) |
---|
515 | n/a | SetEvent(ov->hEvent); |
---|
516 | n/a | } |
---|
517 | n/a | |
---|
518 | n/a | /* |
---|
519 | n/a | * A Python object wrapping an OVERLAPPED structure and other useful data |
---|
520 | n/a | * for overlapped I/O |
---|
521 | n/a | */ |
---|
522 | n/a | |
---|
523 | n/a | PyDoc_STRVAR( |
---|
524 | n/a | Overlapped_doc, |
---|
525 | n/a | "Overlapped object"); |
---|
526 | n/a | |
---|
527 | n/a | static PyObject * |
---|
528 | n/a | Overlapped_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
529 | n/a | { |
---|
530 | n/a | OverlappedObject *self; |
---|
531 | n/a | HANDLE event = INVALID_HANDLE_VALUE; |
---|
532 | n/a | static char *kwlist[] = {"event", NULL}; |
---|
533 | n/a | |
---|
534 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|" F_HANDLE, kwlist, &event)) |
---|
535 | n/a | return NULL; |
---|
536 | n/a | |
---|
537 | n/a | if (event == INVALID_HANDLE_VALUE) { |
---|
538 | n/a | event = CreateEvent(NULL, TRUE, FALSE, NULL); |
---|
539 | n/a | if (event == NULL) |
---|
540 | n/a | return SetFromWindowsErr(0); |
---|
541 | n/a | } |
---|
542 | n/a | |
---|
543 | n/a | self = PyObject_New(OverlappedObject, type); |
---|
544 | n/a | if (self == NULL) { |
---|
545 | n/a | if (event != NULL) |
---|
546 | n/a | CloseHandle(event); |
---|
547 | n/a | return NULL; |
---|
548 | n/a | } |
---|
549 | n/a | |
---|
550 | n/a | self->handle = NULL; |
---|
551 | n/a | self->error = 0; |
---|
552 | n/a | self->type = TYPE_NONE; |
---|
553 | n/a | self->read_buffer = NULL; |
---|
554 | n/a | memset(&self->overlapped, 0, sizeof(OVERLAPPED)); |
---|
555 | n/a | memset(&self->write_buffer, 0, sizeof(Py_buffer)); |
---|
556 | n/a | if (event) |
---|
557 | n/a | self->overlapped.hEvent = event; |
---|
558 | n/a | return (PyObject *)self; |
---|
559 | n/a | } |
---|
560 | n/a | |
---|
561 | n/a | static void |
---|
562 | n/a | Overlapped_dealloc(OverlappedObject *self) |
---|
563 | n/a | { |
---|
564 | n/a | DWORD bytes; |
---|
565 | n/a | DWORD olderr = GetLastError(); |
---|
566 | n/a | BOOL wait = FALSE; |
---|
567 | n/a | BOOL ret; |
---|
568 | n/a | |
---|
569 | n/a | if (!HasOverlappedIoCompleted(&self->overlapped) && |
---|
570 | n/a | self->type != TYPE_NOT_STARTED) |
---|
571 | n/a | { |
---|
572 | n/a | if (Py_CancelIoEx && Py_CancelIoEx(self->handle, &self->overlapped)) |
---|
573 | n/a | wait = TRUE; |
---|
574 | n/a | |
---|
575 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
576 | n/a | ret = GetOverlappedResult(self->handle, &self->overlapped, |
---|
577 | n/a | &bytes, wait); |
---|
578 | n/a | Py_END_ALLOW_THREADS |
---|
579 | n/a | |
---|
580 | n/a | switch (ret ? ERROR_SUCCESS : GetLastError()) { |
---|
581 | n/a | case ERROR_SUCCESS: |
---|
582 | n/a | case ERROR_NOT_FOUND: |
---|
583 | n/a | case ERROR_OPERATION_ABORTED: |
---|
584 | n/a | break; |
---|
585 | n/a | default: |
---|
586 | n/a | PyErr_Format( |
---|
587 | n/a | PyExc_RuntimeError, |
---|
588 | n/a | "%R still has pending operation at " |
---|
589 | n/a | "deallocation, the process may crash", self); |
---|
590 | n/a | PyErr_WriteUnraisable(NULL); |
---|
591 | n/a | } |
---|
592 | n/a | } |
---|
593 | n/a | |
---|
594 | n/a | if (self->overlapped.hEvent != NULL) |
---|
595 | n/a | CloseHandle(self->overlapped.hEvent); |
---|
596 | n/a | |
---|
597 | n/a | switch (self->type) { |
---|
598 | n/a | case TYPE_READ: |
---|
599 | n/a | case TYPE_ACCEPT: |
---|
600 | n/a | Py_CLEAR(self->read_buffer); |
---|
601 | n/a | break; |
---|
602 | n/a | case TYPE_WRITE: |
---|
603 | n/a | if (self->write_buffer.obj) |
---|
604 | n/a | PyBuffer_Release(&self->write_buffer); |
---|
605 | n/a | break; |
---|
606 | n/a | } |
---|
607 | n/a | PyObject_Del(self); |
---|
608 | n/a | SetLastError(olderr); |
---|
609 | n/a | } |
---|
610 | n/a | |
---|
611 | n/a | PyDoc_STRVAR( |
---|
612 | n/a | Overlapped_cancel_doc, |
---|
613 | n/a | "cancel() -> None\n\n" |
---|
614 | n/a | "Cancel overlapped operation"); |
---|
615 | n/a | |
---|
616 | n/a | static PyObject * |
---|
617 | n/a | Overlapped_cancel(OverlappedObject *self) |
---|
618 | n/a | { |
---|
619 | n/a | BOOL ret = TRUE; |
---|
620 | n/a | |
---|
621 | n/a | if (self->type == TYPE_NOT_STARTED |
---|
622 | n/a | || self->type == TYPE_WAIT_NAMED_PIPE_AND_CONNECT) |
---|
623 | n/a | Py_RETURN_NONE; |
---|
624 | n/a | |
---|
625 | n/a | if (!HasOverlappedIoCompleted(&self->overlapped)) { |
---|
626 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
627 | n/a | if (Py_CancelIoEx) |
---|
628 | n/a | ret = Py_CancelIoEx(self->handle, &self->overlapped); |
---|
629 | n/a | else |
---|
630 | n/a | ret = CancelIo(self->handle); |
---|
631 | n/a | Py_END_ALLOW_THREADS |
---|
632 | n/a | } |
---|
633 | n/a | |
---|
634 | n/a | /* CancelIoEx returns ERROR_NOT_FOUND if the I/O completed in-between */ |
---|
635 | n/a | if (!ret && GetLastError() != ERROR_NOT_FOUND) |
---|
636 | n/a | return SetFromWindowsErr(0); |
---|
637 | n/a | Py_RETURN_NONE; |
---|
638 | n/a | } |
---|
639 | n/a | |
---|
640 | n/a | PyDoc_STRVAR( |
---|
641 | n/a | Overlapped_getresult_doc, |
---|
642 | n/a | "getresult(wait=False) -> result\n\n" |
---|
643 | n/a | "Retrieve result of operation. If wait is true then it blocks\n" |
---|
644 | n/a | "until the operation is finished. If wait is false and the\n" |
---|
645 | n/a | "operation is still pending then an error is raised."); |
---|
646 | n/a | |
---|
647 | n/a | static PyObject * |
---|
648 | n/a | Overlapped_getresult(OverlappedObject *self, PyObject *args) |
---|
649 | n/a | { |
---|
650 | n/a | BOOL wait = FALSE; |
---|
651 | n/a | DWORD transferred = 0; |
---|
652 | n/a | BOOL ret; |
---|
653 | n/a | DWORD err; |
---|
654 | n/a | |
---|
655 | n/a | if (!PyArg_ParseTuple(args, "|" F_BOOL, &wait)) |
---|
656 | n/a | return NULL; |
---|
657 | n/a | |
---|
658 | n/a | if (self->type == TYPE_NONE) { |
---|
659 | n/a | PyErr_SetString(PyExc_ValueError, "operation not yet attempted"); |
---|
660 | n/a | return NULL; |
---|
661 | n/a | } |
---|
662 | n/a | |
---|
663 | n/a | if (self->type == TYPE_NOT_STARTED) { |
---|
664 | n/a | PyErr_SetString(PyExc_ValueError, "operation failed to start"); |
---|
665 | n/a | return NULL; |
---|
666 | n/a | } |
---|
667 | n/a | |
---|
668 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
669 | n/a | ret = GetOverlappedResult(self->handle, &self->overlapped, &transferred, |
---|
670 | n/a | wait); |
---|
671 | n/a | Py_END_ALLOW_THREADS |
---|
672 | n/a | |
---|
673 | n/a | self->error = err = ret ? ERROR_SUCCESS : GetLastError(); |
---|
674 | n/a | switch (err) { |
---|
675 | n/a | case ERROR_SUCCESS: |
---|
676 | n/a | case ERROR_MORE_DATA: |
---|
677 | n/a | break; |
---|
678 | n/a | case ERROR_BROKEN_PIPE: |
---|
679 | n/a | if ((self->type == TYPE_READ || self->type == TYPE_ACCEPT) && self->read_buffer != NULL) |
---|
680 | n/a | break; |
---|
681 | n/a | /* fall through */ |
---|
682 | n/a | default: |
---|
683 | n/a | return SetFromWindowsErr(err); |
---|
684 | n/a | } |
---|
685 | n/a | |
---|
686 | n/a | switch (self->type) { |
---|
687 | n/a | case TYPE_READ: |
---|
688 | n/a | assert(PyBytes_CheckExact(self->read_buffer)); |
---|
689 | n/a | if (transferred != PyBytes_GET_SIZE(self->read_buffer) && |
---|
690 | n/a | _PyBytes_Resize(&self->read_buffer, transferred)) |
---|
691 | n/a | return NULL; |
---|
692 | n/a | Py_INCREF(self->read_buffer); |
---|
693 | n/a | return self->read_buffer; |
---|
694 | n/a | default: |
---|
695 | n/a | return PyLong_FromUnsignedLong((unsigned long) transferred); |
---|
696 | n/a | } |
---|
697 | n/a | } |
---|
698 | n/a | |
---|
699 | n/a | PyDoc_STRVAR( |
---|
700 | n/a | Overlapped_ReadFile_doc, |
---|
701 | n/a | "ReadFile(handle, size) -> Overlapped[message]\n\n" |
---|
702 | n/a | "Start overlapped read"); |
---|
703 | n/a | |
---|
704 | n/a | static PyObject * |
---|
705 | n/a | Overlapped_ReadFile(OverlappedObject *self, PyObject *args) |
---|
706 | n/a | { |
---|
707 | n/a | HANDLE handle; |
---|
708 | n/a | DWORD size; |
---|
709 | n/a | DWORD nread; |
---|
710 | n/a | PyObject *buf; |
---|
711 | n/a | BOOL ret; |
---|
712 | n/a | DWORD err; |
---|
713 | n/a | |
---|
714 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_DWORD, &handle, &size)) |
---|
715 | n/a | return NULL; |
---|
716 | n/a | |
---|
717 | n/a | if (self->type != TYPE_NONE) { |
---|
718 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
719 | n/a | return NULL; |
---|
720 | n/a | } |
---|
721 | n/a | |
---|
722 | n/a | #if SIZEOF_SIZE_T <= SIZEOF_LONG |
---|
723 | n/a | size = Py_MIN(size, (DWORD)PY_SSIZE_T_MAX); |
---|
724 | n/a | #endif |
---|
725 | n/a | buf = PyBytes_FromStringAndSize(NULL, Py_MAX(size, 1)); |
---|
726 | n/a | if (buf == NULL) |
---|
727 | n/a | return NULL; |
---|
728 | n/a | |
---|
729 | n/a | self->type = TYPE_READ; |
---|
730 | n/a | self->handle = handle; |
---|
731 | n/a | self->read_buffer = buf; |
---|
732 | n/a | |
---|
733 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
734 | n/a | ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread, |
---|
735 | n/a | &self->overlapped); |
---|
736 | n/a | Py_END_ALLOW_THREADS |
---|
737 | n/a | |
---|
738 | n/a | self->error = err = ret ? ERROR_SUCCESS : GetLastError(); |
---|
739 | n/a | switch (err) { |
---|
740 | n/a | case ERROR_BROKEN_PIPE: |
---|
741 | n/a | mark_as_completed(&self->overlapped); |
---|
742 | n/a | return SetFromWindowsErr(err); |
---|
743 | n/a | case ERROR_SUCCESS: |
---|
744 | n/a | case ERROR_MORE_DATA: |
---|
745 | n/a | case ERROR_IO_PENDING: |
---|
746 | n/a | Py_RETURN_NONE; |
---|
747 | n/a | default: |
---|
748 | n/a | self->type = TYPE_NOT_STARTED; |
---|
749 | n/a | return SetFromWindowsErr(err); |
---|
750 | n/a | } |
---|
751 | n/a | } |
---|
752 | n/a | |
---|
753 | n/a | PyDoc_STRVAR( |
---|
754 | n/a | Overlapped_WSARecv_doc, |
---|
755 | n/a | "RecvFile(handle, size, flags) -> Overlapped[message]\n\n" |
---|
756 | n/a | "Start overlapped receive"); |
---|
757 | n/a | |
---|
758 | n/a | static PyObject * |
---|
759 | n/a | Overlapped_WSARecv(OverlappedObject *self, PyObject *args) |
---|
760 | n/a | { |
---|
761 | n/a | HANDLE handle; |
---|
762 | n/a | DWORD size; |
---|
763 | n/a | DWORD flags = 0; |
---|
764 | n/a | DWORD nread; |
---|
765 | n/a | PyObject *buf; |
---|
766 | n/a | WSABUF wsabuf; |
---|
767 | n/a | int ret; |
---|
768 | n/a | DWORD err; |
---|
769 | n/a | |
---|
770 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_DWORD "|" F_DWORD, |
---|
771 | n/a | &handle, &size, &flags)) |
---|
772 | n/a | return NULL; |
---|
773 | n/a | |
---|
774 | n/a | if (self->type != TYPE_NONE) { |
---|
775 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
776 | n/a | return NULL; |
---|
777 | n/a | } |
---|
778 | n/a | |
---|
779 | n/a | #if SIZEOF_SIZE_T <= SIZEOF_LONG |
---|
780 | n/a | size = Py_MIN(size, (DWORD)PY_SSIZE_T_MAX); |
---|
781 | n/a | #endif |
---|
782 | n/a | buf = PyBytes_FromStringAndSize(NULL, Py_MAX(size, 1)); |
---|
783 | n/a | if (buf == NULL) |
---|
784 | n/a | return NULL; |
---|
785 | n/a | |
---|
786 | n/a | self->type = TYPE_READ; |
---|
787 | n/a | self->handle = handle; |
---|
788 | n/a | self->read_buffer = buf; |
---|
789 | n/a | wsabuf.len = size; |
---|
790 | n/a | wsabuf.buf = PyBytes_AS_STRING(buf); |
---|
791 | n/a | |
---|
792 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
793 | n/a | ret = WSARecv((SOCKET)handle, &wsabuf, 1, &nread, &flags, |
---|
794 | n/a | &self->overlapped, NULL); |
---|
795 | n/a | Py_END_ALLOW_THREADS |
---|
796 | n/a | |
---|
797 | n/a | self->error = err = (ret < 0 ? WSAGetLastError() : ERROR_SUCCESS); |
---|
798 | n/a | switch (err) { |
---|
799 | n/a | case ERROR_BROKEN_PIPE: |
---|
800 | n/a | mark_as_completed(&self->overlapped); |
---|
801 | n/a | return SetFromWindowsErr(err); |
---|
802 | n/a | case ERROR_SUCCESS: |
---|
803 | n/a | case ERROR_MORE_DATA: |
---|
804 | n/a | case ERROR_IO_PENDING: |
---|
805 | n/a | Py_RETURN_NONE; |
---|
806 | n/a | default: |
---|
807 | n/a | self->type = TYPE_NOT_STARTED; |
---|
808 | n/a | return SetFromWindowsErr(err); |
---|
809 | n/a | } |
---|
810 | n/a | } |
---|
811 | n/a | |
---|
812 | n/a | PyDoc_STRVAR( |
---|
813 | n/a | Overlapped_WriteFile_doc, |
---|
814 | n/a | "WriteFile(handle, buf) -> Overlapped[bytes_transferred]\n\n" |
---|
815 | n/a | "Start overlapped write"); |
---|
816 | n/a | |
---|
817 | n/a | static PyObject * |
---|
818 | n/a | Overlapped_WriteFile(OverlappedObject *self, PyObject *args) |
---|
819 | n/a | { |
---|
820 | n/a | HANDLE handle; |
---|
821 | n/a | PyObject *bufobj; |
---|
822 | n/a | DWORD written; |
---|
823 | n/a | BOOL ret; |
---|
824 | n/a | DWORD err; |
---|
825 | n/a | |
---|
826 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE "O", &handle, &bufobj)) |
---|
827 | n/a | return NULL; |
---|
828 | n/a | |
---|
829 | n/a | if (self->type != TYPE_NONE) { |
---|
830 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
831 | n/a | return NULL; |
---|
832 | n/a | } |
---|
833 | n/a | |
---|
834 | n/a | if (!PyArg_Parse(bufobj, "y*", &self->write_buffer)) |
---|
835 | n/a | return NULL; |
---|
836 | n/a | |
---|
837 | n/a | #if SIZEOF_SIZE_T > SIZEOF_LONG |
---|
838 | n/a | if (self->write_buffer.len > (Py_ssize_t)ULONG_MAX) { |
---|
839 | n/a | PyBuffer_Release(&self->write_buffer); |
---|
840 | n/a | PyErr_SetString(PyExc_ValueError, "buffer to large"); |
---|
841 | n/a | return NULL; |
---|
842 | n/a | } |
---|
843 | n/a | #endif |
---|
844 | n/a | |
---|
845 | n/a | self->type = TYPE_WRITE; |
---|
846 | n/a | self->handle = handle; |
---|
847 | n/a | |
---|
848 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
849 | n/a | ret = WriteFile(handle, self->write_buffer.buf, |
---|
850 | n/a | (DWORD)self->write_buffer.len, |
---|
851 | n/a | &written, &self->overlapped); |
---|
852 | n/a | Py_END_ALLOW_THREADS |
---|
853 | n/a | |
---|
854 | n/a | self->error = err = ret ? ERROR_SUCCESS : GetLastError(); |
---|
855 | n/a | switch (err) { |
---|
856 | n/a | case ERROR_SUCCESS: |
---|
857 | n/a | case ERROR_IO_PENDING: |
---|
858 | n/a | Py_RETURN_NONE; |
---|
859 | n/a | default: |
---|
860 | n/a | self->type = TYPE_NOT_STARTED; |
---|
861 | n/a | return SetFromWindowsErr(err); |
---|
862 | n/a | } |
---|
863 | n/a | } |
---|
864 | n/a | |
---|
865 | n/a | PyDoc_STRVAR( |
---|
866 | n/a | Overlapped_WSASend_doc, |
---|
867 | n/a | "WSASend(handle, buf, flags) -> Overlapped[bytes_transferred]\n\n" |
---|
868 | n/a | "Start overlapped send"); |
---|
869 | n/a | |
---|
870 | n/a | static PyObject * |
---|
871 | n/a | Overlapped_WSASend(OverlappedObject *self, PyObject *args) |
---|
872 | n/a | { |
---|
873 | n/a | HANDLE handle; |
---|
874 | n/a | PyObject *bufobj; |
---|
875 | n/a | DWORD flags; |
---|
876 | n/a | DWORD written; |
---|
877 | n/a | WSABUF wsabuf; |
---|
878 | n/a | int ret; |
---|
879 | n/a | DWORD err; |
---|
880 | n/a | |
---|
881 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE "O" F_DWORD, |
---|
882 | n/a | &handle, &bufobj, &flags)) |
---|
883 | n/a | return NULL; |
---|
884 | n/a | |
---|
885 | n/a | if (self->type != TYPE_NONE) { |
---|
886 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
887 | n/a | return NULL; |
---|
888 | n/a | } |
---|
889 | n/a | |
---|
890 | n/a | if (!PyArg_Parse(bufobj, "y*", &self->write_buffer)) |
---|
891 | n/a | return NULL; |
---|
892 | n/a | |
---|
893 | n/a | #if SIZEOF_SIZE_T > SIZEOF_LONG |
---|
894 | n/a | if (self->write_buffer.len > (Py_ssize_t)ULONG_MAX) { |
---|
895 | n/a | PyBuffer_Release(&self->write_buffer); |
---|
896 | n/a | PyErr_SetString(PyExc_ValueError, "buffer to large"); |
---|
897 | n/a | return NULL; |
---|
898 | n/a | } |
---|
899 | n/a | #endif |
---|
900 | n/a | |
---|
901 | n/a | self->type = TYPE_WRITE; |
---|
902 | n/a | self->handle = handle; |
---|
903 | n/a | wsabuf.len = (DWORD)self->write_buffer.len; |
---|
904 | n/a | wsabuf.buf = self->write_buffer.buf; |
---|
905 | n/a | |
---|
906 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
907 | n/a | ret = WSASend((SOCKET)handle, &wsabuf, 1, &written, flags, |
---|
908 | n/a | &self->overlapped, NULL); |
---|
909 | n/a | Py_END_ALLOW_THREADS |
---|
910 | n/a | |
---|
911 | n/a | self->error = err = (ret < 0 ? WSAGetLastError() : ERROR_SUCCESS); |
---|
912 | n/a | switch (err) { |
---|
913 | n/a | case ERROR_SUCCESS: |
---|
914 | n/a | case ERROR_IO_PENDING: |
---|
915 | n/a | Py_RETURN_NONE; |
---|
916 | n/a | default: |
---|
917 | n/a | self->type = TYPE_NOT_STARTED; |
---|
918 | n/a | return SetFromWindowsErr(err); |
---|
919 | n/a | } |
---|
920 | n/a | } |
---|
921 | n/a | |
---|
922 | n/a | PyDoc_STRVAR( |
---|
923 | n/a | Overlapped_AcceptEx_doc, |
---|
924 | n/a | "AcceptEx(listen_handle, accept_handle) -> Overlapped[address_as_bytes]\n\n" |
---|
925 | n/a | "Start overlapped wait for client to connect"); |
---|
926 | n/a | |
---|
927 | n/a | static PyObject * |
---|
928 | n/a | Overlapped_AcceptEx(OverlappedObject *self, PyObject *args) |
---|
929 | n/a | { |
---|
930 | n/a | SOCKET ListenSocket; |
---|
931 | n/a | SOCKET AcceptSocket; |
---|
932 | n/a | DWORD BytesReceived; |
---|
933 | n/a | DWORD size; |
---|
934 | n/a | PyObject *buf; |
---|
935 | n/a | BOOL ret; |
---|
936 | n/a | DWORD err; |
---|
937 | n/a | |
---|
938 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE, |
---|
939 | n/a | &ListenSocket, &AcceptSocket)) |
---|
940 | n/a | return NULL; |
---|
941 | n/a | |
---|
942 | n/a | if (self->type != TYPE_NONE) { |
---|
943 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
944 | n/a | return NULL; |
---|
945 | n/a | } |
---|
946 | n/a | |
---|
947 | n/a | size = sizeof(struct sockaddr_in6) + 16; |
---|
948 | n/a | buf = PyBytes_FromStringAndSize(NULL, size*2); |
---|
949 | n/a | if (!buf) |
---|
950 | n/a | return NULL; |
---|
951 | n/a | |
---|
952 | n/a | self->type = TYPE_ACCEPT; |
---|
953 | n/a | self->handle = (HANDLE)ListenSocket; |
---|
954 | n/a | self->read_buffer = buf; |
---|
955 | n/a | |
---|
956 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
957 | n/a | ret = Py_AcceptEx(ListenSocket, AcceptSocket, PyBytes_AS_STRING(buf), |
---|
958 | n/a | 0, size, size, &BytesReceived, &self->overlapped); |
---|
959 | n/a | Py_END_ALLOW_THREADS |
---|
960 | n/a | |
---|
961 | n/a | self->error = err = ret ? ERROR_SUCCESS : WSAGetLastError(); |
---|
962 | n/a | switch (err) { |
---|
963 | n/a | case ERROR_SUCCESS: |
---|
964 | n/a | case ERROR_IO_PENDING: |
---|
965 | n/a | Py_RETURN_NONE; |
---|
966 | n/a | default: |
---|
967 | n/a | self->type = TYPE_NOT_STARTED; |
---|
968 | n/a | return SetFromWindowsErr(err); |
---|
969 | n/a | } |
---|
970 | n/a | } |
---|
971 | n/a | |
---|
972 | n/a | |
---|
973 | n/a | static int |
---|
974 | n/a | parse_address(PyObject *obj, SOCKADDR *Address, int Length) |
---|
975 | n/a | { |
---|
976 | n/a | Py_UNICODE *Host; |
---|
977 | n/a | unsigned short Port; |
---|
978 | n/a | unsigned long FlowInfo; |
---|
979 | n/a | unsigned long ScopeId; |
---|
980 | n/a | |
---|
981 | n/a | memset(Address, 0, Length); |
---|
982 | n/a | |
---|
983 | n/a | if (PyArg_ParseTuple(obj, "uH", &Host, &Port)) |
---|
984 | n/a | { |
---|
985 | n/a | Address->sa_family = AF_INET; |
---|
986 | n/a | if (WSAStringToAddressW(Host, AF_INET, NULL, Address, &Length) < 0) { |
---|
987 | n/a | SetFromWindowsErr(WSAGetLastError()); |
---|
988 | n/a | return -1; |
---|
989 | n/a | } |
---|
990 | n/a | ((SOCKADDR_IN*)Address)->sin_port = htons(Port); |
---|
991 | n/a | return Length; |
---|
992 | n/a | } |
---|
993 | n/a | else if (PyArg_ParseTuple(obj, "uHkk", &Host, &Port, &FlowInfo, &ScopeId)) |
---|
994 | n/a | { |
---|
995 | n/a | PyErr_Clear(); |
---|
996 | n/a | Address->sa_family = AF_INET6; |
---|
997 | n/a | if (WSAStringToAddressW(Host, AF_INET6, NULL, Address, &Length) < 0) { |
---|
998 | n/a | SetFromWindowsErr(WSAGetLastError()); |
---|
999 | n/a | return -1; |
---|
1000 | n/a | } |
---|
1001 | n/a | ((SOCKADDR_IN6*)Address)->sin6_port = htons(Port); |
---|
1002 | n/a | ((SOCKADDR_IN6*)Address)->sin6_flowinfo = FlowInfo; |
---|
1003 | n/a | ((SOCKADDR_IN6*)Address)->sin6_scope_id = ScopeId; |
---|
1004 | n/a | return Length; |
---|
1005 | n/a | } |
---|
1006 | n/a | |
---|
1007 | n/a | return -1; |
---|
1008 | n/a | } |
---|
1009 | n/a | |
---|
1010 | n/a | |
---|
1011 | n/a | PyDoc_STRVAR( |
---|
1012 | n/a | Overlapped_ConnectEx_doc, |
---|
1013 | n/a | "ConnectEx(client_handle, address_as_bytes) -> Overlapped[None]\n\n" |
---|
1014 | n/a | "Start overlapped connect. client_handle should be unbound."); |
---|
1015 | n/a | |
---|
1016 | n/a | static PyObject * |
---|
1017 | n/a | Overlapped_ConnectEx(OverlappedObject *self, PyObject *args) |
---|
1018 | n/a | { |
---|
1019 | n/a | SOCKET ConnectSocket; |
---|
1020 | n/a | PyObject *AddressObj; |
---|
1021 | n/a | char AddressBuf[sizeof(struct sockaddr_in6)]; |
---|
1022 | n/a | SOCKADDR *Address = (SOCKADDR*)AddressBuf; |
---|
1023 | n/a | int Length; |
---|
1024 | n/a | BOOL ret; |
---|
1025 | n/a | DWORD err; |
---|
1026 | n/a | |
---|
1027 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE "O", &ConnectSocket, &AddressObj)) |
---|
1028 | n/a | return NULL; |
---|
1029 | n/a | |
---|
1030 | n/a | if (self->type != TYPE_NONE) { |
---|
1031 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
1032 | n/a | return NULL; |
---|
1033 | n/a | } |
---|
1034 | n/a | |
---|
1035 | n/a | Length = sizeof(AddressBuf); |
---|
1036 | n/a | Length = parse_address(AddressObj, Address, Length); |
---|
1037 | n/a | if (Length < 0) |
---|
1038 | n/a | return NULL; |
---|
1039 | n/a | |
---|
1040 | n/a | self->type = TYPE_CONNECT; |
---|
1041 | n/a | self->handle = (HANDLE)ConnectSocket; |
---|
1042 | n/a | |
---|
1043 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1044 | n/a | ret = Py_ConnectEx(ConnectSocket, Address, Length, |
---|
1045 | n/a | NULL, 0, NULL, &self->overlapped); |
---|
1046 | n/a | Py_END_ALLOW_THREADS |
---|
1047 | n/a | |
---|
1048 | n/a | self->error = err = ret ? ERROR_SUCCESS : WSAGetLastError(); |
---|
1049 | n/a | switch (err) { |
---|
1050 | n/a | case ERROR_SUCCESS: |
---|
1051 | n/a | case ERROR_IO_PENDING: |
---|
1052 | n/a | Py_RETURN_NONE; |
---|
1053 | n/a | default: |
---|
1054 | n/a | self->type = TYPE_NOT_STARTED; |
---|
1055 | n/a | return SetFromWindowsErr(err); |
---|
1056 | n/a | } |
---|
1057 | n/a | } |
---|
1058 | n/a | |
---|
1059 | n/a | PyDoc_STRVAR( |
---|
1060 | n/a | Overlapped_DisconnectEx_doc, |
---|
1061 | n/a | "DisconnectEx(handle, flags) -> Overlapped[None]\n\n" |
---|
1062 | n/a | "Start overlapped connect. client_handle should be unbound."); |
---|
1063 | n/a | |
---|
1064 | n/a | static PyObject * |
---|
1065 | n/a | Overlapped_DisconnectEx(OverlappedObject *self, PyObject *args) |
---|
1066 | n/a | { |
---|
1067 | n/a | SOCKET Socket; |
---|
1068 | n/a | DWORD flags; |
---|
1069 | n/a | BOOL ret; |
---|
1070 | n/a | DWORD err; |
---|
1071 | n/a | |
---|
1072 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE F_DWORD, &Socket, &flags)) |
---|
1073 | n/a | return NULL; |
---|
1074 | n/a | |
---|
1075 | n/a | if (self->type != TYPE_NONE) { |
---|
1076 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
1077 | n/a | return NULL; |
---|
1078 | n/a | } |
---|
1079 | n/a | |
---|
1080 | n/a | self->type = TYPE_DISCONNECT; |
---|
1081 | n/a | self->handle = (HANDLE)Socket; |
---|
1082 | n/a | |
---|
1083 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1084 | n/a | ret = Py_DisconnectEx(Socket, &self->overlapped, flags, 0); |
---|
1085 | n/a | Py_END_ALLOW_THREADS |
---|
1086 | n/a | |
---|
1087 | n/a | self->error = err = ret ? ERROR_SUCCESS : WSAGetLastError(); |
---|
1088 | n/a | switch (err) { |
---|
1089 | n/a | case ERROR_SUCCESS: |
---|
1090 | n/a | case ERROR_IO_PENDING: |
---|
1091 | n/a | Py_RETURN_NONE; |
---|
1092 | n/a | default: |
---|
1093 | n/a | self->type = TYPE_NOT_STARTED; |
---|
1094 | n/a | return SetFromWindowsErr(err); |
---|
1095 | n/a | } |
---|
1096 | n/a | } |
---|
1097 | n/a | |
---|
1098 | n/a | PyDoc_STRVAR( |
---|
1099 | n/a | Overlapped_ConnectNamedPipe_doc, |
---|
1100 | n/a | "ConnectNamedPipe(handle) -> Overlapped[None]\n\n" |
---|
1101 | n/a | "Start overlapped wait for a client to connect."); |
---|
1102 | n/a | |
---|
1103 | n/a | static PyObject * |
---|
1104 | n/a | Overlapped_ConnectNamedPipe(OverlappedObject *self, PyObject *args) |
---|
1105 | n/a | { |
---|
1106 | n/a | HANDLE Pipe; |
---|
1107 | n/a | BOOL ret; |
---|
1108 | n/a | DWORD err; |
---|
1109 | n/a | |
---|
1110 | n/a | if (!PyArg_ParseTuple(args, F_HANDLE, &Pipe)) |
---|
1111 | n/a | return NULL; |
---|
1112 | n/a | |
---|
1113 | n/a | if (self->type != TYPE_NONE) { |
---|
1114 | n/a | PyErr_SetString(PyExc_ValueError, "operation already attempted"); |
---|
1115 | n/a | return NULL; |
---|
1116 | n/a | } |
---|
1117 | n/a | |
---|
1118 | n/a | self->type = TYPE_CONNECT_NAMED_PIPE; |
---|
1119 | n/a | self->handle = Pipe; |
---|
1120 | n/a | |
---|
1121 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1122 | n/a | ret = ConnectNamedPipe(Pipe, &self->overlapped); |
---|
1123 | n/a | Py_END_ALLOW_THREADS |
---|
1124 | n/a | |
---|
1125 | n/a | self->error = err = ret ? ERROR_SUCCESS : GetLastError(); |
---|
1126 | n/a | switch (err) { |
---|
1127 | n/a | case ERROR_PIPE_CONNECTED: |
---|
1128 | n/a | mark_as_completed(&self->overlapped); |
---|
1129 | n/a | Py_RETURN_TRUE; |
---|
1130 | n/a | case ERROR_SUCCESS: |
---|
1131 | n/a | case ERROR_IO_PENDING: |
---|
1132 | n/a | Py_RETURN_FALSE; |
---|
1133 | n/a | default: |
---|
1134 | n/a | self->type = TYPE_NOT_STARTED; |
---|
1135 | n/a | return SetFromWindowsErr(err); |
---|
1136 | n/a | } |
---|
1137 | n/a | } |
---|
1138 | n/a | |
---|
1139 | n/a | PyDoc_STRVAR( |
---|
1140 | n/a | ConnectPipe_doc, |
---|
1141 | n/a | "ConnectPipe(addr) -> pipe_handle\n\n" |
---|
1142 | n/a | "Connect to the pipe for asynchronous I/O (overlapped)."); |
---|
1143 | n/a | |
---|
1144 | n/a | static PyObject * |
---|
1145 | n/a | ConnectPipe(OverlappedObject *self, PyObject *args) |
---|
1146 | n/a | { |
---|
1147 | n/a | PyObject *AddressObj; |
---|
1148 | n/a | wchar_t *Address; |
---|
1149 | n/a | HANDLE PipeHandle; |
---|
1150 | n/a | |
---|
1151 | n/a | if (!PyArg_ParseTuple(args, "U", &AddressObj)) |
---|
1152 | n/a | return NULL; |
---|
1153 | n/a | |
---|
1154 | n/a | Address = PyUnicode_AsWideCharString(AddressObj, NULL); |
---|
1155 | n/a | if (Address == NULL) |
---|
1156 | n/a | return NULL; |
---|
1157 | n/a | |
---|
1158 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1159 | n/a | PipeHandle = CreateFileW(Address, |
---|
1160 | n/a | GENERIC_READ | GENERIC_WRITE, |
---|
1161 | n/a | 0, NULL, OPEN_EXISTING, |
---|
1162 | n/a | FILE_FLAG_OVERLAPPED, NULL); |
---|
1163 | n/a | Py_END_ALLOW_THREADS |
---|
1164 | n/a | |
---|
1165 | n/a | PyMem_Free(Address); |
---|
1166 | n/a | if (PipeHandle == INVALID_HANDLE_VALUE) |
---|
1167 | n/a | return SetFromWindowsErr(0); |
---|
1168 | n/a | return Py_BuildValue(F_HANDLE, PipeHandle); |
---|
1169 | n/a | } |
---|
1170 | n/a | |
---|
1171 | n/a | static PyObject* |
---|
1172 | n/a | Overlapped_getaddress(OverlappedObject *self) |
---|
1173 | n/a | { |
---|
1174 | n/a | return PyLong_FromVoidPtr(&self->overlapped); |
---|
1175 | n/a | } |
---|
1176 | n/a | |
---|
1177 | n/a | static PyObject* |
---|
1178 | n/a | Overlapped_getpending(OverlappedObject *self) |
---|
1179 | n/a | { |
---|
1180 | n/a | return PyBool_FromLong(!HasOverlappedIoCompleted(&self->overlapped) && |
---|
1181 | n/a | self->type != TYPE_NOT_STARTED); |
---|
1182 | n/a | } |
---|
1183 | n/a | |
---|
1184 | n/a | static PyMethodDef Overlapped_methods[] = { |
---|
1185 | n/a | {"getresult", (PyCFunction) Overlapped_getresult, |
---|
1186 | n/a | METH_VARARGS, Overlapped_getresult_doc}, |
---|
1187 | n/a | {"cancel", (PyCFunction) Overlapped_cancel, |
---|
1188 | n/a | METH_NOARGS, Overlapped_cancel_doc}, |
---|
1189 | n/a | {"ReadFile", (PyCFunction) Overlapped_ReadFile, |
---|
1190 | n/a | METH_VARARGS, Overlapped_ReadFile_doc}, |
---|
1191 | n/a | {"WSARecv", (PyCFunction) Overlapped_WSARecv, |
---|
1192 | n/a | METH_VARARGS, Overlapped_WSARecv_doc}, |
---|
1193 | n/a | {"WriteFile", (PyCFunction) Overlapped_WriteFile, |
---|
1194 | n/a | METH_VARARGS, Overlapped_WriteFile_doc}, |
---|
1195 | n/a | {"WSASend", (PyCFunction) Overlapped_WSASend, |
---|
1196 | n/a | METH_VARARGS, Overlapped_WSASend_doc}, |
---|
1197 | n/a | {"AcceptEx", (PyCFunction) Overlapped_AcceptEx, |
---|
1198 | n/a | METH_VARARGS, Overlapped_AcceptEx_doc}, |
---|
1199 | n/a | {"ConnectEx", (PyCFunction) Overlapped_ConnectEx, |
---|
1200 | n/a | METH_VARARGS, Overlapped_ConnectEx_doc}, |
---|
1201 | n/a | {"DisconnectEx", (PyCFunction) Overlapped_DisconnectEx, |
---|
1202 | n/a | METH_VARARGS, Overlapped_DisconnectEx_doc}, |
---|
1203 | n/a | {"ConnectNamedPipe", (PyCFunction) Overlapped_ConnectNamedPipe, |
---|
1204 | n/a | METH_VARARGS, Overlapped_ConnectNamedPipe_doc}, |
---|
1205 | n/a | {NULL} |
---|
1206 | n/a | }; |
---|
1207 | n/a | |
---|
1208 | n/a | static PyMemberDef Overlapped_members[] = { |
---|
1209 | n/a | {"error", T_ULONG, |
---|
1210 | n/a | offsetof(OverlappedObject, error), |
---|
1211 | n/a | READONLY, "Error from last operation"}, |
---|
1212 | n/a | {"event", T_HANDLE, |
---|
1213 | n/a | offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent), |
---|
1214 | n/a | READONLY, "Overlapped event handle"}, |
---|
1215 | n/a | {NULL} |
---|
1216 | n/a | }; |
---|
1217 | n/a | |
---|
1218 | n/a | static PyGetSetDef Overlapped_getsets[] = { |
---|
1219 | n/a | {"address", (getter)Overlapped_getaddress, NULL, |
---|
1220 | n/a | "Address of overlapped structure"}, |
---|
1221 | n/a | {"pending", (getter)Overlapped_getpending, NULL, |
---|
1222 | n/a | "Whether the operation is pending"}, |
---|
1223 | n/a | {NULL}, |
---|
1224 | n/a | }; |
---|
1225 | n/a | |
---|
1226 | n/a | PyTypeObject OverlappedType = { |
---|
1227 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1228 | n/a | /* tp_name */ "_overlapped.Overlapped", |
---|
1229 | n/a | /* tp_basicsize */ sizeof(OverlappedObject), |
---|
1230 | n/a | /* tp_itemsize */ 0, |
---|
1231 | n/a | /* tp_dealloc */ (destructor) Overlapped_dealloc, |
---|
1232 | n/a | /* tp_print */ 0, |
---|
1233 | n/a | /* tp_getattr */ 0, |
---|
1234 | n/a | /* tp_setattr */ 0, |
---|
1235 | n/a | /* tp_reserved */ 0, |
---|
1236 | n/a | /* tp_repr */ 0, |
---|
1237 | n/a | /* tp_as_number */ 0, |
---|
1238 | n/a | /* tp_as_sequence */ 0, |
---|
1239 | n/a | /* tp_as_mapping */ 0, |
---|
1240 | n/a | /* tp_hash */ 0, |
---|
1241 | n/a | /* tp_call */ 0, |
---|
1242 | n/a | /* tp_str */ 0, |
---|
1243 | n/a | /* tp_getattro */ 0, |
---|
1244 | n/a | /* tp_setattro */ 0, |
---|
1245 | n/a | /* tp_as_buffer */ 0, |
---|
1246 | n/a | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
---|
1247 | n/a | /* tp_doc */ "OVERLAPPED structure wrapper", |
---|
1248 | n/a | /* tp_traverse */ 0, |
---|
1249 | n/a | /* tp_clear */ 0, |
---|
1250 | n/a | /* tp_richcompare */ 0, |
---|
1251 | n/a | /* tp_weaklistoffset */ 0, |
---|
1252 | n/a | /* tp_iter */ 0, |
---|
1253 | n/a | /* tp_iternext */ 0, |
---|
1254 | n/a | /* tp_methods */ Overlapped_methods, |
---|
1255 | n/a | /* tp_members */ Overlapped_members, |
---|
1256 | n/a | /* tp_getset */ Overlapped_getsets, |
---|
1257 | n/a | /* tp_base */ 0, |
---|
1258 | n/a | /* tp_dict */ 0, |
---|
1259 | n/a | /* tp_descr_get */ 0, |
---|
1260 | n/a | /* tp_descr_set */ 0, |
---|
1261 | n/a | /* tp_dictoffset */ 0, |
---|
1262 | n/a | /* tp_init */ 0, |
---|
1263 | n/a | /* tp_alloc */ 0, |
---|
1264 | n/a | /* tp_new */ Overlapped_new, |
---|
1265 | n/a | }; |
---|
1266 | n/a | |
---|
1267 | n/a | static PyMethodDef overlapped_functions[] = { |
---|
1268 | n/a | {"CreateIoCompletionPort", overlapped_CreateIoCompletionPort, |
---|
1269 | n/a | METH_VARARGS, CreateIoCompletionPort_doc}, |
---|
1270 | n/a | {"GetQueuedCompletionStatus", overlapped_GetQueuedCompletionStatus, |
---|
1271 | n/a | METH_VARARGS, GetQueuedCompletionStatus_doc}, |
---|
1272 | n/a | {"PostQueuedCompletionStatus", overlapped_PostQueuedCompletionStatus, |
---|
1273 | n/a | METH_VARARGS, PostQueuedCompletionStatus_doc}, |
---|
1274 | n/a | {"FormatMessage", overlapped_FormatMessage, |
---|
1275 | n/a | METH_VARARGS, FormatMessage_doc}, |
---|
1276 | n/a | {"BindLocal", overlapped_BindLocal, |
---|
1277 | n/a | METH_VARARGS, BindLocal_doc}, |
---|
1278 | n/a | {"RegisterWaitWithQueue", overlapped_RegisterWaitWithQueue, |
---|
1279 | n/a | METH_VARARGS, RegisterWaitWithQueue_doc}, |
---|
1280 | n/a | {"UnregisterWait", overlapped_UnregisterWait, |
---|
1281 | n/a | METH_VARARGS, UnregisterWait_doc}, |
---|
1282 | n/a | {"UnregisterWaitEx", overlapped_UnregisterWaitEx, |
---|
1283 | n/a | METH_VARARGS, UnregisterWaitEx_doc}, |
---|
1284 | n/a | {"CreateEvent", overlapped_CreateEvent, |
---|
1285 | n/a | METH_VARARGS, CreateEvent_doc}, |
---|
1286 | n/a | {"SetEvent", overlapped_SetEvent, |
---|
1287 | n/a | METH_VARARGS, SetEvent_doc}, |
---|
1288 | n/a | {"ResetEvent", overlapped_ResetEvent, |
---|
1289 | n/a | METH_VARARGS, ResetEvent_doc}, |
---|
1290 | n/a | {"ConnectPipe", |
---|
1291 | n/a | (PyCFunction) ConnectPipe, |
---|
1292 | n/a | METH_VARARGS, ConnectPipe_doc}, |
---|
1293 | n/a | {NULL} |
---|
1294 | n/a | }; |
---|
1295 | n/a | |
---|
1296 | n/a | static struct PyModuleDef overlapped_module = { |
---|
1297 | n/a | PyModuleDef_HEAD_INIT, |
---|
1298 | n/a | "_overlapped", |
---|
1299 | n/a | NULL, |
---|
1300 | n/a | -1, |
---|
1301 | n/a | overlapped_functions, |
---|
1302 | n/a | NULL, |
---|
1303 | n/a | NULL, |
---|
1304 | n/a | NULL, |
---|
1305 | n/a | NULL |
---|
1306 | n/a | }; |
---|
1307 | n/a | |
---|
1308 | n/a | #define WINAPI_CONSTANT(fmt, con) \ |
---|
1309 | n/a | PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con)) |
---|
1310 | n/a | |
---|
1311 | n/a | PyMODINIT_FUNC |
---|
1312 | n/a | PyInit__overlapped(void) |
---|
1313 | n/a | { |
---|
1314 | n/a | PyObject *m, *d; |
---|
1315 | n/a | |
---|
1316 | n/a | /* Ensure WSAStartup() called before initializing function pointers */ |
---|
1317 | n/a | m = PyImport_ImportModule("_socket"); |
---|
1318 | n/a | if (!m) |
---|
1319 | n/a | return NULL; |
---|
1320 | n/a | Py_DECREF(m); |
---|
1321 | n/a | |
---|
1322 | n/a | if (initialize_function_pointers() < 0) |
---|
1323 | n/a | return NULL; |
---|
1324 | n/a | |
---|
1325 | n/a | if (PyType_Ready(&OverlappedType) < 0) |
---|
1326 | n/a | return NULL; |
---|
1327 | n/a | |
---|
1328 | n/a | m = PyModule_Create(&overlapped_module); |
---|
1329 | n/a | if (PyModule_AddObject(m, "Overlapped", (PyObject *)&OverlappedType) < 0) |
---|
1330 | n/a | return NULL; |
---|
1331 | n/a | |
---|
1332 | n/a | d = PyModule_GetDict(m); |
---|
1333 | n/a | |
---|
1334 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING); |
---|
1335 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
---|
1336 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT); |
---|
1337 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY); |
---|
1338 | n/a | WINAPI_CONSTANT(F_DWORD, INFINITE); |
---|
1339 | n/a | WINAPI_CONSTANT(F_HANDLE, INVALID_HANDLE_VALUE); |
---|
1340 | n/a | WINAPI_CONSTANT(F_HANDLE, NULL); |
---|
1341 | n/a | WINAPI_CONSTANT(F_DWORD, SO_UPDATE_ACCEPT_CONTEXT); |
---|
1342 | n/a | WINAPI_CONSTANT(F_DWORD, SO_UPDATE_CONNECT_CONTEXT); |
---|
1343 | n/a | WINAPI_CONSTANT(F_DWORD, TF_REUSE_SOCKET); |
---|
1344 | n/a | |
---|
1345 | n/a | return m; |
---|
1346 | n/a | } |
---|