def _pipe_read(self, buf): read = c_ulong(0) r = ReadFile(self.pipe_handle, self.read_buffer_ptr, BUFSIZE, byref(read), byref(self.read_overlapped)) log("ReadFile(..)=%i, len=%s", r, read.value) if not r and self.pipe_handle: e = GetLastError() if e != ERROR_IO_PENDING: log("ReadFile: %s", IO_ERROR_STR.get(e, e)) r = WaitForSingleObject(self.read_event, INFINITE) log("WaitForSingleObject(..)=%s, len=%s", WAIT_STR.get(r, r), read.value) if r and self.pipe_handle: raise Exception("failed to read from named pipe handle %s" % self.pipe_handle) if self.pipe_handle: if not GetOverlappedResult(self.pipe_handle, byref(self.read_overlapped), byref(read), False): e = GetLastError() if e != ERROR_BROKEN_PIPE: raise Exception("overlapped read failed: %s" % IO_ERROR_STR.get(e, e)) if read.value == 0: data = None else: data = string_at(self.read_buffer_ptr, read.value) log("pipe_read: %i bytes", len(data or "")) #, binascii.hexlify(s)) return data
def _pipe_write(self, buf): size = len(buf) log("pipe_write: %i bytes", size) #binascii.hexlify(buf)) written = c_ulong(0) r = WriteFile(self.pipe_handle, c_char_p(buf), len(buf), byref(written), byref(self.write_overlapped)) log("WriteFile(..)=%s, len=%i", r, written.value) if not r and self.pipe_handle: e = GetLastError() if e != ERROR_IO_PENDING: log("WriteFile: %s", IO_ERROR_STR.get(e, e)) r = WaitForSingleObject(self.write_event, INFINITE) log("WaitForSingleObject(..)=%s, len=%i", WAIT_STR.get(r, r), written.value) if not self.pipe_handle: #closed already! return written.value if r: raise Exception( "failed to write buffer to named pipe handle %s" % self.pipe_handle) if self.pipe_handle: if not GetOverlappedResult(self.pipe_handle, byref(self.write_overlapped), byref(written), False): e = GetLastError() raise Exception("overlapped write failed: %s" % IO_ERROR_STR.get(e, e)) log("pipe_write: %i bytes written", written.value) if self.pipe_handle: FlushFileBuffers(self.pipe_handle) #SetFilePointer(self.pipe_handle, 0, FILE_BEGIN) return written.value
def _pipe_read(self, n): read = c_ulong(0) n_bytes = DWORD(min(n, BUFSIZE)) r = ReadFile(self.pipe_handle, self.read_buffer_ptr, n_bytes, byref(read), byref(self.read_overlapped)) log("ReadFile(%i)=%i, len=%s", n, r, read.value) if not r and self.pipe_handle: e = GetLastError() if e!=ERROR_IO_PENDING: log("ReadFile: %s", IO_ERROR_STR.get(e, e)) if e in CONNECTION_CLOSED_ERRORS: raise ConnectionClosedException(CONNECTION_CLOSED_ERRORS[e]) r = WaitForSingleObject(self.read_event, INFINITE) log("WaitForSingleObject(..)=%s, len=%s", WAIT_STR.get(r, r), read.value) if r and self.pipe_handle: raise Exception("failed to read from named pipe handle %s" % self.pipe_handle) if self.pipe_handle: if not GetOverlappedResult(self.pipe_handle, byref(self.read_overlapped), byref(read), False): e = GetLastError() if e in CONNECTION_CLOSED_ERRORS: raise ConnectionClosedException(CONNECTION_CLOSED_ERRORS[e]) raise Exception("overlapped read failed: %s" % IO_ERROR_STR.get(e, e)) if read.value==0: data = None else: data = string_at(self.read_buffer_ptr, read.value) log("pipe_read: %i bytes", len(data or "")) #, binascii.hexlify(s)) return data