2
* A type which wraps a pipe handle in message oriented mode
6
* Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
9
#include "multiprocessing.h"
11
#define CLOSE(h) CloseHandle(h)
14
* Send string to the pipe; assumes in message oriented mode
18
conn_send_string(ConnectionObject *conn, char *string, size_t length)
23
Py_BEGIN_ALLOW_THREADS
24
ret = WriteFile(conn->handle, string, length, &amount_written, NULL);
26
return ret ? MP_SUCCESS : MP_STANDARD_ERROR;
30
* Attempts to read into buffer, or if buffer too small into *newbuffer.
32
* Returns number of bytes read. Assumes in message oriented mode.
36
conn_recv_string(ConnectionObject *conn, char *buffer,
37
size_t buflength, char **newbuffer, size_t maxlength)
39
DWORD left, length, full_length, err;
43
Py_BEGIN_ALLOW_THREADS
44
ret = ReadFile(conn->handle, buffer, MIN(buflength, maxlength),
51
if (err != ERROR_MORE_DATA) {
52
if (err == ERROR_BROKEN_PIPE)
53
return MP_END_OF_FILE;
54
return MP_STANDARD_ERROR;
57
if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, NULL, &left))
58
return MP_STANDARD_ERROR;
60
full_length = length + left;
61
if (full_length > maxlength)
62
return MP_BAD_MESSAGE_LENGTH;
64
*newbuffer = PyMem_Malloc(full_length);
65
if (*newbuffer == NULL)
66
return MP_MEMORY_ERROR;
68
memcpy(*newbuffer, buffer, length);
70
Py_BEGIN_ALLOW_THREADS
71
ret = ReadFile(conn->handle, *newbuffer+length, left, &length, NULL);
74
assert(length == left);
77
PyMem_Free(*newbuffer);
78
return MP_STANDARD_ERROR;
83
* Check whether any data is available for reading
86
#define conn_poll(conn, timeout) conn_poll_save(conn, timeout, _save)
89
conn_poll_save(ConnectionObject *conn, double timeout, PyThreadState *_save)
91
DWORD bytes, deadline, delay;
95
if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
96
return MP_STANDARD_ERROR;
104
/* XXX does not check for overflow */
105
deadline = GetTickCount() + (DWORD)(1000 * timeout + 0.5);
109
for (delay = 1 ; ; delay += 1) {
110
if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
111
return MP_STANDARD_ERROR;
116
difference = deadline - GetTickCount();
119
if ((int)delay > difference)
128
/* check for signals */
130
res = PyErr_CheckSignals();
134
return MP_EXCEPTION_HAS_BEEN_SET;
139
* "connection.h" defines the PipeConnection type using the definitions above
142
#define CONNECTION_NAME "PipeConnection"
143
#define CONNECTION_TYPE PipeConnectionType
145
#include "connection.h"