2000-09-11 01:00:46 -03:00
|
|
|
# -*- Mode: Python -*-
|
2001-01-14 14:09:23 -04:00
|
|
|
# Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp
|
2000-09-11 01:00:46 -03:00
|
|
|
# Author: Sam Rushing <rushing@nightmare.com>
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
# ======================================================================
|
|
|
|
# Copyright 1996 by Sam Rushing
|
2001-01-14 14:09:23 -04:00
|
|
|
#
|
1999-01-12 16:19:27 -04:00
|
|
|
# All Rights Reserved
|
2001-01-14 14:09:23 -04:00
|
|
|
#
|
1999-01-12 16:19:27 -04:00
|
|
|
# Permission to use, copy, modify, and distribute this software and
|
|
|
|
# its documentation for any purpose and without fee is hereby
|
|
|
|
# granted, provided that the above copyright notice appear in all
|
|
|
|
# copies and that both that copyright notice and this permission
|
|
|
|
# notice appear in supporting documentation, and that the name of Sam
|
|
|
|
# Rushing not be used in advertising or publicity pertaining to
|
|
|
|
# distribution of the software without specific, written prior
|
|
|
|
# permission.
|
2001-01-14 14:09:23 -04:00
|
|
|
#
|
1999-01-12 16:19:27 -04:00
|
|
|
# SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
|
|
|
|
# NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
# ======================================================================
|
|
|
|
|
2000-02-04 11:39:30 -04:00
|
|
|
"""Basic infrastructure for asynchronous socket service clients and servers.
|
|
|
|
|
|
|
|
There are only two ways to have a program on a single processor do "more
|
2001-01-14 14:09:23 -04:00
|
|
|
than one thing at a time". Multi-threaded programming is the simplest and
|
2000-02-04 11:39:30 -04:00
|
|
|
most popular way to do it, but there is another very different technique,
|
|
|
|
that lets you have nearly all the advantages of multi-threading, without
|
|
|
|
actually using multiple threads. it's really only practical if your program
|
|
|
|
is largely I/O bound. If your program is CPU bound, then pre-emptive
|
|
|
|
scheduled threads are probably what you really need. Network servers are
|
2001-01-14 14:09:23 -04:00
|
|
|
rarely CPU-bound, however.
|
2000-02-04 11:39:30 -04:00
|
|
|
|
2001-01-14 14:09:23 -04:00
|
|
|
If your operating system supports the select() system call in its I/O
|
2000-02-04 11:39:30 -04:00
|
|
|
library (and nearly all do), then you can use it to juggle multiple
|
|
|
|
communication channels at once; doing other work while your I/O is taking
|
|
|
|
place in the "background." Although this strategy can seem strange and
|
|
|
|
complex, especially at first, it is in many ways easier to understand and
|
|
|
|
control than multi-threaded programming. The module documented here solves
|
|
|
|
many of the difficult problems for you, making the task of building
|
2001-01-14 14:09:23 -04:00
|
|
|
sophisticated high-performance network servers and clients a snap.
|
2000-02-04 11:39:30 -04:00
|
|
|
"""
|
|
|
|
|
1999-01-12 16:19:27 -04:00
|
|
|
import select
|
|
|
|
import socket
|
|
|
|
import sys
|
2002-09-24 14:30:31 -03:00
|
|
|
import time
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
import os
|
2001-08-10 11:30:35 -03:00
|
|
|
from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \
|
2008-06-10 02:00:08 -03:00
|
|
|
ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, errorcode
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2000-09-08 17:30:39 -03:00
|
|
|
try:
|
2000-09-11 01:00:46 -03:00
|
|
|
socket_map
|
2000-09-08 17:30:39 -03:00
|
|
|
except NameError:
|
2000-09-11 01:00:46 -03:00
|
|
|
socket_map = {}
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2008-06-10 02:00:08 -03:00
|
|
|
def _strerror(err):
|
|
|
|
res = os.strerror(err)
|
|
|
|
if res == 'Unknown error':
|
|
|
|
res = errorcode[err]
|
|
|
|
return res
|
|
|
|
|
2005-08-31 21:45:28 -03:00
|
|
|
class ExitNow(Exception):
|
2000-09-11 01:00:46 -03:00
|
|
|
pass
|
2000-09-08 17:30:39 -03:00
|
|
|
|
2009-03-31 16:32:34 -03:00
|
|
|
_reraised_exceptions = (ExitNow, KeyboardInterrupt, SystemExit)
|
|
|
|
|
2002-09-07 21:14:54 -03:00
|
|
|
def read(obj):
|
|
|
|
try:
|
|
|
|
obj.handle_read_event()
|
2009-03-31 16:32:34 -03:00
|
|
|
except _reraised_exceptions:
|
2002-09-07 21:14:54 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
|
|
|
def write(obj):
|
|
|
|
try:
|
|
|
|
obj.handle_write_event()
|
2009-03-31 16:32:34 -03:00
|
|
|
except _reraised_exceptions:
|
2002-09-07 21:14:54 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
2008-06-10 02:00:08 -03:00
|
|
|
def _exception(obj):
|
2004-07-10 14:36:11 -03:00
|
|
|
try:
|
|
|
|
obj.handle_expt_event()
|
2009-03-31 16:32:34 -03:00
|
|
|
except _reraised_exceptions:
|
2004-07-10 14:36:11 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
|
|
|
|
2002-09-07 21:14:54 -03:00
|
|
|
def readwrite(obj, flags):
|
|
|
|
try:
|
2009-03-31 16:32:34 -03:00
|
|
|
if flags & select.POLLIN:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj.handle_read_event()
|
|
|
|
if flags & select.POLLOUT:
|
|
|
|
obj.handle_write_event()
|
2009-03-31 16:32:34 -03:00
|
|
|
if flags & (select.POLLHUP | select.POLLERR | select.POLLNVAL):
|
2008-09-07 01:37:10 -03:00
|
|
|
obj.handle_close()
|
2009-03-31 18:49:36 -03:00
|
|
|
if flags & select.POLLPRI:
|
|
|
|
obj.handle_expt_event()
|
2009-03-31 16:32:34 -03:00
|
|
|
except _reraised_exceptions:
|
2002-09-07 21:14:54 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
obj.handle_error()
|
2000-09-08 17:30:39 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def poll(timeout=0.0, map=None):
|
2000-09-11 01:00:46 -03:00
|
|
|
if map is None:
|
|
|
|
map = socket_map
|
|
|
|
if map:
|
|
|
|
r = []; w = []; e = []
|
2002-09-12 01:57:29 -03:00
|
|
|
for fd, obj in map.items():
|
2004-09-01 11:04:51 -03:00
|
|
|
is_r = obj.readable()
|
|
|
|
is_w = obj.writable()
|
|
|
|
if is_r:
|
2002-09-07 21:14:54 -03:00
|
|
|
r.append(fd)
|
2004-09-01 11:04:51 -03:00
|
|
|
if is_w:
|
2002-09-07 21:14:54 -03:00
|
|
|
w.append(fd)
|
2004-09-01 11:04:51 -03:00
|
|
|
if is_r or is_w:
|
|
|
|
e.append(fd)
|
2002-09-24 14:30:31 -03:00
|
|
|
if [] == r == w == e:
|
|
|
|
time.sleep(timeout)
|
2008-06-10 02:00:08 -03:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
r, w, e = select.select(r, w, e, timeout)
|
|
|
|
except select.error, err:
|
2008-07-20 04:29:58 -03:00
|
|
|
if err.args[0] != EINTR:
|
2008-06-10 02:00:08 -03:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
return
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
for fd in r:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2001-10-09 07:10:33 -03:00
|
|
|
continue
|
2002-09-07 21:14:54 -03:00
|
|
|
read(obj)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
for fd in w:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2001-10-09 07:10:33 -03:00
|
|
|
continue
|
2002-09-07 21:14:54 -03:00
|
|
|
write(obj)
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2004-07-10 14:36:11 -03:00
|
|
|
for fd in e:
|
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
|
|
|
continue
|
|
|
|
_exception(obj)
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def poll2(timeout=0.0, map=None):
|
2001-01-24 11:50:19 -04:00
|
|
|
# Use the poll() support added to the select module in Python 2.0
|
|
|
|
if map is None:
|
2002-09-12 01:57:29 -03:00
|
|
|
map = socket_map
|
2001-09-19 14:31:47 -03:00
|
|
|
if timeout is not None:
|
|
|
|
# timeout is in milliseconds
|
|
|
|
timeout = int(timeout*1000)
|
2001-01-24 11:50:19 -04:00
|
|
|
pollster = select.poll()
|
|
|
|
if map:
|
2002-09-12 01:57:29 -03:00
|
|
|
for fd, obj in map.items():
|
2004-09-01 11:04:51 -03:00
|
|
|
flags = 0
|
2001-01-24 11:50:19 -04:00
|
|
|
if obj.readable():
|
2004-07-07 09:23:53 -03:00
|
|
|
flags |= select.POLLIN | select.POLLPRI
|
2001-01-24 11:50:19 -04:00
|
|
|
if obj.writable():
|
2004-07-07 09:23:53 -03:00
|
|
|
flags |= select.POLLOUT
|
2001-01-24 11:50:19 -04:00
|
|
|
if flags:
|
2004-09-01 11:04:51 -03:00
|
|
|
# Only check for exceptions if object was either readable
|
|
|
|
# or writable.
|
|
|
|
flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL
|
2001-01-24 11:50:19 -04:00
|
|
|
pollster.register(fd, flags)
|
2001-10-29 12:32:19 -04:00
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
r = pollster.poll(timeout)
|
2001-10-29 12:32:19 -04:00
|
|
|
except select.error, err:
|
2008-07-20 04:29:58 -03:00
|
|
|
if err.args[0] != EINTR:
|
2001-10-29 12:32:19 -04:00
|
|
|
raise
|
|
|
|
r = []
|
2001-01-24 11:50:19 -04:00
|
|
|
for fd, flags in r:
|
2002-09-07 21:14:54 -03:00
|
|
|
obj = map.get(fd)
|
|
|
|
if obj is None:
|
2001-10-09 07:10:33 -03:00
|
|
|
continue
|
2002-09-07 21:14:54 -03:00
|
|
|
readwrite(obj, flags)
|
2001-01-24 11:50:19 -04:00
|
|
|
|
2003-10-22 11:38:27 -03:00
|
|
|
poll3 = poll2 # Alias for backward compatibility
|
|
|
|
|
2004-06-30 06:02:33 -03:00
|
|
|
def loop(timeout=30.0, use_poll=False, map=None, count=None):
|
2001-01-24 11:50:19 -04:00
|
|
|
if map is None:
|
2002-09-07 21:14:54 -03:00
|
|
|
map = socket_map
|
2001-01-24 11:50:19 -04:00
|
|
|
|
2003-10-22 11:38:27 -03:00
|
|
|
if use_poll and hasattr(select, 'poll'):
|
|
|
|
poll_fun = poll2
|
2000-09-11 01:00:46 -03:00
|
|
|
else:
|
|
|
|
poll_fun = poll
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2004-06-30 06:02:33 -03:00
|
|
|
if count is None:
|
|
|
|
while map:
|
|
|
|
poll_fun(timeout, map)
|
|
|
|
|
|
|
|
else:
|
|
|
|
while map and count > 0:
|
|
|
|
poll_fun(timeout, map)
|
|
|
|
count = count - 1
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
class dispatcher:
|
2002-09-12 01:57:29 -03:00
|
|
|
|
2004-03-21 15:46:16 -04:00
|
|
|
debug = False
|
|
|
|
connected = False
|
|
|
|
accepting = False
|
|
|
|
closing = False
|
2000-09-11 01:00:46 -03:00
|
|
|
addr = None
|
2009-03-31 16:32:34 -03:00
|
|
|
ignore_log_types = frozenset(['warning'])
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def __init__(self, sock=None, map=None):
|
2003-10-22 10:48:27 -03:00
|
|
|
if map is None:
|
|
|
|
self._map = socket_map
|
|
|
|
else:
|
|
|
|
self._map = map
|
|
|
|
|
2008-06-10 02:00:08 -03:00
|
|
|
self._fileno = None
|
|
|
|
|
2000-09-11 01:00:46 -03:00
|
|
|
if sock:
|
2008-06-10 02:00:08 -03:00
|
|
|
# Set to nonblocking just to make sure for cases where we
|
|
|
|
# get a socket from a blocking source.
|
|
|
|
sock.setblocking(0)
|
2002-09-12 01:57:29 -03:00
|
|
|
self.set_socket(sock, map)
|
2004-03-21 15:46:16 -04:00
|
|
|
self.connected = True
|
2008-06-10 02:00:08 -03:00
|
|
|
# The constructor no longer requires that the socket
|
|
|
|
# passed be connected.
|
2001-12-14 12:15:11 -04:00
|
|
|
try:
|
|
|
|
self.addr = sock.getpeername()
|
2008-06-10 12:58:19 -03:00
|
|
|
except socket.error, err:
|
2008-07-20 04:29:58 -03:00
|
|
|
if err.args[0] == ENOTCONN:
|
2008-06-10 02:00:08 -03:00
|
|
|
# To handle the case where we got an unconnected
|
|
|
|
# socket.
|
|
|
|
self.connected = False
|
|
|
|
else:
|
|
|
|
# The socket is broken in some unknown way, alert
|
|
|
|
# the user and remove it from the map (to prevent
|
|
|
|
# polling of broken sockets).
|
|
|
|
self.del_channel(map)
|
|
|
|
raise
|
2001-10-29 12:32:19 -04:00
|
|
|
else:
|
|
|
|
self.socket = None
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def __repr__(self):
|
2001-10-18 14:33:19 -03:00
|
|
|
status = [self.__class__.__module__+"."+self.__class__.__name__]
|
|
|
|
if self.accepting and self.addr:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append('listening')
|
2001-10-18 14:33:19 -03:00
|
|
|
elif self.connected:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append('connected')
|
2001-10-18 14:33:19 -03:00
|
|
|
if self.addr is not None:
|
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append('%s:%d' % self.addr)
|
2001-10-18 14:33:19 -03:00
|
|
|
except TypeError:
|
2002-09-12 01:57:29 -03:00
|
|
|
status.append(repr(self.addr))
|
|
|
|
return '<%s at %#x>' % (' '.join(status), id(self))
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def add_channel(self, map=None):
|
|
|
|
#self.log_info('adding channel %s' % self)
|
2000-09-11 01:00:46 -03:00
|
|
|
if map is None:
|
2003-10-22 10:48:27 -03:00
|
|
|
map = self._map
|
2002-09-13 11:09:26 -03:00
|
|
|
map[self._fileno] = self
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def del_channel(self, map=None):
|
2000-09-11 01:00:46 -03:00
|
|
|
fd = self._fileno
|
|
|
|
if map is None:
|
2003-10-22 10:48:27 -03:00
|
|
|
map = self._map
|
2008-08-02 00:32:13 -03:00
|
|
|
if fd in map:
|
2002-09-12 01:57:29 -03:00
|
|
|
#self.log_info('closing channel %d:%s' % (fd, self))
|
2002-09-13 11:09:26 -03:00
|
|
|
del map[fd]
|
2004-02-08 07:32:50 -04:00
|
|
|
self._fileno = None
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def create_socket(self, family, type):
|
2000-09-11 01:00:46 -03:00
|
|
|
self.family_and_type = family, type
|
2008-06-10 02:00:08 -03:00
|
|
|
sock = socket.socket(family, type)
|
|
|
|
sock.setblocking(0)
|
|
|
|
self.set_socket(sock)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def set_socket(self, sock, map=None):
|
2001-10-29 12:32:19 -04:00
|
|
|
self.socket = sock
|
|
|
|
## self.__dict__['socket'] = sock
|
2000-09-11 01:00:46 -03:00
|
|
|
self._fileno = sock.fileno()
|
2002-09-12 01:57:29 -03:00
|
|
|
self.add_channel(map)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def set_reuse_addr(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
# try to re-use a server port if possible
|
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
self.socket.setsockopt(
|
2004-08-13 17:06:57 -03:00
|
|
|
socket.SOL_SOCKET, socket.SO_REUSEADDR,
|
2002-09-12 01:57:29 -03:00
|
|
|
self.socket.getsockopt(socket.SOL_SOCKET,
|
2004-08-13 17:06:57 -03:00
|
|
|
socket.SO_REUSEADDR) | 1
|
2000-09-11 01:00:46 -03:00
|
|
|
)
|
2001-05-11 15:28:54 -03:00
|
|
|
except socket.error:
|
2000-09-11 01:00:46 -03:00
|
|
|
pass
|
2004-07-18 03:16:08 -03:00
|
|
|
|
2000-09-11 01:00:46 -03:00
|
|
|
# ==================================================
|
|
|
|
# predicates for select()
|
|
|
|
# these are used as filters for the lists of sockets
|
|
|
|
# to pass to select().
|
|
|
|
# ==================================================
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def readable(self):
|
2002-04-04 18:55:58 -04:00
|
|
|
return True
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2004-03-21 15:52:01 -04:00
|
|
|
def writable(self):
|
|
|
|
return True
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
# ==================================================
|
|
|
|
# socket object methods.
|
|
|
|
# ==================================================
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def listen(self, num):
|
2004-03-21 15:46:16 -04:00
|
|
|
self.accepting = True
|
2000-09-11 01:00:46 -03:00
|
|
|
if os.name == 'nt' and num > 5:
|
2008-06-10 02:00:08 -03:00
|
|
|
num = 5
|
2002-09-12 01:57:29 -03:00
|
|
|
return self.socket.listen(num)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def bind(self, addr):
|
2000-09-11 01:00:46 -03:00
|
|
|
self.addr = addr
|
2002-09-12 01:57:29 -03:00
|
|
|
return self.socket.bind(addr)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def connect(self, address):
|
2004-03-21 15:46:16 -04:00
|
|
|
self.connected = False
|
2001-10-29 12:44:37 -04:00
|
|
|
err = self.socket.connect_ex(address)
|
2002-12-26 14:22:54 -04:00
|
|
|
# XXX Should interpret Winsock return values
|
2001-10-29 12:44:37 -04:00
|
|
|
if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
|
|
|
|
return
|
|
|
|
if err in (0, EISCONN):
|
|
|
|
self.addr = address
|
2008-06-10 02:00:08 -03:00
|
|
|
self.handle_connect_event()
|
2001-10-30 10:16:17 -04:00
|
|
|
else:
|
2008-06-10 02:00:08 -03:00
|
|
|
raise socket.error(err, errorcode[err])
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def accept(self):
|
2002-04-04 17:02:24 -04:00
|
|
|
# XXX can return either an address pair or None
|
2000-09-11 01:00:46 -03:00
|
|
|
try:
|
|
|
|
conn, addr = self.socket.accept()
|
|
|
|
return conn, addr
|
|
|
|
except socket.error, why:
|
2008-07-20 04:29:58 -03:00
|
|
|
if why.args[0] == EWOULDBLOCK:
|
2000-09-11 01:00:46 -03:00
|
|
|
pass
|
|
|
|
else:
|
2004-07-07 17:54:48 -03:00
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def send(self, data):
|
2000-09-11 01:00:46 -03:00
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
result = self.socket.send(data)
|
2000-09-11 01:00:46 -03:00
|
|
|
return result
|
|
|
|
except socket.error, why:
|
2008-07-20 04:29:58 -03:00
|
|
|
if why.args[0] == EWOULDBLOCK:
|
2000-09-11 01:00:46 -03:00
|
|
|
return 0
|
2008-07-20 04:29:58 -03:00
|
|
|
elif why.args[0] in (ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED):
|
2008-06-10 02:00:08 -03:00
|
|
|
self.handle_close()
|
|
|
|
return 0
|
2000-09-11 01:00:46 -03:00
|
|
|
else:
|
2004-07-07 17:54:48 -03:00
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def recv(self, buffer_size):
|
2000-09-11 01:00:46 -03:00
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
data = self.socket.recv(buffer_size)
|
2000-09-11 01:00:46 -03:00
|
|
|
if not data:
|
|
|
|
# a closed connection is indicated by signaling
|
|
|
|
# a read condition, and having recv() return 0.
|
|
|
|
self.handle_close()
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
return data
|
|
|
|
except socket.error, why:
|
|
|
|
# winsock sometimes throws ENOTCONN
|
2008-07-20 04:29:58 -03:00
|
|
|
if why.args[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED]:
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_close()
|
|
|
|
return ''
|
|
|
|
else:
|
2004-07-07 17:54:48 -03:00
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def close(self):
|
2008-06-10 02:00:08 -03:00
|
|
|
self.connected = False
|
|
|
|
self.accepting = False
|
2000-09-11 01:00:46 -03:00
|
|
|
self.del_channel()
|
2008-06-10 02:00:08 -03:00
|
|
|
try:
|
|
|
|
self.socket.close()
|
|
|
|
except socket.error, why:
|
2008-07-20 04:29:58 -03:00
|
|
|
if why.args[0] not in (ENOTCONN, EBADF):
|
2008-06-10 02:00:08 -03:00
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
# cheap inheritance, used to pass all other attribute
|
|
|
|
# references to the underlying socket object.
|
2002-09-12 01:57:29 -03:00
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.socket, attr)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2003-02-13 21:13:01 -04:00
|
|
|
# log and log_info may be overridden to provide more sophisticated
|
2000-09-11 01:00:46 -03:00
|
|
|
# logging and warning methods. In general, log is for 'hit' logging
|
2001-01-14 14:09:23 -04:00
|
|
|
# and 'log_info' is for informational, warning and error logging.
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def log(self, message):
|
|
|
|
sys.stderr.write('log: %s\n' % str(message))
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def log_info(self, message, type='info'):
|
2009-03-31 22:28:11 -03:00
|
|
|
if type not in self.ignore_log_types:
|
2000-09-11 01:00:46 -03:00
|
|
|
print '%s: %s' % (type, message)
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_read_event(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
if self.accepting:
|
2008-06-10 02:00:08 -03:00
|
|
|
# accepting sockets are never connected, they "spawn" new
|
|
|
|
# sockets that are connected
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_accept()
|
|
|
|
elif not self.connected:
|
2008-06-10 02:00:08 -03:00
|
|
|
self.handle_connect_event()
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_read()
|
|
|
|
else:
|
|
|
|
self.handle_read()
|
|
|
|
|
2008-06-10 02:00:08 -03:00
|
|
|
def handle_connect_event(self):
|
|
|
|
self.connected = True
|
|
|
|
self.handle_connect()
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_write_event(self):
|
2008-06-10 02:00:08 -03:00
|
|
|
if self.accepting:
|
|
|
|
# Accepting sockets shouldn't get a write event.
|
|
|
|
# We will pretend it didn't happen.
|
|
|
|
return
|
|
|
|
|
2000-09-11 01:00:46 -03:00
|
|
|
if not self.connected:
|
2008-06-10 02:00:08 -03:00
|
|
|
#check for errors
|
|
|
|
err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
|
|
|
|
if err != 0:
|
2008-06-10 12:58:19 -03:00
|
|
|
raise socket.error(err, _strerror(err))
|
2008-06-10 02:00:08 -03:00
|
|
|
|
|
|
|
self.handle_connect_event()
|
2000-09-11 01:00:46 -03:00
|
|
|
self.handle_write()
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_expt_event(self):
|
2009-03-31 16:32:34 -03:00
|
|
|
# handle_expt_event() is called if there might be an error on the
|
|
|
|
# socket, or if there is OOB data
|
|
|
|
# check for the error condition first
|
|
|
|
err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
|
|
|
|
if err != 0:
|
|
|
|
# we can get here when select.select() says that there is an
|
|
|
|
# exceptional condition on the socket
|
|
|
|
# since there is an error, we'll go ahead and close the socket
|
|
|
|
# like we would in a subclassed handle_read() that received no
|
|
|
|
# data
|
|
|
|
self.handle_close()
|
2008-06-10 02:00:08 -03:00
|
|
|
else:
|
|
|
|
self.handle_expt()
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_error(self):
|
2001-08-10 11:30:35 -03:00
|
|
|
nil, t, v, tbinfo = compact_traceback()
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
# sometimes a user repr method will crash.
|
|
|
|
try:
|
2002-09-12 01:57:29 -03:00
|
|
|
self_repr = repr(self)
|
2000-09-11 01:00:46 -03:00
|
|
|
except:
|
2002-09-12 01:57:29 -03:00
|
|
|
self_repr = '<__repr__(self) failed for object at %0x>' % id(self)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
self.log_info(
|
2000-09-11 01:00:46 -03:00
|
|
|
'uncaptured python exception, closing channel %s (%s:%s %s)' % (
|
|
|
|
self_repr,
|
|
|
|
t,
|
|
|
|
v,
|
|
|
|
tbinfo
|
|
|
|
),
|
|
|
|
'error'
|
|
|
|
)
|
2008-07-07 01:51:46 -03:00
|
|
|
self.handle_close()
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_expt(self):
|
2009-03-31 16:32:34 -03:00
|
|
|
self.log_info('unhandled incoming priority event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_read(self):
|
|
|
|
self.log_info('unhandled read event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_write(self):
|
|
|
|
self.log_info('unhandled write event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_connect(self):
|
|
|
|
self.log_info('unhandled connect event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_accept(self):
|
|
|
|
self.log_info('unhandled accept event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_close(self):
|
|
|
|
self.log_info('unhandled close event', 'warning')
|
2000-09-11 01:00:46 -03:00
|
|
|
self.close()
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# adds simple buffered output capability, useful for simple clients.
|
|
|
|
# [for more sophisticated usage use asynchat.async_chat]
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
class dispatcher_with_send(dispatcher):
|
|
|
|
|
2004-03-21 16:03:18 -04:00
|
|
|
def __init__(self, sock=None, map=None):
|
|
|
|
dispatcher.__init__(self, sock, map)
|
2000-09-11 01:00:46 -03:00
|
|
|
self.out_buffer = ''
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def initiate_send(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
num_sent = 0
|
2002-09-12 01:57:29 -03:00
|
|
|
num_sent = dispatcher.send(self, self.out_buffer[:512])
|
2000-09-11 01:00:46 -03:00
|
|
|
self.out_buffer = self.out_buffer[num_sent:]
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def handle_write(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
self.initiate_send()
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def writable(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
return (not self.connected) or len(self.out_buffer)
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def send(self, data):
|
2000-09-11 01:00:46 -03:00
|
|
|
if self.debug:
|
2002-09-12 01:57:29 -03:00
|
|
|
self.log_info('sending %s' % repr(data))
|
2000-09-11 01:00:46 -03:00
|
|
|
self.out_buffer = self.out_buffer + data
|
|
|
|
self.initiate_send()
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# used for debugging.
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def compact_traceback():
|
2002-09-13 11:09:26 -03:00
|
|
|
t, v, tb = sys.exc_info()
|
2000-09-11 01:00:46 -03:00
|
|
|
tbinfo = []
|
2008-06-10 02:00:08 -03:00
|
|
|
if not tb: # Must have a traceback
|
|
|
|
raise AssertionError("traceback does not exist")
|
2002-09-13 11:09:26 -03:00
|
|
|
while tb:
|
2002-09-12 01:57:29 -03:00
|
|
|
tbinfo.append((
|
2000-09-11 01:00:46 -03:00
|
|
|
tb.tb_frame.f_code.co_filename,
|
2001-01-14 14:09:23 -04:00
|
|
|
tb.tb_frame.f_code.co_name,
|
2000-09-11 01:00:46 -03:00
|
|
|
str(tb.tb_lineno)
|
|
|
|
))
|
|
|
|
tb = tb.tb_next
|
|
|
|
|
|
|
|
# just to be safe
|
|
|
|
del tb
|
|
|
|
|
|
|
|
file, function, line = tbinfo[-1]
|
2002-09-13 11:09:26 -03:00
|
|
|
info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo])
|
2000-09-11 01:00:46 -03:00
|
|
|
return (file, function, line), t, v, info
|
1999-01-12 16:19:27 -04:00
|
|
|
|
2008-06-10 02:00:08 -03:00
|
|
|
def close_all(map=None, ignore_all=False):
|
2000-09-11 01:00:46 -03:00
|
|
|
if map is None:
|
2002-09-12 01:57:29 -03:00
|
|
|
map = socket_map
|
2000-09-11 01:00:46 -03:00
|
|
|
for x in map.values():
|
2008-06-10 02:00:08 -03:00
|
|
|
try:
|
|
|
|
x.close()
|
|
|
|
except OSError, x:
|
2008-07-20 04:29:58 -03:00
|
|
|
if x.args[0] == EBADF:
|
2008-06-10 02:00:08 -03:00
|
|
|
pass
|
|
|
|
elif not ignore_all:
|
|
|
|
raise
|
2009-03-31 16:32:34 -03:00
|
|
|
except _reraised_exceptions:
|
2008-06-10 02:00:08 -03:00
|
|
|
raise
|
|
|
|
except:
|
|
|
|
if not ignore_all:
|
|
|
|
raise
|
2000-09-11 01:00:46 -03:00
|
|
|
map.clear()
|
1999-01-12 16:19:27 -04:00
|
|
|
|
|
|
|
# Asynchronous File I/O:
|
|
|
|
#
|
|
|
|
# After a little research (reading man pages on various unixen, and
|
|
|
|
# digging through the linux kernel), I've determined that select()
|
2003-10-20 11:01:56 -03:00
|
|
|
# isn't meant for doing asynchronous file i/o.
|
1999-01-12 16:19:27 -04:00
|
|
|
# Heartening, though - reading linux/mm/filemap.c shows that linux
|
|
|
|
# supports asynchronous read-ahead. So _MOST_ of the time, the data
|
|
|
|
# will be sitting in memory for us already when we go to read it.
|
|
|
|
#
|
|
|
|
# What other OS's (besides NT) support async file i/o? [VMS?]
|
|
|
|
#
|
|
|
|
# Regardless, this is useful for pipes, and stdin/stdout...
|
|
|
|
|
|
|
|
if os.name == 'posix':
|
2000-09-11 01:00:46 -03:00
|
|
|
import fcntl
|
|
|
|
|
|
|
|
class file_wrapper:
|
2008-06-10 02:00:08 -03:00
|
|
|
# Here we override just enough to make a file
|
2000-09-11 01:00:46 -03:00
|
|
|
# look like a socket for the purposes of asyncore.
|
2008-06-10 02:00:08 -03:00
|
|
|
# The passed fd is automatically os.dup()'d
|
2002-09-12 01:57:29 -03:00
|
|
|
|
|
|
|
def __init__(self, fd):
|
2008-06-10 02:00:08 -03:00
|
|
|
self.fd = os.dup(fd)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def recv(self, *args):
|
2002-04-04 17:02:24 -04:00
|
|
|
return os.read(self.fd, *args)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def send(self, *args):
|
2002-04-04 17:02:24 -04:00
|
|
|
return os.write(self.fd, *args)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
|
|
|
read = recv
|
|
|
|
write = send
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def close(self):
|
2004-07-10 12:51:19 -03:00
|
|
|
os.close(self.fd)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def fileno(self):
|
2000-09-11 01:00:46 -03:00
|
|
|
return self.fd
|
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
class file_dispatcher(dispatcher):
|
|
|
|
|
2004-03-21 16:03:18 -04:00
|
|
|
def __init__(self, fd, map=None):
|
|
|
|
dispatcher.__init__(self, None, map)
|
2004-03-21 15:46:16 -04:00
|
|
|
self.connected = True
|
2008-06-10 02:00:08 -03:00
|
|
|
try:
|
|
|
|
fd = fd.fileno()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2004-07-10 12:51:19 -03:00
|
|
|
self.set_file(fd)
|
2000-09-11 01:00:46 -03:00
|
|
|
# set it to non-blocking mode
|
2002-09-12 01:57:29 -03:00
|
|
|
flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
|
2001-05-10 12:33:31 -03:00
|
|
|
flags = flags | os.O_NONBLOCK
|
2002-09-12 01:57:29 -03:00
|
|
|
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
|
2000-09-11 01:00:46 -03:00
|
|
|
|
2002-09-12 01:57:29 -03:00
|
|
|
def set_file(self, fd):
|
|
|
|
self.socket = file_wrapper(fd)
|
2008-11-19 14:26:12 -04:00
|
|
|
self._fileno = self.socket.fileno()
|
2000-09-11 01:00:46 -03:00
|
|
|
self.add_channel()
|