-
Notifications
You must be signed in to change notification settings - Fork 2
/
e2reactor.py
205 lines (172 loc) · 5.14 KB
/
e2reactor.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
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
# enigma2 reactor: based on pollreactor, which is
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Maintainer: U{Felix Domke<mailto:[email protected]>}
"""
# System imports
import select, errno, sys
# Twisted imports
from twisted.python import log, failure
from twisted.internet import main, posixbase, error
#from twisted.internet.pollreactor import PollReactor, poller
from enigma import getApplication
# globals
reads = {}
writes = {}
selectables = {}
POLL_DISCONNECTED = (select.POLLHUP | select.POLLERR | select.POLLNVAL)
class E2SharedPoll:
def __init__(self):
self.dict = { }
self.eApp = getApplication()
def register(self, fd, eventmask = select.POLLIN | select.POLLERR | select.POLLOUT):
self.dict[fd] = eventmask
def unregister(self, fd):
del self.dict[fd]
def poll(self, timeout = None):
try:
r = self.eApp.poll(timeout, self.dict)
except KeyboardInterrupt:
return None
return r
poller = E2SharedPoll()
class PollReactor(posixbase.PosixReactorBase):
"""A reactor that uses poll(2)."""
def _updateRegistration(self, fd):
"""Register/unregister an fd with the poller."""
try:
poller.unregister(fd)
except KeyError:
pass
mask = 0
if reads.has_key(fd): mask = mask | select.POLLIN
if writes.has_key(fd): mask = mask | select.POLLOUT
if mask != 0:
poller.register(fd, mask)
else:
if selectables.has_key(fd): del selectables[fd]
poller.eApp.interruptPoll()
def _dictRemove(self, selectable, mdict):
try:
# the easy way
fd = selectable.fileno()
# make sure the fd is actually real. In some situations we can get
# -1 here.
mdict[fd]
except:
# the hard way: necessary because fileno() may disappear at any
# moment, thanks to python's underlying sockets impl
for fd, fdes in selectables.items():
if selectable is fdes:
break
else:
# Hmm, maybe not the right course of action? This method can't
# fail, because it happens inside error detection...
return
if mdict.has_key(fd):
del mdict[fd]
self._updateRegistration(fd)
def addReader(self, reader):
"""Add a FileDescriptor for notification of data available to read.
"""
fd = reader.fileno()
if not reads.has_key(fd):
selectables[fd] = reader
reads[fd] = 1
self._updateRegistration(fd)
def addWriter(self, writer, writes=writes, selectables=selectables):
"""Add a FileDescriptor for notification of data available to write.
"""
fd = writer.fileno()
if not writes.has_key(fd):
selectables[fd] = writer
writes[fd] = 1
self._updateRegistration(fd)
def removeReader(self, reader, reads=reads):
"""Remove a Selectable for notification of data available to read.
"""
return self._dictRemove(reader, reads)
def removeWriter(self, writer, writes=writes):
"""Remove a Selectable for notification of data available to write.
"""
return self._dictRemove(writer, writes)
def removeAll(self, reads=reads, writes=writes, selectables=selectables):
"""Remove all selectables, and return a list of them."""
if self.waker is not None:
self.removeReader(self.waker)
result = selectables.values()
fds = selectables.keys()
reads.clear()
writes.clear()
selectables.clear()
for fd in fds:
poller.unregister(fd)
if self.waker is not None:
self.addReader(self.waker)
return result
def doPoll(self, timeout,
reads=reads,
writes=writes,
selectables=selectables,
select=select,
log=log,
POLLIN=select.POLLIN,
POLLOUT=select.POLLOUT):
"""Poll the poller for new events."""
if timeout is not None:
timeout = int(timeout * 1000) # convert seconds to milliseconds
try:
l = poller.poll(timeout)
if l is None:
if self.running:
self.stop()
l = [ ]
except select.error, e:
if e[0] == errno.EINTR:
return
else:
raise
_drdw = self._doReadOrWrite
for fd, event in l:
try:
selectable = selectables[fd]
except KeyError:
# Handles the infrequent case where one selectable's
# handler disconnects another.
continue
log.callWithLogger(selectable, _drdw, selectable, fd, event, POLLIN, POLLOUT, log)
doIteration = doPoll
def _doReadOrWrite(self, selectable, fd, event, POLLIN, POLLOUT, log,
faildict={
error.ConnectionDone: failure.Failure(error.ConnectionDone()),
error.ConnectionLost: failure.Failure(error.ConnectionLost())
}):
why = None
inRead = False
if event & POLL_DISCONNECTED and not (event & POLLIN):
why = main.CONNECTION_LOST
else:
try:
if event & POLLIN:
why = selectable.doRead()
inRead = True
if not why and event & POLLOUT:
why = selectable.doWrite()
inRead = False
if not selectable.fileno() == fd:
why = error.ConnectionFdescWentAway('Filedescriptor went away')
inRead = False
except:
log.deferr()
why = sys.exc_info()[1]
if why:
self._disconnectSelectable(selectable, why, inRead)
def callLater(self, *args, **kwargs):
poller.eApp.interruptPoll()
return posixbase.PosixReactorBase.callLater(self, *args, **kwargs)
def install():
"""Install the poll() reactor."""
p = PollReactor()
main.installReactor(p)
__all__ = ["PollReactor", "install"]