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 __init__(self, name, pipe_handle): log("NamedPipeConnection(%s, %#x)", name, pipe_handle) Connection.__init__(self, name, "named-pipe") self.pipe_handle = pipe_handle self.read_buffer = (c_char * BUFSIZE)() self.read_buffer_ptr = cast(addressof(self.read_buffer), c_void_p) self.read_event = CreateEventA(None, True, False, None) self.read_overlapped = OVERLAPPED() self.read_overlapped.hEvent = self.read_event self.read_overlapped.Internal = None self.read_overlapped.InternalHigh = None self.read_overlapped.union.Pointer = None self.write_event = CreateEventA(None, True, False, None) self.write_overlapped = OVERLAPPED() self.write_overlapped.hEvent = self.write_event self.write_overlapped.Internal = None self.write_overlapped.InternalHigh = None self.write_overlapped.union.Pointer = None
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)