39
|
1 |
from qmsk.net.lib.event2.event cimport *
|
|
2 |
from qmsk.net.lib.event2.base cimport event_base
|
|
3 |
|
|
4 |
cdef lib.timeval* build_timeout (lib.timeval *tv, object timeout = None) except? <lib.timeval *>-1 :
|
|
5 |
"""
|
|
6 |
If a timeout is given, it is treated as a float and stored in *tv, which is returned. Otherwise, simply returns
|
|
7 |
NULL.
|
|
8 |
"""
|
|
9 |
|
|
10 |
cdef double t
|
|
11 |
|
|
12 |
if timeout :
|
|
13 |
t = timeout
|
|
14 |
|
|
15 |
tv.tv_sec = <int>(t)
|
|
16 |
tv.tv_usec = <int>((t - <int> t) * 100000)
|
|
17 |
|
|
18 |
return tv
|
|
19 |
|
|
20 |
else :
|
|
21 |
return NULL
|
|
22 |
|
|
23 |
|
|
24 |
# our shared callback handler
|
|
25 |
# this must aquire the GIL before invoking the python object
|
|
26 |
# this means that the libevent loop *must* be run with the GIL released!
|
|
27 |
cdef void ev_callback (lib.evutil_socket_t fd, short mask, void *arg) with gil :
|
|
28 |
"""
|
|
29 |
Proxy event back to event object passed as arg
|
|
30 |
"""
|
|
31 |
|
|
32 |
# unpack opaque arg
|
|
33 |
cdef event ev = <event>arg
|
|
34 |
|
|
35 |
# invoke
|
|
36 |
# XXX: logging?
|
|
37 |
ev(fd, mask)
|
|
38 |
|
|
39 |
|
|
40 |
cdef class event :
|
|
41 |
|
|
42 |
def __init__ (self, event_base base, lib.evutil_socket_t fd, short events) :
|
|
43 |
|
|
44 |
# 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)
|
|
46 |
|
|
47 |
if self.ev == NULL :
|
|
48 |
raise Exception("event_new")
|
|
49 |
|
|
50 |
|
|
51 |
def assign (self, event_base base, lib.evutil_socket_t fd, short events) :
|
|
52 |
"""
|
|
53 |
Re-assign our event parameters to the given ones.
|
|
54 |
|
|
55 |
It is illegal to call this method after calling .add() but before del()/callback!
|
|
56 |
"""
|
|
57 |
|
|
58 |
# interesting... no error return code
|
|
59 |
lib.event_assign(self.ev, base.ev_base, fd, events, ev_callback, <void *>self)
|
|
60 |
|
|
61 |
|
|
62 |
def add (self, timeout = None) :
|
|
63 |
"""
|
|
64 |
Add this event to our event_base's set of monitored events.
|
|
65 |
|
|
66 |
This event will be executed if any of the events specified in 'events' occurs, or the given timeout
|
|
67 |
expires. If no timeout is given, no timeout will occur.
|
|
68 |
|
|
69 |
If this event already has a shceduled timeout, the old timeout will be replaced by the new one.
|
|
70 |
|
|
71 |
timeout - a float representing the timeout length
|
|
72 |
|
|
73 |
XXX: "The event in the ev argument must be already initialized by event_set() and may not be used in calls to event_set() until it has timed out or been removed with event_del()" -- enforce
|
|
74 |
"""
|
|
75 |
|
|
76 |
cdef lib.timeval tv
|
|
77 |
|
|
78 |
if lib.event_add(self.ev, build_timeout(&tv, timeout)) < 0 :
|
|
79 |
raise Exception("event_new")
|
|
80 |
|
|
81 |
|
|
82 |
def delete (self) :
|
|
83 |
"""
|
|
84 |
Remove this event from our event_base's set of monitored events.
|
|
85 |
|
|
86 |
If this event has already executed or has never been added this call with have no effect.
|
|
87 |
"""
|
|
88 |
|
|
89 |
if lib.event_del(self.ev) < 0 :
|
|
90 |
raise Exception("event_del")
|
|
91 |
|
|
92 |
|
|
93 |
def activate (self, int fd, short mask) :
|
|
94 |
"""
|
|
95 |
'Fake' this event as active, triggering the callback.
|
|
96 |
"""
|
|
97 |
|
|
98 |
lib.event_active(self.ev, fd, mask)
|
|
99 |
|
|
100 |
|
|
101 |
def pending (self, short mask) :
|
|
102 |
"""
|
|
103 |
Returns a bool indicating if this event is pending (that is, has been .add()'d).
|
|
104 |
|
|
105 |
XXX: support returning timeout value?
|
|
106 |
"""
|
|
107 |
|
|
108 |
return bool(lib.event_pending(self.ev, mask, NULL))
|
|
109 |
|
|
110 |
|
|
111 |
property fd :
|
|
112 |
def __get__ (self) :
|
|
113 |
"""
|
|
114 |
Get the OS file descriptor associated with this event.
|
|
115 |
"""
|
|
116 |
|
|
117 |
return lib.event_get_fd(self.ev)
|
|
118 |
|
|
119 |
|
|
120 |
def __call__ (self, lib.evutil_socket_t fd, short mask) :
|
|
121 |
"""
|
|
122 |
The method invoked by the internal libevent callback.
|
|
123 |
"""
|
|
124 |
|
|
125 |
pass
|
|
126 |
|
|
127 |
|
|
128 |
def __dealloc__ (self) :
|
|
129 |
"""
|
|
130 |
Release the event object usign event_free. This should be completely safe as regards out event_base.
|
|
131 |
|
|
132 |
XXX: what happens if event_base's __dealloc__ is triggered, but there are still event objects alive?
|
|
133 |
"""
|
|
134 |
|
|
135 |
lib.event_free(self.ev)
|
|
136 |
|