示例#1
0
 def fill_pending__(self, timeout):
     """
     wait for the next event to happen.
     respect timeout specified on each channel
     """
     if dbg>=2: LOG("%s\n" % self)
     # to = 
     if timeout == float("inf"):
         timeout_ms = win32event.INFINITE
     else:
         timeout_ms = int(timeout * 1.0E3)
     if dbg>=2: LOG("%s WaitForMultipleObjects(%s)\n" % (self, self.handles))
     x = self.nointr_wait_for_multiple_object(self.handles, 0, timeout_ms)
     o0 = win32event.WAIT_OBJECT_0
     assert (o0 <= x < o0 + len(self.handles) \
                 or x == win32event.WAIT_TIMEOUT), \
         (o0, x, len(self.handles))
     if x == win32event.WAIT_TIMEOUT: return
     h = self.handles[x - o0]
     ch = self.channels[h]
     if dbg>=2: LOG("-> handle=%s ch=%s\n" % (h, ch))
     if isinstance(ch, chan_win_process):
         status,err = ch.do_io()
         assert (err is None), err
         self.pending.append(ioman_base.event_death(ch.pid, status))
     elif isinstance(ch, chan_win_server_socket):
         new_ch,err = ch.do_io()
         assert (err is None), err
         self.pending.append(ioman_base.event_accept(ch, new_ch, err))
     elif isinstance(ch, chan_win_handle_read):
         data,err = ch.do_read()
         eof = 0
         if data == "" or err:
             eof = 1
         self.pending.append(ioman_base.event_read(ch, data, eof, err))
     elif isinstance(ch, chan_win_handle_write):
         pending_bytes,pending_close,err = ch.do_write()
         self.pending.append(ioman_base.event_write(ch, pending_bytes,
                                                  pending_close, err))
     elif isinstance(ch, chan_win_socket):
         n_events = 0       # make sure we get at least one event
         if ch.read_wouldnt_block():
             data,err = ch.do_read()
             eof = 0
             if data == "" or err:
                 eof = 1
             n_events = n_events + 1
             self.pending.append(ioman_base.event_read(ch, data, eof, err))
         if ch.requests and ch.so and ch.write_wouldnt_block():
             pending_bytes,pending_close,err = ch.do_write()
             n_events = n_events + 1
             self.pending.append(ioman_base.event_write(ch, pending_bytes,
                                                      pending_close, err))
     elif isinstance(ch, chan_win_ctrlc):
         ch.reset()
         self.pending.append(ioman_base.event_win_ctrlc(ch))
     else:
         raise ioman_win_bug()
示例#2
0
 def fill_pending__(self, to):
     """
     wait for the next event to happen.
     respect timeout specified on each channel
     """
     if dbg>=3: LOG("\n")
     if to is None or to == float("inf"):
         events = no_intr(self.po.poll, ())
     else:
         events = no_intr(self.po.poll, (int(to * 1000.0),))
     for fd,event in events:
         if dbg>=3:
             LOG("fd %d got event %s\n" % (fd, self.pp_event__(event)))
         ch = self.channels[fd]
         if event & self.events_error:
             assert (event & (self.events_out | self.events_in)), (fd, event)
         if event & self.events_out:
             pending_bytes,pending_close,err = ch.do_write()
             self.pending.append(ioman_base.event_write(ch, pending_bytes, pending_close, err))
         if event & (self.events_in | self.events_hup):
             if ch is self.child_watch:
                 assert (event & self.events_hup) == 0
                 assert (event & self.events_error) == 0
                 data,err = ch.do_read()
                 assert len(data) > 0
                 assert err is None
                 for pid,st in self.reap_children__():
                     self.pending.append(ioman_base.event_death(pid, st))
             elif isinstance(ch, chan_unix_server_socket):
                 assert (event & self.events_hup) == 0
                 assert (event & self.events_error) == 0
                 new_ch,err = ch.do_read()
                 self.pending.append(ioman_base.event_accept(ch, new_ch, err))
             elif event & self.events_hup:
                 # read until we get EOF or error
                 c = cStringIO.StringIO()
                 while 1:
                     data,err = ch.do_read()
                     # I thought hup means EOF, but for sockets, I got
                     # hup after connection reset by peers error
                     # assert (err is None), err
                     if err:
                         assert (data is None)
                         break
                     elif data == "": 
                         break
                     c.write(data)
                 data = c.getvalue()
                 eof = 1
                 self.pending.append(ioman_base.event_read(ch, data, eof, err))
             else:
                 data,err = ch.do_read()
                 if data == "" or err:
                     eof = 1
                 else:
                     eof = 0
                 self.pending.append(ioman_base.event_read(ch, data, eof, err))