def accept(self): handle = win32.CreateNamedPipeW( self._path, win32.PIPE_ACCESS_DUPLEX | win32.FILE_FLAG_OVERLAPPED, win32.PIPE_TYPE_MESSAGE | win32.PIPE_READMODE_MESSAGE | win32.PIPE_WAIT, win32.PIPE_UNLIMITED_INSTANCES, _SIZE, _SIZE, 0, None) if win32.INVALID_HANDLE_VALUE == handle: error = win32.GetLastError() raise win32.create_WindowsError(error) else: overlapped = conveyor.connection.PipeConnection.createoverlapped( ) result = win32.ConnectNamedPipe(handle, overlapped) if not result: error = win32.GetLastError() if win32.ERROR_IO_PENDING == error: pass elif win32.ERROR_PIPE_CONNECTED == error: connection = conveyor.connection.PipeConnection.create( handle) return connection else: raise win32.create_WindowsError(error) connection = self._accept_pending(handle, overlapped) return connection
def write(self, data): """ writes data over a socket. Loops until either .stop() is set or data has been sent successfully. Exceptions for flow are handled in this functions, others are throw upwards. @param data The data you want to send """ with self._condition: s = str(data) result = win32.WriteFile( self._handle, s, len(s), None, self._overlapped_write) if not result: error = win32.GetLastError() if win32.ERROR_BROKEN_PIPE == error: raise ConnectionWriteException elif win32.ERROR_IO_PENDING == error: count = ctypes.wintypes.DWORD() result = win32.GetOverlappedResult( self._handle, ctypes.byref(self._overlapped_write), ctypes.byref(count), True) if not result: error = win32.GetLastError() if win32.ERROR_BROKEN_PIPE == error: raise ConnectionWriteException else: raise win32.create_WindowsError(error) else: raise win32.create_WindowsError(error)
def write(self, data): """ writes data over a socket. Loops until either .stop() is set or data has been sent successfully. Exceptions for flow are handled in this functions, others are throw upwards. @param data The data you want to send """ with self._condition: s = str(data) result = win32.WriteFile(self._handle, s, len(s), None, self._overlapped_write) if not result: error = win32.GetLastError() if win32.ERROR_BROKEN_PIPE == error: raise ConnectionWriteException elif win32.ERROR_IO_PENDING == error: count = ctypes.wintypes.DWORD() result = win32.GetOverlappedResult( self._handle, ctypes.byref(self._overlapped_write), ctypes.byref(count), True) if not result: error = win32.GetLastError() if win32.ERROR_BROKEN_PIPE == error: raise ConnectionWriteException else: raise win32.create_WindowsError(error) else: raise win32.create_WindowsError(error)
def _read_pending(self, count): while True: if self._stopped: return '' else: result = win32.WaitForSingleObject( self._overlapped_read.hEvent, _TIMEOUT) if win32.WAIT_TIMEOUT == result: continue elif win32.WAIT_OBJECT_0 == result: result = win32.GetOverlappedResult( self._handle, ctypes.byref(self._overlapped_read), ctypes.byref(count), True) if result: s = str(self._buffer[:count.value]) return s else: error = win32.GetLastError() if win32.ERROR_BROKEN_PIPE == error: return '' elif win32.ERROR_MORE_DATA == error: s = str(self._buffer[:count.value]) return s else: raise win32.create_WindowsError(error) else: raise ValueError(result)
def read(self): count = ctypes.wintypes.DWORD() result = win32.ReadFile( self._handle, self._buffer, _SIZE, ctypes.byref(count), ctypes.byref(self._overlapped_read)) if result: s = str(self._buffer[:count.value]) return s else: error = win32.GetLastError() if win32.ERROR_BROKEN_PIPE == error: return '' elif win32.ERROR_MORE_DATA == error: s = str(self._buffer[:count.value]) return s elif win32.ERROR_IO_PENDING == error: s = self._read_pending(count) return s else: raise win32.create_WindowsError(error)
def read(self): count = ctypes.wintypes.DWORD() result = win32.ReadFile(self._handle, self._buffer, _SIZE, ctypes.byref(count), ctypes.byref(self._overlapped_read)) if result: s = str(self._buffer[:count.value]) return s else: error = win32.GetLastError() if win32.ERROR_BROKEN_PIPE == error: return '' elif win32.ERROR_MORE_DATA == error: s = str(self._buffer[:count.value]) return s elif win32.ERROR_IO_PENDING == error: s = self._read_pending(count) return s else: raise win32.create_WindowsError(error)