Connection class based on a Windows named pipe. Overlapped I/O is used, so the handles must have been created with FILE_FLAG_OVERLAPPED.
| 273 | if _winapi: |
| 274 | |
| 275 | class PipeConnection(_ConnectionBase): |
| 276 | """ |
| 277 | Connection class based on a Windows named pipe. |
| 278 | Overlapped I/O is used, so the handles must have been created |
| 279 | with FILE_FLAG_OVERLAPPED. |
| 280 | """ |
| 281 | _got_empty_message = False |
| 282 | _send_ov = None |
| 283 | |
| 284 | def _close(self, _CloseHandle=_winapi.CloseHandle): |
| 285 | ov = self._send_ov |
| 286 | if ov is not None: |
| 287 | # Interrupt WaitForMultipleObjects() in _send_bytes() |
| 288 | ov.cancel() |
| 289 | _CloseHandle(self._handle) |
| 290 | |
| 291 | def _send_bytes(self, buf): |
| 292 | if self._send_ov is not None: |
| 293 | # A connection should only be used by a single thread |
| 294 | raise ValueError("concurrent send_bytes() calls " |
| 295 | "are not supported") |
| 296 | ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True) |
| 297 | self._send_ov = ov |
| 298 | try: |
| 299 | if err == _winapi.ERROR_IO_PENDING: |
| 300 | waitres = _winapi.WaitForMultipleObjects( |
| 301 | [ov.event], False, INFINITE) |
| 302 | assert waitres == WAIT_OBJECT_0 |
| 303 | except: |
| 304 | ov.cancel() |
| 305 | raise |
| 306 | finally: |
| 307 | self._send_ov = None |
| 308 | nwritten, err = ov.GetOverlappedResult(True) |
| 309 | if err == _winapi.ERROR_OPERATION_ABORTED: |
| 310 | # close() was called by another thread while |
| 311 | # WaitForMultipleObjects() was waiting for the overlapped |
| 312 | # operation. |
| 313 | raise OSError(errno.EPIPE, "handle is closed") |
| 314 | assert err == 0 |
| 315 | assert nwritten == len(buf) |
| 316 | |
| 317 | def _recv_bytes(self, maxsize=None): |
| 318 | if self._got_empty_message: |
| 319 | self._got_empty_message = False |
| 320 | return io.BytesIO() |
| 321 | else: |
| 322 | bsize = 128 if maxsize is None else min(maxsize, 128) |
| 323 | try: |
| 324 | ov, err = _winapi.ReadFile(self._handle, bsize, |
| 325 | overlapped=True) |
| 326 | |
| 327 | sentinel = object() |
| 328 | return_value = sentinel |
| 329 | try: |
| 330 | try: |
| 331 | if err == _winapi.ERROR_IO_PENDING: |
| 332 | waitres = _winapi.WaitForMultipleObjects( |
no outgoing calls
no test coverage detected
searching dependent graphs…