- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathpipe_connection.c
149 lines (118 loc) · 3.56 KB
/
pipe_connection.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* A type which wraps a pipe handle in message oriented mode
*
* pipe_connection.c
*
* Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
*/
#include"multiprocessing.h"
#defineCLOSE(h) CloseHandle(h)
/*
* Send string to the pipe; assumes in message oriented mode
*/
staticPy_ssize_t
conn_send_string(ConnectionObject*conn, char*string, size_tlength)
{
DWORDamount_written;
BOOLret;
Py_BEGIN_ALLOW_THREADS
ret=WriteFile(conn->handle, string, length, &amount_written, NULL);
Py_END_ALLOW_THREADS
if (ret==0&&GetLastError() ==ERROR_NO_SYSTEM_RESOURCES) {
PyErr_Format(PyExc_ValueError, "Cannnot send %"PY_FORMAT_SIZE_T"d bytes over connection", length);
returnMP_STANDARD_ERROR;
}
returnret ? MP_SUCCESS : MP_STANDARD_ERROR;
}
/*
* Attempts to read into buffer, or if buffer too small into *newbuffer.
*
* Returns number of bytes read. Assumes in message oriented mode.
*/
staticPy_ssize_t
conn_recv_string(ConnectionObject*conn, char*buffer,
size_tbuflength, char**newbuffer, size_tmaxlength)
{
DWORDleft, length, full_length, err;
BOOLret;
*newbuffer=NULL;
Py_BEGIN_ALLOW_THREADS
ret=ReadFile(conn->handle, buffer, MIN(buflength, maxlength),
&length, NULL);
Py_END_ALLOW_THREADS
if (ret)
returnlength;
err=GetLastError();
if (err!=ERROR_MORE_DATA) {
if (err==ERROR_BROKEN_PIPE)
returnMP_END_OF_FILE;
returnMP_STANDARD_ERROR;
}
if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, NULL, &left))
returnMP_STANDARD_ERROR;
full_length=length+left;
if (full_length>maxlength)
returnMP_BAD_MESSAGE_LENGTH;
*newbuffer=PyMem_Malloc(full_length);
if (*newbuffer==NULL)
returnMP_MEMORY_ERROR;
memcpy(*newbuffer, buffer, length);
Py_BEGIN_ALLOW_THREADS
ret=ReadFile(conn->handle, *newbuffer+length, left, &length, NULL);
Py_END_ALLOW_THREADS
if (ret) {
assert(length==left);
returnfull_length;
} else {
PyMem_Free(*newbuffer);
returnMP_STANDARD_ERROR;
}
}
/*
* Check whether any data is available for reading
*/
staticint
conn_poll(ConnectionObject*conn, doubletimeout, PyThreadState*_save)
{
DWORDbytes, deadline, delay;
intdifference, res;
BOOLblock= FALSE;
if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
returnMP_STANDARD_ERROR;
if (timeout==0.0)
returnbytes>0;
if (timeout<0.0)
block= TRUE;
else
/* XXX does not check for overflow */
deadline=GetTickCount() + (DWORD)(1000*timeout+0.5);
Sleep(0);
for (delay=1 ; ; delay+=1) {
if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, &bytes, NULL))
returnMP_STANDARD_ERROR;
elseif (bytes>0)
return TRUE;
if (!block) {
difference=deadline-GetTickCount();
if (difference<0)
return FALSE;
if ((int)delay>difference)
delay=difference;
}
if (delay>20)
delay=20;
Sleep(delay);
/* check for signals */
Py_BLOCK_THREADS
res=PyErr_CheckSignals();
Py_UNBLOCK_THREADS
if (res)
returnMP_EXCEPTION_HAS_BEEN_SET;
}
}
/*
* "connection.h" defines the PipeConnection type using the definitions above
*/
#defineCONNECTION_NAME "PipeConnection"
#defineCONNECTION_TYPE PipeConnectionType
#include"connection.h"