1 # -*- Mode: Python; tab-width: 4 -*-
3 # Author: Sam Rushing <rushing@nightmare.com>
5 # ======================================================================
6 # Copyright 1996 by Sam Rushing
10 # Permission to use, copy, modify, and distribute this software and
11 # its documentation for any purpose and without fee is hereby
12 # granted, provided that the above copyright notice appear in all
13 # copies and that both that copyright notice and this permission
14 # notice appear in supporting documentation, and that the name of Sam
15 # Rushing not be used in advertising or publicity pertaining to
16 # distribution of the software without specific, written prior
19 # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
21 # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22 # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
23 # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
24 # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
25 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 # ======================================================================
41 from errno
import EALREADY
, EINPROGRESS
, EWOULDBLOCK
, ECONNRESET
, ENOTCONN
45 def poll (timeout
=0.0, ignore_expt
=1):
47 sockets
= socket_map
.keys()
48 r
= filter (lambda x
: x
.readable(), sockets
)
49 w
= filter (lambda x
: x
.writable(), sockets
)
55 (r
,w
,e
) = select
.select (r
,w
,e
, timeout
)
61 x
.handle_error (sys
.exc_type
, sys
.exc_value
, sys
.exc_traceback
)
66 x
.handle_error (sys
.exc_type
, sys
.exc_value
, sys
.exc_traceback
)
69 x
.handle_write_event()
71 x
.handle_error (sys
.exc_type
, sys
.exc_value
, sys
.exc_traceback
)
73 def poll2 (timeout
=0.0):
75 # timeout is in milliseconds
76 timeout
= int(timeout
*1000)
79 for s
in socket_map
.keys():
80 fd_map
[s
.fileno()] = s
82 for fd
, s
in fd_map
.items():
87 flags
= flags | poll
.POLLOUT
90 r
= poll
.poll (l
, timeout
)
95 if (flags
& poll
.POLLIN
):
97 if (flags
& poll
.POLLOUT
):
98 s
.handle_write_event()
99 if (flags
& poll
.POLLERR
):
100 s
.handle_expt_event()
102 apply (s
.handle_error
, sys
.exc_info())
105 def loop (timeout
=30.0, use_poll
=0):
122 def __init__ (self
, sock
=None):
124 self
.set_socket (sock
)
125 # I think it should inherit this anyway
126 self
.socket
.setblocking (0)
132 if self
.accepting
and self
.addr
:
133 status
.append ('listening')
135 status
.append ('connected')
137 status
.append ('%s:%d' % self
.addr
)
138 return '<%s %s at %x>' % (
139 self
.__class
__.__name
__,
140 string
.join (status
, ' '),
149 return '<__repr__ (self) failed for object at %x (addr=%s)>' % (id(self
),ar
)
151 def add_channel (self
):
152 self
.log ('adding channel %s' % self
)
153 socket_map
[self
] = 1
155 def del_channel (self
):
156 if socket_map
.has_key (self
):
157 self
.log ('closing channel %d:%s' % (self
.fileno(), self
))
158 del socket_map
[self
]
160 def create_socket (self
, family
, type):
161 self
.family_and_type
= family
, type
162 self
.socket
= socket
.socket (family
, type)
163 self
.socket
.setblocking(0)
166 def set_socket (self
, socket
):
170 def set_reuse_addr (self
):
171 # try to re-use a server port if possible
173 self
.socket
.setsockopt (
174 socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
,
175 self
.socket
.getsockopt (socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
) |
1
180 # ==================================================
181 # predicates for select()
182 # these are used as filters for the lists of sockets
183 # to pass to select().
184 # ==================================================
190 # The macintosh will select a listening socket for
191 # write if you let it. What might this mean?
193 return not self
.accepting
198 # ==================================================
199 # socket object methods.
200 # ==================================================
202 def listen (self
, num
):
204 if os
.name
== 'nt' and num
> 5:
206 return self
.socket
.listen (num
)
208 def bind (self
, addr
):
210 return self
.socket
.bind (addr
)
212 def connect (self
, address
):
214 self
.socket
.connect (address
)
215 except socket
.error
, why
:
216 if why
[0] in (EINPROGRESS
, EALREADY
, EWOULDBLOCK
):
219 raise socket
.error
, why
221 self
.handle_connect()
225 conn
, addr
= self
.socket
.accept()
227 except socket
.error
, why
:
228 if why
[0] == EWOULDBLOCK
:
231 raise socket
.error
, why
233 def send (self
, data
):
235 result
= self
.socket
.send (data
)
237 except socket
.error
, why
:
238 if why
[0] == EWOULDBLOCK
:
241 raise socket
.error
, why
244 def recv (self
, buffer_size
):
246 data
= self
.socket
.recv (buffer_size
)
248 # a closed connection is indicated by signaling
249 # a read condition, and having recv() return 0.
254 except socket
.error
, why
:
255 # winsock sometimes throws ENOTCONN
256 if why
[0] in [ECONNRESET
, ENOTCONN
]:
260 raise socket
.error
, why
267 # cheap inheritance, used to pass all other attribute
268 # references to the underlying socket object.
269 def __getattr__ (self
, attr
):
271 return getattr (self
.socket
, attr
)
273 raise AttributeError, attr
275 def log (self
, message
):
276 print 'log:', message
278 def handle_read_event (self
):
280 # for an accepting socket, getting a read implies
281 # that we are connected
282 if not self
.connected
:
285 elif not self
.connected
:
286 self
.handle_connect()
292 def handle_write_event (self
):
293 # getting a write implies that we are connected
294 if not self
.connected
:
295 self
.handle_connect()
299 def handle_expt_event (self
):
302 def handle_error (self
, *info
):
304 (file,fun
,line
), tbinfo
= compact_traceback (t
,v
,tb
)
306 # sometimes a user repr method will crash.
308 self_repr
= repr (self
)
310 self_repr
= '<__repr__ (self) failed for object at %0x>' % id(self
)
313 'uncaptured python exception, closing channel %s (%s:%s %s)' % (
323 def handle_expt (self
):
324 self
.log ('unhandled exception')
326 def handle_read (self
):
327 self
.log ('unhandled read event')
329 def handle_write (self
):
330 self
.log ('unhandled write event')
332 def handle_connect (self
):
333 self
.log ('unhandled connect event')
335 def handle_oob (self
):
336 self
.log ('unhandled out-of-band event')
338 def handle_accept (self
):
339 self
.log ('unhandled accept event')
341 def handle_close (self
):
342 self
.log ('unhandled close event')
345 # ---------------------------------------------------------------------------
346 # adds simple buffered output capability, useful for simple clients.
347 # [for more sophisticated usage use asynchat.async_chat]
348 # ---------------------------------------------------------------------------
350 class dispatcher_with_send (dispatcher
):
351 def __init__ (self
, sock
=None):
352 dispatcher
.__init
__ (self
, sock
)
355 def initiate_send (self
):
357 num_sent
= dispatcher
.send (self
, self
.out_buffer
[:512])
358 self
.out_buffer
= self
.out_buffer
[num_sent
:]
360 def handle_write (self
):
364 return (not self
.connected
) or len(self
.out_buffer
)
366 def send (self
, data
):
368 self
.log ('sending %s' % repr(data
))
369 self
.out_buffer
= self
.out_buffer
+ data
372 # ---------------------------------------------------------------------------
373 # used for debugging.
374 # ---------------------------------------------------------------------------
376 def compact_traceback (t
,v
,tb
):
380 tb
.tb_frame
.f_code
.co_filename
,
381 tb
.tb_frame
.f_code
.co_name
,
388 file, function
, line
= tbinfo
[-1]
389 info
= '[' + string
.join (
391 lambda x
: string
.join (x
, '|'),
396 return (file, function
, line
), info
400 for x
in socket_map
.keys():
404 # Asynchronous File I/O:
406 # After a little research (reading man pages on various unixen, and
407 # digging through the linux kernel), I've determined that select()
408 # isn't meant for doing doing asynchronous file i/o.
409 # Heartening, though - reading linux/mm/filemap.c shows that linux
410 # supports asynchronous read-ahead. So _MOST_ of the time, the data
411 # will be sitting in memory for us already when we go to read it.
413 # What other OS's (besides NT) support async file i/o? [VMS?]
415 # Regardless, this is useful for pipes, and stdin/stdout...
418 if os
.name
== 'posix':
423 # here we override just enough to make a file
424 # look like a socket for the purposes of asyncore.
425 def __init__ (self
, fd
):
428 def recv (self
, *args
):
429 return apply (os
.read
, (self
.fd
,)+args
)
431 def write (self
, *args
):
432 return apply (os
.write
, (self
.fd
,)+args
)
435 return os
.close (self
.fd
)
440 class file_dispatcher (dispatcher
):
441 def __init__ (self
, fd
):
442 dispatcher
.__init
__ (self
)
444 # set it to non-blocking mode
445 flags
= fcntl
.fcntl (fd
, FCNTL
.F_GETFL
, 0)
446 flags
= flags | FCNTL
.O_NONBLOCK
447 fcntl
.fcntl (fd
, FCNTL
.F_SETFL
, flags
)
450 def set_file (self
, fd
):
451 self
.socket
= file_wrapper (fd
)