-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathev.pyx
316 lines (262 loc) · 8.26 KB
/
ev.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import signal
import traceback
import socket
from cpython cimport Py_INCREF, Py_DECREF
# a signal
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
cdef void on_signal(ev_loop_t *loop, ev_signal_t *self, int revents) except *:
try:
(<Loop>self.data).on_signal(revents)
except:
ev_unloop(loop, 1)
raise
# Loop
cdef class Loop(object):
def __init__(self):
self._loop = ev_default_loop(0)
ev_signal_init(&self.sigint_watcher, on_signal, signal.SIGINT)
self.sigint_watcher.data = <void *>self
ev_signal_start(self._loop, &self.sigint_watcher)
cpdef loop(self, handle_errors=False):
if handle_errors:
while True:
try:
ev_loop(self._loop, 0)
break
except KeyboardInterrupt:
break
except:
traceback.print_exc()
else:
ev_loop(self._loop, 0)
cpdef unloop(self):
ev_unloop(self._loop, 1)
cdef on_signal(self, revents):
raise KeyboardInterrupt()
# a timer
cdef void on_timer(ev_loop_t *loop, ev_timer_t *self, int revents) except *:
try:
(<Timer>self.data).on_timer(revents)
except:
ev_unloop(loop, 1)
raise
cdef class Timer(object):
def __init__(self, loop, callback, delay, repeat):
self.loop = loop
self.callback = callback
self.once = repeat <= 0
self.watcher.data = <void *>self
ev_timer_init(&self.watcher, on_timer, delay, repeat)
self.start()
def start(self):
Py_INCREF(self)
ev_timer_start(self.loop._loop, &self.watcher)
def stop(self):
ev_timer_stop(self.loop._loop, &self.watcher)
Py_DECREF(self)
cdef on_timer(self, revents):
try:
self.callback()
finally:
if self.once:
self.stop()
# a socket
cdef void on_async_socket_read(ev_loop_t *loop, ev_io_t *self, int revents) except *:
try:
sock = <AsyncSocket>self.data
sock.on_readable(revents)
except:
ev_unloop(loop, 1)
raise
cdef void on_async_socket_write(ev_loop_t *loop, ev_io_t *self, int revents) except *:
try:
(<AsyncSocket>self.data).on_writeable(revents)
except:
ev_unloop(loop, 1)
raise
cdef class AsyncSocket(object):
def __init__(self, loop, sock, server=None):
self.server = server
self.sock = sock
self.loop = loop
self.read_buffer = ''
self.write_buffer = ''
self.close_on_sent = False
self.reffed = True
Py_INCREF(self)
self.read_watcher.data = <void *>self
self.write_watcher.data = <void *>self
ev_io_init(&self.read_watcher, on_async_socket_read, sock.fileno(), EV_READ)
ev_io_init(&self.write_watcher, on_async_socket_write, sock.fileno(), EV_WRITE)
ev_io_start(self.loop._loop, &self.read_watcher)
def close(self):
ev_io_stop(self.loop._loop, &self.read_watcher)
ev_io_stop(self.loop._loop, &self.write_watcher)
self.read_watcher.data = NULL
self.write_watcher.data = NULL
try:
self.sock.close()
except:
pass
self.on_close()
if self.reffed:
self.reffed = False
Py_DECREF(self)
def flush_and_close(self):
if not self.write_buffer:
self.close()
else:
self.close_on_sent = True
def send(self, data):
if not data:
return
self.write_buffer += data
ev_io_start(self.loop._loop, &self.write_watcher)
cdef on_readable(self, int revents):
data = self.sock.recv(10240)
if not data:
self.close()
else:
self.read_buffer += data
self.on_read()
cdef on_writeable(self, revents):
sent = self.sock.send(self.write_buffer)
if sent < 0:
self.close()
else:
self.write_buffer = self.write_buffer[sent:]
if not self.write_buffer:
ev_io_stop(self.loop._loop, &self.write_watcher)
if self.close_on_sent:
self.close()
def on_read(self):
pass
def on_close(self):
pass
# a server
cdef void on_async_server_read(ev_loop_t *loop, ev_io_t *self, int revents) except *:
try:
(<AsyncServer>self.data).on_readable(revents)
except:
ev_unloop(loop, 1)
raise
cdef class AsyncServer(object):
def __init__(self, loop, sock, Client):
if isinstance(sock, tuple):
loc = sock
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(loc)
sock.listen(5)
self.loop = loop
self.sock = sock
self.Client = Client
Py_INCREF(self)
self.read_watcher.data = <void *>self
ev_io_init(&self.read_watcher, on_async_server_read, sock.fileno(), EV_READ)
ev_io_start(self.loop._loop, &self.read_watcher)
def close(self):
ev_io_stop(self.loop._loop, &self.read_watcher)
self.read_watcher.data = NULL
try:
self.sock.close()
except:
pass
Py_DECREF(self)
cdef on_readable(self, int revents):
try:
sock, addr = self.sock.accept()
self.Client(self.loop, sock, addr, self)
except:
import traceback
traceback.print_exc()
# file reader
cdef void on_file_reader_read(ev_loop_t *loop, ev_io_t *self, int revents) except *:
try:
(<FileReader>self.data).on_readable(revents)
except:
ev_unloop(loop, 1)
raise
cdef class FileReader(object):
def __init__(self, loop, file):
self.loop = loop
self.file = file
self.read_buffer = ''
self.reffed = True
Py_INCREF(self)
self.read_watcher.data = <void *>self
ev_io_init(&self.read_watcher, on_file_reader_read, file.fileno(), EV_READ)
ev_io_start(self.loop._loop, &self.read_watcher)
def close(self):
ev_io_stop(self.loop._loop, &self.read_watcher)
self.read_watcher.data = NULL
try:
self.file.close()
except:
pass
self.on_close()
if self.reffed:
self.reffed = False
Py_DECREF(self)
cdef on_readable(self, int revents):
data = self.file.read(10240)
if not data:
self.close()
else:
self.read_buffer += data
self.on_read()
def on_read(self):
pass
def on_close(self):
pass
# file writer
cdef void on_file_writer_write(ev_loop_t *loop, ev_io_t *self, int revents) except *:
try:
(<FileWriter>self.data).on_writeable(revents)
except:
ev_unloop(loop, 1)
raise
cdef class FileWriter(object):
def __init__(self, loop, file, data=''):
self.loop = loop
self.file = file
self.write_buffer = ''
self.close_on_sent = False
self.reffed = True
Py_INCREF(self)
self.write_watcher.data = <void *>self
ev_io_init(&self.write_watcher, on_file_writer_write, file.fileno(), EV_WRITE)
self.write(data)
def close(self):
ev_io_stop(self.loop._loop, &self.write_watcher)
self.write_watcher.data = NULL
try:
self.file.close()
except:
pass
self.on_close()
if self.reffed:
self.reffed = False
Py_DECREF(self)
def flush_and_close(self):
if not self.write_buffer:
self.close()
else:
self.close_on_sent = True
def write(self, data):
if not data:
return
self.write_buffer += data
ev_io_start(self.loop._loop, &self.write_watcher)
cdef on_writeable(self, int revents):
sent = self.file.write(self.write_buffer)
if sent < 0:
self.close()
else:
self.write_buffer = self.write_buffer[sent:]
if not self.write_buffer:
ev_io_stop(self.loop._loop, &self.write_watcher)
if self.close_on_sent:
self.close()
def on_close(self):
pass