Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 1 | # -*- Mode: Python -*- |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 2 | # Id: asyncore.py,v 2.51 2000/09/07 22:29:26 rushing Exp |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 3 | # Author: Sam Rushing <rushing@nightmare.com> |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 4 | |
| 5 | # ====================================================================== |
| 6 | # Copyright 1996 by Sam Rushing |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 7 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 8 | # All Rights Reserved |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 9 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 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 |
| 17 | # permission. |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 18 | # |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 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 | # ====================================================================== |
| 27 | |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 28 | """Basic infrastructure for asynchronous socket service clients and servers. |
| 29 | |
| 30 | There are only two ways to have a program on a single processor do "more |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 31 | than one thing at a time". Multi-threaded programming is the simplest and |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 32 | most popular way to do it, but there is another very different technique, |
| 33 | that lets you have nearly all the advantages of multi-threading, without |
| 34 | actually using multiple threads. it's really only practical if your program |
| 35 | is largely I/O bound. If your program is CPU bound, then pre-emptive |
| 36 | scheduled threads are probably what you really need. Network servers are |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 37 | rarely CPU-bound, however. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 38 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 39 | If your operating system supports the select() system call in its I/O |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 40 | library (and nearly all do), then you can use it to juggle multiple |
| 41 | communication channels at once; doing other work while your I/O is taking |
| 42 | place in the "background." Although this strategy can seem strange and |
| 43 | complex, especially at first, it is in many ways easier to understand and |
| 44 | control than multi-threaded programming. The module documented here solves |
| 45 | many of the difficult problems for you, making the task of building |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 46 | sophisticated high-performance network servers and clients a snap. |
Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 47 | """ |
| 48 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 49 | import select |
| 50 | import socket |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 51 | import sys |
Thomas Heller | d8ce87a | 2002-09-24 17:30:31 +0000 | [diff] [blame] | 52 | import time |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 53 | |
| 54 | import os |
Jeremy Hylton | a8b5f7d | 2001-08-10 14:30:35 +0000 | [diff] [blame] | 55 | from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ |
Andrew M. Kuchling | 174bdbc | 2004-03-21 19:58:28 +0000 | [diff] [blame] | 56 | ENOTCONN, ESHUTDOWN, EINTR, EISCONN, errorcode |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 57 | |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 58 | try: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 59 | socket_map |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 60 | except NameError: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 61 | socket_map = {} |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 62 | |
Neal Norwitz | 4ce69a5 | 2005-09-01 00:45:28 +0000 | [diff] [blame] | 63 | class ExitNow(Exception): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 64 | pass |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 66 | def read(obj): |
| 67 | try: |
| 68 | obj.handle_read_event() |
| 69 | except ExitNow: |
| 70 | raise |
| 71 | except: |
| 72 | obj.handle_error() |
| 73 | |
| 74 | def write(obj): |
| 75 | try: |
| 76 | obj.handle_write_event() |
| 77 | except ExitNow: |
| 78 | raise |
| 79 | except: |
| 80 | obj.handle_error() |
| 81 | |
Andrew M. Kuchling | 0fff6c8 | 2004-07-10 17:36:11 +0000 | [diff] [blame] | 82 | def _exception (obj): |
| 83 | try: |
| 84 | obj.handle_expt_event() |
| 85 | except ExitNow: |
| 86 | raise |
| 87 | except: |
| 88 | obj.handle_error() |
| 89 | |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 90 | def readwrite(obj, flags): |
| 91 | try: |
Andrew M. Kuchling | 9303777 | 2004-03-21 19:26:00 +0000 | [diff] [blame] | 92 | if flags & (select.POLLIN | select.POLLPRI): |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 93 | obj.handle_read_event() |
| 94 | if flags & select.POLLOUT: |
| 95 | obj.handle_write_event() |
Andrew M. Kuchling | 9303777 | 2004-03-21 19:26:00 +0000 | [diff] [blame] | 96 | if flags & (select.POLLERR | select.POLLHUP | select.POLLNVAL): |
| 97 | obj.handle_expt_event() |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 98 | except ExitNow: |
| 99 | raise |
| 100 | except: |
| 101 | obj.handle_error() |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 103 | def poll(timeout=0.0, map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 104 | if map is None: |
| 105 | map = socket_map |
| 106 | if map: |
| 107 | r = []; w = []; e = [] |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 108 | for fd, obj in map.items(): |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 109 | is_r = obj.readable() |
| 110 | is_w = obj.writable() |
| 111 | if is_r: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 112 | r.append(fd) |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 113 | if is_w: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 114 | w.append(fd) |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 115 | if is_r or is_w: |
| 116 | e.append(fd) |
Thomas Heller | d8ce87a | 2002-09-24 17:30:31 +0000 | [diff] [blame] | 117 | if [] == r == w == e: |
| 118 | time.sleep(timeout) |
| 119 | else: |
| 120 | try: |
| 121 | r, w, e = select.select(r, w, e, timeout) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 122 | except select.error as err: |
Georg Brandl | 6464d47 | 2007-10-22 16:16:13 +0000 | [diff] [blame] | 123 | if err.args[0] != EINTR: |
Thomas Heller | d8ce87a | 2002-09-24 17:30:31 +0000 | [diff] [blame] | 124 | raise |
Guido van Rossum | e94d8fa | 2002-11-05 18:41:20 +0000 | [diff] [blame] | 125 | else: |
| 126 | return |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 127 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 128 | for fd in r: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 129 | obj = map.get(fd) |
| 130 | if obj is None: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 131 | continue |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 132 | read(obj) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 133 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 134 | for fd in w: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 135 | obj = map.get(fd) |
| 136 | if obj is None: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 137 | continue |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 138 | write(obj) |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 139 | |
Andrew M. Kuchling | 0fff6c8 | 2004-07-10 17:36:11 +0000 | [diff] [blame] | 140 | for fd in e: |
| 141 | obj = map.get(fd) |
| 142 | if obj is None: |
| 143 | continue |
| 144 | _exception(obj) |
| 145 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 146 | def poll2(timeout=0.0, map=None): |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 147 | # Use the poll() support added to the select module in Python 2.0 |
| 148 | if map is None: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 149 | map = socket_map |
Martin v. Löwis | f6cc07c | 2001-09-19 17:31:47 +0000 | [diff] [blame] | 150 | if timeout is not None: |
| 151 | # timeout is in milliseconds |
| 152 | timeout = int(timeout*1000) |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 153 | pollster = select.poll() |
| 154 | if map: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 155 | for fd, obj in map.items(): |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 156 | flags = 0 |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 157 | if obj.readable(): |
Andrew M. Kuchling | 6fe93cd | 2004-07-07 12:23:53 +0000 | [diff] [blame] | 158 | flags |= select.POLLIN | select.POLLPRI |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 159 | if obj.writable(): |
Andrew M. Kuchling | 6fe93cd | 2004-07-07 12:23:53 +0000 | [diff] [blame] | 160 | flags |= select.POLLOUT |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 161 | if flags: |
Andrew M. Kuchling | e47c381 | 2004-09-01 14:04:51 +0000 | [diff] [blame] | 162 | # Only check for exceptions if object was either readable |
| 163 | # or writable. |
| 164 | flags |= select.POLLERR | select.POLLHUP | select.POLLNVAL |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 165 | pollster.register(fd, flags) |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 166 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 167 | r = pollster.poll(timeout) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 168 | except select.error as err: |
Georg Brandl | 6464d47 | 2007-10-22 16:16:13 +0000 | [diff] [blame] | 169 | if err.args[0] != EINTR: |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 170 | raise |
| 171 | r = [] |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 172 | for fd, flags in r: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 173 | obj = map.get(fd) |
| 174 | if obj is None: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 175 | continue |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 176 | readwrite(obj, flags) |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 177 | |
Andrew M. Kuchling | 6c2871e | 2003-10-22 14:38:27 +0000 | [diff] [blame] | 178 | poll3 = poll2 # Alias for backward compatibility |
| 179 | |
Michael W. Hudson | d5cf143 | 2004-06-30 09:02:33 +0000 | [diff] [blame] | 180 | def loop(timeout=30.0, use_poll=False, map=None, count=None): |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 181 | if map is None: |
Jeremy Hylton | d750036 | 2002-09-08 00:14:54 +0000 | [diff] [blame] | 182 | map = socket_map |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 183 | |
Andrew M. Kuchling | 6c2871e | 2003-10-22 14:38:27 +0000 | [diff] [blame] | 184 | if use_poll and hasattr(select, 'poll'): |
| 185 | poll_fun = poll2 |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 186 | else: |
| 187 | poll_fun = poll |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 188 | |
Michael W. Hudson | d5cf143 | 2004-06-30 09:02:33 +0000 | [diff] [blame] | 189 | if count is None: |
| 190 | while map: |
| 191 | poll_fun(timeout, map) |
| 192 | |
| 193 | else: |
| 194 | while map and count > 0: |
| 195 | poll_fun(timeout, map) |
| 196 | count = count - 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 197 | |
| 198 | class dispatcher: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 199 | |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 200 | debug = False |
| 201 | connected = False |
| 202 | accepting = False |
| 203 | closing = False |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 204 | addr = None |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 206 | def __init__(self, sock=None, map=None): |
Andrew M. Kuchling | f9ca409 | 2003-10-22 13:48:27 +0000 | [diff] [blame] | 207 | if map is None: |
| 208 | self._map = socket_map |
| 209 | else: |
| 210 | self._map = map |
| 211 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 212 | if sock: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 213 | self.set_socket(sock, map) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 214 | # I think it should inherit this anyway |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 215 | self.socket.setblocking(0) |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 216 | self.connected = True |
Jeremy Hylton | 2a05bc7 | 2001-12-14 16:15:11 +0000 | [diff] [blame] | 217 | # XXX Does the constructor require that the socket passed |
| 218 | # be connected? |
| 219 | try: |
| 220 | self.addr = sock.getpeername() |
| 221 | except socket.error: |
| 222 | # The addr isn't crucial |
| 223 | pass |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 224 | else: |
| 225 | self.socket = None |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 227 | def __repr__(self): |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 228 | status = [self.__class__.__module__+"."+self.__class__.__name__] |
| 229 | if self.accepting and self.addr: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 230 | status.append('listening') |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 231 | elif self.connected: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 232 | status.append('connected') |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 233 | if self.addr is not None: |
| 234 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 235 | status.append('%s:%d' % self.addr) |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 236 | except TypeError: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 237 | status.append(repr(self.addr)) |
| 238 | return '<%s at %#x>' % (' '.join(status), id(self)) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 240 | def add_channel(self, map=None): |
| 241 | #self.log_info('adding channel %s' % self) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 242 | if map is None: |
Andrew M. Kuchling | f9ca409 | 2003-10-22 13:48:27 +0000 | [diff] [blame] | 243 | map = self._map |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 244 | map[self._fileno] = self |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 246 | def del_channel(self, map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 247 | fd = self._fileno |
| 248 | if map is None: |
Andrew M. Kuchling | f9ca409 | 2003-10-22 13:48:27 +0000 | [diff] [blame] | 249 | map = self._map |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 250 | if fd in map: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 251 | #self.log_info('closing channel %d:%s' % (fd, self)) |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 252 | del map[fd] |
Raymond Hettinger | 3dc3484 | 2004-02-08 11:32:50 +0000 | [diff] [blame] | 253 | self._fileno = None |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 255 | def create_socket(self, family, type): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 256 | self.family_and_type = family, type |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 257 | self.socket = socket.socket(family, type) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 258 | self.socket.setblocking(0) |
| 259 | self._fileno = self.socket.fileno() |
| 260 | self.add_channel() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 262 | def set_socket(self, sock, map=None): |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 263 | self.socket = sock |
| 264 | ## self.__dict__['socket'] = sock |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 265 | self._fileno = sock.fileno() |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 266 | self.add_channel(map) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 268 | def set_reuse_addr(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 269 | # try to re-use a server port if possible |
| 270 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 271 | self.socket.setsockopt( |
Andrew M. Kuchling | 9d499f2 | 2004-08-13 20:06:57 +0000 | [diff] [blame] | 272 | socket.SOL_SOCKET, socket.SO_REUSEADDR, |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 273 | self.socket.getsockopt(socket.SOL_SOCKET, |
Andrew M. Kuchling | 9d499f2 | 2004-08-13 20:06:57 +0000 | [diff] [blame] | 274 | socket.SO_REUSEADDR) | 1 |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 275 | ) |
Fred Drake | 9f9b593 | 2001-05-11 18:28:54 +0000 | [diff] [blame] | 276 | except socket.error: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 277 | pass |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 278 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 279 | # ================================================== |
| 280 | # predicates for select() |
| 281 | # these are used as filters for the lists of sockets |
| 282 | # to pass to select(). |
| 283 | # ================================================== |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 285 | def readable(self): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 286 | return True |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 287 | |
Andrew M. Kuchling | 419af88 | 2004-03-21 19:52:01 +0000 | [diff] [blame] | 288 | def writable(self): |
| 289 | return True |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 290 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 291 | # ================================================== |
| 292 | # socket object methods. |
| 293 | # ================================================== |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 295 | def listen(self, num): |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 296 | self.accepting = True |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 297 | if os.name == 'nt' and num > 5: |
| 298 | num = 1 |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 299 | return self.socket.listen(num) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 301 | def bind(self, addr): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 302 | self.addr = addr |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 303 | return self.socket.bind(addr) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 305 | def connect(self, address): |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 306 | self.connected = False |
Jeremy Hylton | e16e54f | 2001-10-29 16:44:37 +0000 | [diff] [blame] | 307 | err = self.socket.connect_ex(address) |
Guido van Rossum | 9a40c1c | 2002-12-26 18:22:54 +0000 | [diff] [blame] | 308 | # XXX Should interpret Winsock return values |
Jeremy Hylton | e16e54f | 2001-10-29 16:44:37 +0000 | [diff] [blame] | 309 | if err in (EINPROGRESS, EALREADY, EWOULDBLOCK): |
| 310 | return |
| 311 | if err in (0, EISCONN): |
| 312 | self.addr = address |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 313 | self.connected = True |
Jeremy Hylton | e16e54f | 2001-10-29 16:44:37 +0000 | [diff] [blame] | 314 | self.handle_connect() |
Jeremy Hylton | f24339f | 2001-10-30 14:16:17 +0000 | [diff] [blame] | 315 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 316 | raise socket.error(err, errorcode[err]) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 318 | def accept(self): |
Jeremy Hylton | f32e459 | 2002-04-04 21:02:24 +0000 | [diff] [blame] | 319 | # XXX can return either an address pair or None |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 320 | try: |
| 321 | conn, addr = self.socket.accept() |
| 322 | return conn, addr |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 323 | except socket.error as why: |
Georg Brandl | 7357c23 | 2008-01-06 18:23:30 +0000 | [diff] [blame] | 324 | if why.args[0] == EWOULDBLOCK: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 325 | pass |
| 326 | else: |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 327 | raise |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 328 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 329 | def send(self, data): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 330 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 331 | result = self.socket.send(data) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 332 | return result |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 333 | except socket.error as why: |
Georg Brandl | 7357c23 | 2008-01-06 18:23:30 +0000 | [diff] [blame] | 334 | if why.args[0] == EWOULDBLOCK: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 335 | return 0 |
| 336 | else: |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 337 | raise |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 338 | return 0 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 340 | def recv(self, buffer_size): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 341 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 342 | data = self.socket.recv(buffer_size) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 343 | if not data: |
| 344 | # a closed connection is indicated by signaling |
| 345 | # a read condition, and having recv() return 0. |
| 346 | self.handle_close() |
Guido van Rossum | df4a743 | 2007-07-18 20:57:44 +0000 | [diff] [blame] | 347 | return b'' |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 348 | else: |
| 349 | return data |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 350 | except socket.error as why: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 351 | # winsock sometimes throws ENOTCONN |
Georg Brandl | 7357c23 | 2008-01-06 18:23:30 +0000 | [diff] [blame] | 352 | if why.args[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 353 | self.handle_close() |
Guido van Rossum | df4a743 | 2007-07-18 20:57:44 +0000 | [diff] [blame] | 354 | return b'' |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 355 | else: |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 356 | raise |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 358 | def close(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 359 | self.del_channel() |
| 360 | self.socket.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 361 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 362 | # cheap inheritance, used to pass all other attribute |
| 363 | # references to the underlying socket object. |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 364 | def __getattr__(self, attr): |
| 365 | return getattr(self.socket, attr) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 366 | |
Andrew M. Kuchling | c07fb2f | 2003-02-14 01:13:01 +0000 | [diff] [blame] | 367 | # log and log_info may be overridden to provide more sophisticated |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 368 | # logging and warning methods. In general, log is for 'hit' logging |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 369 | # and 'log_info' is for informational, warning and error logging. |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 370 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 371 | def log(self, message): |
| 372 | sys.stderr.write('log: %s\n' % str(message)) |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 374 | def log_info(self, message, type='info'): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 375 | if __debug__ or type != 'info': |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 376 | print('%s: %s' % (type, message)) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 378 | def handle_read_event(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 379 | if self.accepting: |
| 380 | # for an accepting socket, getting a read implies |
| 381 | # that we are connected |
| 382 | if not self.connected: |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 383 | self.connected = True |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 384 | self.handle_accept() |
| 385 | elif not self.connected: |
| 386 | self.handle_connect() |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 387 | self.connected = True |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 388 | self.handle_read() |
| 389 | else: |
| 390 | self.handle_read() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 392 | def handle_write_event(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 393 | # getting a write implies that we are connected |
| 394 | if not self.connected: |
| 395 | self.handle_connect() |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 396 | self.connected = True |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 397 | self.handle_write() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 399 | def handle_expt_event(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 400 | self.handle_expt() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 402 | def handle_error(self): |
Jeremy Hylton | a8b5f7d | 2001-08-10 14:30:35 +0000 | [diff] [blame] | 403 | nil, t, v, tbinfo = compact_traceback() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 404 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 405 | # sometimes a user repr method will crash. |
| 406 | try: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 407 | self_repr = repr(self) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 408 | except: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 409 | self_repr = '<__repr__(self) failed for object at %0x>' % id(self) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 411 | self.log_info( |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 412 | 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( |
| 413 | self_repr, |
| 414 | t, |
| 415 | v, |
| 416 | tbinfo |
| 417 | ), |
| 418 | 'error' |
| 419 | ) |
| 420 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 421 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 422 | def handle_expt(self): |
| 423 | self.log_info('unhandled exception', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 425 | def handle_read(self): |
| 426 | self.log_info('unhandled read event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 427 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 428 | def handle_write(self): |
| 429 | self.log_info('unhandled write event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 431 | def handle_connect(self): |
| 432 | self.log_info('unhandled connect event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 433 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 434 | def handle_accept(self): |
| 435 | self.log_info('unhandled accept event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 436 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 437 | def handle_close(self): |
| 438 | self.log_info('unhandled close event', 'warning') |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 439 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 440 | |
| 441 | # --------------------------------------------------------------------------- |
| 442 | # adds simple buffered output capability, useful for simple clients. |
| 443 | # [for more sophisticated usage use asynchat.async_chat] |
| 444 | # --------------------------------------------------------------------------- |
| 445 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 446 | class dispatcher_with_send(dispatcher): |
| 447 | |
Andrew M. Kuchling | 67867ea | 2004-03-21 20:03:18 +0000 | [diff] [blame] | 448 | def __init__(self, sock=None, map=None): |
| 449 | dispatcher.__init__(self, sock, map) |
Guido van Rossum | df4a743 | 2007-07-18 20:57:44 +0000 | [diff] [blame] | 450 | self.out_buffer = b'' |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 452 | def initiate_send(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 453 | num_sent = 0 |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 454 | num_sent = dispatcher.send(self, self.out_buffer[:512]) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 455 | self.out_buffer = self.out_buffer[num_sent:] |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 456 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 457 | def handle_write(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 458 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 459 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 460 | def writable(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 461 | return (not self.connected) or len(self.out_buffer) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 462 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 463 | def send(self, data): |
Guido van Rossum | df4a743 | 2007-07-18 20:57:44 +0000 | [diff] [blame] | 464 | assert isinstance(data, bytes) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 465 | if self.debug: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 466 | self.log_info('sending %s' % repr(data)) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 467 | self.out_buffer = self.out_buffer + data |
| 468 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 469 | |
| 470 | # --------------------------------------------------------------------------- |
| 471 | # used for debugging. |
| 472 | # --------------------------------------------------------------------------- |
| 473 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 474 | def compact_traceback(): |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 475 | t, v, tb = sys.exc_info() |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 476 | tbinfo = [] |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 477 | assert tb # Must have a traceback |
| 478 | while tb: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 479 | tbinfo.append(( |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 480 | tb.tb_frame.f_code.co_filename, |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 481 | tb.tb_frame.f_code.co_name, |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 482 | str(tb.tb_lineno) |
| 483 | )) |
| 484 | tb = tb.tb_next |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 485 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 486 | # just to be safe |
| 487 | del tb |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 488 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 489 | file, function, line = tbinfo[-1] |
Guido van Rossum | 12e9668 | 2002-09-13 14:09:26 +0000 | [diff] [blame] | 490 | info = ' '.join(['[%s|%s|%s]' % x for x in tbinfo]) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 491 | return (file, function, line), t, v, info |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 493 | def close_all(map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 494 | if map is None: |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 495 | map = socket_map |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 496 | for x in map.values(): |
| 497 | x.socket.close() |
| 498 | map.clear() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 499 | |
| 500 | # Asynchronous File I/O: |
| 501 | # |
| 502 | # After a little research (reading man pages on various unixen, and |
| 503 | # digging through the linux kernel), I've determined that select() |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 504 | # isn't meant for doing asynchronous file i/o. |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 505 | # Heartening, though - reading linux/mm/filemap.c shows that linux |
| 506 | # supports asynchronous read-ahead. So _MOST_ of the time, the data |
| 507 | # will be sitting in memory for us already when we go to read it. |
| 508 | # |
| 509 | # What other OS's (besides NT) support async file i/o? [VMS?] |
| 510 | # |
| 511 | # Regardless, this is useful for pipes, and stdin/stdout... |
| 512 | |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 513 | if os.name == 'posix': |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 514 | import fcntl |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 515 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 516 | class file_wrapper: |
| 517 | # here we override just enough to make a file |
| 518 | # look like a socket for the purposes of asyncore. |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 519 | |
| 520 | def __init__(self, fd): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 521 | self.fd = fd |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 522 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 523 | def recv(self, *args): |
Jeremy Hylton | f32e459 | 2002-04-04 21:02:24 +0000 | [diff] [blame] | 524 | return os.read(self.fd, *args) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 525 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 526 | def send(self, *args): |
Jeremy Hylton | f32e459 | 2002-04-04 21:02:24 +0000 | [diff] [blame] | 527 | return os.write(self.fd, *args) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 528 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 529 | read = recv |
| 530 | write = send |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 532 | def close(self): |
Andrew M. Kuchling | dfa74b9 | 2004-07-10 15:51:19 +0000 | [diff] [blame] | 533 | os.close(self.fd) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 534 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 535 | def fileno(self): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 536 | return self.fd |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 538 | class file_dispatcher(dispatcher): |
| 539 | |
Andrew M. Kuchling | 67867ea | 2004-03-21 20:03:18 +0000 | [diff] [blame] | 540 | def __init__(self, fd, map=None): |
| 541 | dispatcher.__init__(self, None, map) |
Andrew M. Kuchling | 68522b1 | 2004-03-21 19:46:16 +0000 | [diff] [blame] | 542 | self.connected = True |
Andrew M. Kuchling | dfa74b9 | 2004-07-10 15:51:19 +0000 | [diff] [blame] | 543 | self.set_file(fd) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 544 | # set it to non-blocking mode |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 545 | flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0) |
Fred Drake | a94414a | 2001-05-10 15:33:31 +0000 | [diff] [blame] | 546 | flags = flags | os.O_NONBLOCK |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 547 | fcntl.fcntl(fd, fcntl.F_SETFL, flags) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 548 | |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 549 | def set_file(self, fd): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 550 | self._fileno = fd |
Guido van Rossum | d560ace | 2002-09-12 04:57:29 +0000 | [diff] [blame] | 551 | self.socket = file_wrapper(fd) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 552 | self.add_channel() |