def do_run(self): pipe_handle = None while not self.exit_loop: if not pipe_handle: try: pipe_handle = self.CreatePipeHandle() except Exception as e: log("CreatePipeHandle()", exc_info=True) log.error("Error: failed to create named pipe") log.error(" at path '%s'", self.pipe_name) log.error(" %s", e) return log("CreatePipeHandle()=%#x", pipe_handle) if c_long(pipe_handle).value==INVALID_HANDLE_VALUE: log.error("Error: invalid handle for named pipe '%s'", self.pipe_name) e = GetLastError() log.error(" '%s' (%i)", FormatMessageSystem(e).rstrip("\n\r."), e) return event = CreateEventA(None, True, False, None) overlapped = OVERLAPPED() overlapped.hEvent = event overlapped.Internal = None overlapped.InternalHigh = None overlapped.union.Pointer = None r = ConnectNamedPipe(pipe_handle, overlapped) log("ConnectNamedPipe()=%s", r) if self.exit_loop: break if r==0: e = GetLastError() log("GetLastError()=%s (%i)", FormatMessageSystem(e).rstrip("\n\r"), e) if e==ERROR_PIPE_CONNECTED: pass elif e==ERROR_IO_PENDING: while not self.exit_loop: r = WaitForSingleObject(event, INFINITE) log("WaitForSingleObject(..)=%s", WAIT_STR.get(r, r)) if r==WAIT_TIMEOUT: continue if r==0: break log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) log.error(" %s", WAIT_STR.get(r, r)) CloseHandle(pipe_handle) pipe_handle = None break else: log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) log.error(" error %s", e) CloseHandle(pipe_handle) pipe_handle = None if self.exit_loop: break #from now on, the pipe_handle will be managed elsewhere: if pipe_handle: self.new_connection_cb("named-pipe", self, pipe_handle) pipe_handle = None if pipe_handle: self.close_handle(pipe_handle)
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, 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_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
def do_run(self): while not self.exit_loop: pipe_handle = None try: pipe_handle = self.CreatePipeHandle() except Exception as e: log("CreatePipeHandle()", exc_info=True) log.error("Error: failed to create named pipe") log.error(" at path '%s'", self.pipe_name) log.error(" %s", e) return log("CreatePipeHandle()=%s", pipe_handle) if pipe_handle == INVALID_HANDLE_VALUE: log.error("Error: invalid handle for named pipe '%s'", self.pipe_name) return event = CreateEventA(None, True, False, None) overlapped = OVERLAPPED() overlapped.hEvent = event overlapped.Internal = None overlapped.InternalHigh = None overlapped.union.Pointer = None r = ConnectNamedPipe(pipe_handle, overlapped) log("ConnectNamedPipe()=%s", r) if not r and not self.exit_loop: r = WaitForSingleObject(event, INFINITE) log("WaitForSingleObject(..)=%s", WAIT_STR.get(r, r)) if r: log("do_run() error on WaitForSingleObject", exc_info=True) log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) CloseHandle(pipe_handle) continue if self.exit_loop: CloseHandle(pipe_handle) break if r == 0 and False: e = GetLastError() if e == ERROR_PIPE_CONNECTED: pass else: log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) log.error(" error %s", e) CloseHandle(pipe_handle) continue #from now on, the pipe_handle will be managed elsewhere: self.new_connection_cb(self, pipe_handle)