-
Notifications
You must be signed in to change notification settings - Fork 221
/
ioloop.py
174 lines (146 loc) · 5.51 KB
/
ioloop.py
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
__author__ = 'xsank'
import select
import socket
import errno
import logging
from threading import Thread
from utils import Platform
MAX_DATA_BUFFER = 1024*1024
class IOLoop(Thread):
READ = 0x001
WRITE = 0x004
ERROR = 0x008 | 0x010
def __init__(self, impl):
super(IOLoop, self).__init__()
self.daemon = True
self.impl = impl
self.bridges = {}
self.futures = {}
@staticmethod
def instance():
if not hasattr(IOLoop, "_instance"):
if Platform.is_linux():
IOLoop._instance = EPollIOLoop()
elif Platform.is_mac():
IOLoop._instance = KQueueIOLoop()
else:
IOLoop._instance = SelectIOLoop()
return IOLoop._instance
def register(self, bridge):
raise NotImplemented("the register method should be implemented")
def _add_bridge(self, bridge):
self.bridges[bridge.id] = bridge
def add_future(self, future):
fd = next(future)
self.futures[fd] = future
next(future)
def close(self, fd):
self.impl.unregister(fd)
self.bridges[fd].detroy()
del self.bridges[fd]
class EPollIOLoop(IOLoop):
def __init__(self):
super(EPollIOLoop, self).__init__(impl=select.epoll())
def register(self, bridge):
self._add_bridge(bridge)
self.impl.register(bridge.id, select.EPOLLIN | select.EPOLLET)
def run(self):
while True:
poll_list = self.impl.poll()
for fd, events in poll_list:
if select.EPOLLIN & events:
while True:
try:
data = self.bridges[fd].shell.recv(MAX_DATA_BUFFER)
except socket.error as e:
if e.errno == errno.EAGAIN:
self.impl.modify(fd, select.EPOLLET)
elif isinstance(e, socket.timeout):
break
else:
self.close(fd)
try:
self.futures[fd].send(data)
except StopIteration:
break
elif select.EPOLLHUP & events:
self.close(fd)
else:
continue
class SelectIOLoop(IOLoop):
def __init__(self):
super(SelectIOLoop, self).__init__(impl=select.select)
self.read_fds = set()
self.write_fds = set()
self.error_fds = set()
self.fd_sets = (self.read_fds, self.write_fds, self.error_fds)
def register(self, bridge):
self._add_bridge(bridge)
self.read_fds.add(bridge.id)
def run(self):
import time
while True:
if self.read_fds:
readable, writeable, errors = self.impl(
self.read_fds, self.write_fds, self.error_fds, 1)
events = {}
for fd in readable:
events[fd] = events.get(fd, 0) | self.READ
for fd in errors:
events[fd] = events.get(fd, 0) | self.ERROR
for fd, events in events.items():
if self.READ & events:
while True:
try:
data = self.bridges[fd].shell.recv(MAX_DATA_BUFFER)
except socket.error as e:
if isinstance(e, socket.timeout):
break
else:
self.close(fd)
try:
self.futures[fd].send(data)
except StopIteration:
break
elif self.ERROR & events:
self.close(fd)
else:
continue
else:
time.sleep(1)
class KQueueIOLoop(IOLoop):
def __init__(self):
super(KQueueIOLoop, self).__init__(impl=select.kqueue())
def register(self, bridge):
self._add_bridge(bridge)
kevent = select.kevent(
bridge.id, filter=select.KQ_FILTER_READ, flags=select.KQ_EV_ADD)
self.impl.control([kevent], 0)
def run(self):
while True:
kevents = self.impl.control(None, 1000, 1)
events = {}
for kevent in kevents:
fd = kevent.ident
if kevent.filter == select.KQ_FILTER_READ:
events[fd] = events.get(fd, 0) | self.READ
if kevent.flags & select.KQ_EV_ERROR:
events[fd] = events.get(fd, 0) | self.ERROR
for fd, events in events.items():
if select.KQ_FILTER_READ & events:
while True:
try:
data = self.bridges[fd].shell.recv(MAX_DATA_BUFFER)
except socket.error as e:
if isinstance(e, socket.timeout):
break
else:
self.close(fd)
try:
self.futures[fd].send(data)
except StopIteration:
break
elif select.KQ_EV_ERROR & events:
self.close(fd)
else:
continue