qmsk/net/lib/event2/event.pyx
changeset 47 b45a6648931c
parent 46 64b4ffb44754
parent 42 0ff56f7216ee
child 50 da394bb715af
equal deleted inserted replaced
46:64b4ffb44754 47:b45a6648931c
     7         NULL.
     7         NULL.
     8     """
     8     """
     9 
     9 
    10     cdef double t
    10     cdef double t
    11 
    11 
    12     if timeout :
    12     if timeout is not None :
    13         t = timeout
    13         t = timeout
    14 
    14 
    15         tv.tv_sec = <int>(t)
    15         tv.tv_sec = <int>(t)
    16         tv.tv_usec = <int>((t - <int> t) * 100000)
    16         tv.tv_usec = <int>((t - <int> t) * 100000)
    17         
    17         
    37     ev(fd, mask)
    37     ev(fd, mask)
    38 
    38 
    39 
    39 
    40 cdef class event :
    40 cdef class event :
    41 
    41 
    42     def __init__ (self, event_base base, lib.evutil_socket_t fd, short events) :
    42     def __init__ (self, event_base base not None, lib.evutil_socket_t fd, short events) :
       
    43 
       
    44         # libevent seems to segfault otherwise...
       
    45         if events & (lib.EV_READ | lib.EV_WRITE) and fd < 0 :
       
    46             raise ValueError(fd)
    43 
    47 
    44         # construct event using our internal callback, with self as the arg
    48         # construct event using our internal callback, with self as the arg
    45         self.ev = lib.event_new(base.ev_base, fd, events, ev_callback, <void *>self)
    49         self.ev = lib.event_new(base.ev_base, fd, events, ev_callback, <void *>self)
    46 
    50 
    47         if self.ev == NULL :
    51         if self.ev == NULL :
    48             raise Exception("event_new")
    52             raise Exception("event_new")
    49     
    53     
    50 
    54 
    51     def assign (self, event_base base, lib.evutil_socket_t fd, short events) :
    55     def assign (self, event_base base not None, lib.evutil_socket_t fd, short events) :
    52         """
    56         """
    53             Re-assign our event parameters to the given ones.
    57             Re-assign our event parameters to the given ones.
    54 
    58 
    55             It is illegal to call this method after calling .add() but before del()/callback!
    59             It is illegal to call this method after calling .add() but before del()/callback!
    56         """
    60         """
    96         """
   100         """
    97 
   101 
    98         lib.event_active(self.ev, fd, mask)
   102         lib.event_active(self.ev, fd, mask)
    99 
   103 
   100 
   104 
   101     def pending (self, short mask) :
   105     def pending (self, short mask = 0xffff) :
   102         """
   106         """
   103             Returns a bool indicating if this event is pending (that is, has been .add()'d).
   107             Returns a bool indicating if this event is pending (that is, has been .add()'d).
       
   108 
       
   109             For convenience, this defaults to testing all possible flags, and so will return true if any events are
       
   110             pending.
   104 
   111 
   105             XXX: support returning timeout value?
   112             XXX: support returning timeout value?
   106         """
   113         """
   107 
   114 
   108         return bool(lib.event_pending(self.ev, mask, NULL))
   115         return bool(lib.event_pending(self.ev, mask, NULL))
   117             return lib.event_get_fd(self.ev)
   124             return lib.event_get_fd(self.ev)
   118 
   125 
   119 
   126 
   120     def __call__ (self, lib.evutil_socket_t fd, short mask) :
   127     def __call__ (self, lib.evutil_socket_t fd, short mask) :
   121         """
   128         """
   122             The method invoked by the internal libevent callback.
   129             The method invoked by the internal libevent callback when the event becomes active.
       
   130             
       
   131                 fd          - OS file descriptor the event occured on, or -1
       
   132                 mask        - bitmask of EV_* flags that represents the triggered event
       
   133 
       
   134 
       
   135             The default implementation of __call__ does nothing. The method's return value will be ignored, and should
       
   136             be None. Any errors raised by the callback will be printed out as warning messages, and ignored.
   123         """
   137         """
   124 
   138 
   125         pass
   139         pass
   126 
   140 
   127 
   141 
   129         """
   143         """
   130             Release the event object usign event_free. This should be completely safe as regards out event_base.
   144             Release the event object usign event_free. This should be completely safe as regards out event_base.
   131 
   145 
   132             XXX: what happens if event_base's __dealloc__ is triggered, but there are still event objects alive?
   146             XXX: what happens if event_base's __dealloc__ is triggered, but there are still event objects alive?
   133         """
   147         """
   134 
   148         
   135         lib.event_free(self.ev)
   149         if self.ev != NULL :
       
   150             lib.event_free(self.ev)
   136 
   151 
   137 class CallbackEvent (event) :
   152 class CallbackEvent (event) :
   138     """
   153     """
   139         Extends the event type to take a callback and additional arguments to invoke from the __call__ method.
   154         Extends the event type to take a callback and additional arguments to invoke from the __call__ method.
   140     """
   155     """