2
2
import uselect as select
3
3
import usocket as _socket
4
4
from uasyncio .core import *
5
+ from sys import platform
6
+
7
+ def sock_fd (sock_ref ):
8
+ # Compute socket file descriptor replacement for esp8266
9
+ # sock_ref is a fd in most systems. esp8266 uses sockets instead
10
+ if isinstance (sock_ref , int ):
11
+ return sock_ref
12
+ return sock_ref .fileno () if platform != 'esp8266' else id (sock_ref )
5
13
6
14
7
15
class EpollEventLoop (EventLoop ):
@@ -16,33 +24,33 @@ def add_reader(self, fd, cb, *args):
16
24
log .debug ("add_reader%s" , (fd , cb , args ))
17
25
if args :
18
26
self .poller .register (fd , select .POLLIN )
19
- self .objmap [fd ] = (cb , args )
27
+ self .objmap [sock_fd ( fd ) ] = (cb , args )
20
28
else :
21
29
self .poller .register (fd , select .POLLIN )
22
- self .objmap [fd ] = cb
30
+ self .objmap [sock_fd ( fd ) ] = cb
23
31
24
32
def remove_reader (self , fd ):
25
33
if __debug__ :
26
34
log .debug ("remove_reader(%s)" , fd )
27
35
self .poller .unregister (fd )
28
- del self .objmap [fd ]
36
+ del self .objmap [sock_fd ( fd ) ]
29
37
30
38
def add_writer (self , fd , cb , * args ):
31
39
if __debug__ :
32
40
log .debug ("add_writer%s" , (fd , cb , args ))
33
41
if args :
34
42
self .poller .register (fd , select .POLLOUT )
35
- self .objmap [fd ] = (cb , args )
43
+ self .objmap [sock_fd ( fd ) ] = (cb , args )
36
44
else :
37
45
self .poller .register (fd , select .POLLOUT )
38
- self .objmap [fd ] = cb
46
+ self .objmap [sock_fd ( fd ) ] = cb
39
47
40
48
def remove_writer (self , fd ):
41
49
if __debug__ :
42
50
log .debug ("remove_writer(%s)" , fd )
43
51
try :
44
52
self .poller .unregister (fd )
45
- self .objmap .pop (fd , None )
53
+ self .objmap .pop (sock_fd ( fd ) , None )
46
54
except OSError as e :
47
55
# StreamWriter.awrite() first tries to write to an fd,
48
56
# and if that succeeds, yield IOWrite may never be called
@@ -61,7 +69,7 @@ def wait(self, delay):
61
69
res = self .poller .poll (delay , 1 )
62
70
#log.debug("epoll result: %s", res)
63
71
for fd , ev in res :
64
- cb = self .objmap [fd ]
72
+ cb = self .objmap [sock_fd ( fd ) ]
65
73
if __debug__ :
66
74
log .debug ("Calling IO callback: %r" , cb )
67
75
if isinstance (cb , tuple ):
0 commit comments