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 | |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 49 | import exceptions |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 50 | import select |
| 51 | import socket |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 52 | import sys |
| 53 | |
| 54 | import os |
Jeremy Hylton | a8b5f7d | 2001-08-10 14:30:35 +0000 | [diff] [blame] | 55 | from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, \ |
Jeremy Hylton | e16e54f | 2001-10-29 16:44:37 +0000 | [diff] [blame] | 56 | ENOTCONN, ESHUTDOWN, EINTR, EISCONN |
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 | |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 63 | class ExitNow (exceptions.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 | |
| 66 | DEBUG = 0 |
| 67 | |
| 68 | def poll (timeout=0.0, map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 69 | if map is None: |
| 70 | map = socket_map |
| 71 | if map: |
| 72 | r = []; w = []; e = [] |
| 73 | for fd, obj in map.items(): |
| 74 | if obj.readable(): |
| 75 | r.append (fd) |
| 76 | if obj.writable(): |
| 77 | w.append (fd) |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 78 | try: |
| 79 | r,w,e = select.select (r,w,e, timeout) |
| 80 | except select.error, err: |
| 81 | if err[0] != EINTR: |
| 82 | raise |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 83 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 84 | if DEBUG: |
| 85 | print r,w,e |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 86 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 87 | for fd in r: |
| 88 | try: |
| 89 | obj = map[fd] |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 90 | except KeyError: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 91 | continue |
| 92 | |
| 93 | try: |
| 94 | obj.handle_read_event() |
| 95 | except ExitNow: |
| 96 | raise ExitNow |
| 97 | except: |
| 98 | obj.handle_error() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 99 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 100 | for fd in w: |
| 101 | try: |
| 102 | obj = map[fd] |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 103 | except KeyError: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 104 | continue |
| 105 | |
| 106 | try: |
| 107 | obj.handle_write_event() |
| 108 | except ExitNow: |
| 109 | raise ExitNow |
| 110 | except: |
| 111 | obj.handle_error() |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 112 | |
| 113 | def poll2 (timeout=0.0, map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 114 | import poll |
| 115 | if map is None: |
| 116 | map=socket_map |
Martin v. Löwis | f6cc07c | 2001-09-19 17:31:47 +0000 | [diff] [blame] | 117 | if timeout is not None: |
| 118 | # timeout is in milliseconds |
| 119 | timeout = int(timeout*1000) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 120 | if map: |
| 121 | l = [] |
| 122 | for fd, obj in map.items(): |
| 123 | flags = 0 |
| 124 | if obj.readable(): |
| 125 | flags = poll.POLLIN |
| 126 | if obj.writable(): |
| 127 | flags = flags | poll.POLLOUT |
| 128 | if flags: |
| 129 | l.append ((fd, flags)) |
| 130 | r = poll.poll (l, timeout) |
| 131 | for fd, flags in r: |
| 132 | try: |
| 133 | obj = map[fd] |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 134 | except KeyError: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 135 | continue |
| 136 | |
| 137 | try: |
| 138 | if (flags & poll.POLLIN): |
| 139 | obj.handle_read_event() |
| 140 | if (flags & poll.POLLOUT): |
| 141 | obj.handle_write_event() |
| 142 | except ExitNow: |
| 143 | raise ExitNow |
| 144 | except: |
| 145 | obj.handle_error() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 146 | |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 147 | def poll3 (timeout=0.0, map=None): |
| 148 | # Use the poll() support added to the select module in Python 2.0 |
| 149 | if map is None: |
| 150 | map=socket_map |
Martin v. Löwis | f6cc07c | 2001-09-19 17:31:47 +0000 | [diff] [blame] | 151 | if timeout is not None: |
| 152 | # timeout is in milliseconds |
| 153 | timeout = int(timeout*1000) |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 154 | pollster = select.poll() |
| 155 | if map: |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 156 | for fd, obj in map.items(): |
| 157 | flags = 0 |
| 158 | if obj.readable(): |
| 159 | flags = select.POLLIN |
| 160 | if obj.writable(): |
| 161 | flags = flags | select.POLLOUT |
| 162 | if flags: |
| 163 | pollster.register(fd, flags) |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 164 | try: |
| 165 | r = pollster.poll (timeout) |
| 166 | except select.error, err: |
| 167 | if err[0] != EINTR: |
| 168 | raise |
| 169 | r = [] |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 170 | for fd, flags in r: |
| 171 | try: |
| 172 | obj = map[fd] |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 173 | except KeyError: |
Martin v. Löwis | 6ec9a36 | 2001-10-09 10:10:33 +0000 | [diff] [blame] | 174 | continue |
| 175 | |
| 176 | try: |
| 177 | if (flags & select.POLLIN): |
| 178 | obj.handle_read_event() |
| 179 | if (flags & select.POLLOUT): |
| 180 | obj.handle_write_event() |
| 181 | except ExitNow: |
| 182 | raise ExitNow |
| 183 | except: |
| 184 | obj.handle_error() |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 185 | |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 186 | def loop (timeout=30.0, use_poll=0, map=None): |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 187 | |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 188 | if map is None: |
| 189 | map=socket_map |
| 190 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 191 | if use_poll: |
Andrew M. Kuchling | af6963c | 2001-01-24 15:50:19 +0000 | [diff] [blame] | 192 | if hasattr (select, 'poll'): |
| 193 | poll_fun = poll3 |
| 194 | else: |
| 195 | poll_fun = poll2 |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 196 | else: |
| 197 | poll_fun = poll |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 198 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 199 | while map: |
| 200 | poll_fun (timeout, map) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 201 | |
| 202 | class dispatcher: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 203 | debug = 0 |
| 204 | connected = 0 |
| 205 | accepting = 0 |
| 206 | closing = 0 |
| 207 | addr = None |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 208 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 209 | def __init__ (self, sock=None, map=None): |
| 210 | if sock: |
| 211 | self.set_socket (sock, map) |
| 212 | # I think it should inherit this anyway |
| 213 | self.socket.setblocking (0) |
| 214 | self.connected = 1 |
Andrew M. Kuchling | 4602c1b | 2001-10-03 17:07:25 +0000 | [diff] [blame] | 215 | self.addr = sock.getpeername() |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 216 | else: |
| 217 | self.socket = None |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 218 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 219 | def __repr__ (self): |
Martin v. Löwis | 29103c7 | 2001-10-18 17:33:19 +0000 | [diff] [blame] | 220 | status = [self.__class__.__module__+"."+self.__class__.__name__] |
| 221 | if self.accepting and self.addr: |
| 222 | status.append ('listening') |
| 223 | elif self.connected: |
| 224 | status.append ('connected') |
| 225 | if self.addr is not None: |
| 226 | try: |
| 227 | status.append ('%s:%d' % self.addr) |
| 228 | except TypeError: |
| 229 | status.append (repr(self.addr)) |
| 230 | return '<%s at %#x>' % (' '.join (status), id (self)) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 231 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 232 | def add_channel (self, map=None): |
| 233 | #self.log_info ('adding channel %s' % self) |
| 234 | if map is None: |
| 235 | map=socket_map |
| 236 | map [self._fileno] = self |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 237 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 238 | def del_channel (self, map=None): |
| 239 | fd = self._fileno |
| 240 | if map is None: |
| 241 | map=socket_map |
| 242 | if map.has_key (fd): |
| 243 | #self.log_info ('closing channel %d:%s' % (fd, self)) |
| 244 | del map [fd] |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 245 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 246 | def create_socket (self, family, type): |
| 247 | self.family_and_type = family, type |
| 248 | self.socket = socket.socket (family, type) |
| 249 | self.socket.setblocking(0) |
| 250 | self._fileno = self.socket.fileno() |
| 251 | self.add_channel() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 252 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 253 | def set_socket (self, sock, map=None): |
Jeremy Hylton | fbd5797 | 2001-10-29 16:32:19 +0000 | [diff] [blame] | 254 | self.socket = sock |
| 255 | ## self.__dict__['socket'] = sock |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 256 | self._fileno = sock.fileno() |
| 257 | self.add_channel (map) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 258 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 259 | def set_reuse_addr (self): |
| 260 | # try to re-use a server port if possible |
| 261 | try: |
| 262 | self.socket.setsockopt ( |
| 263 | socket.SOL_SOCKET, socket.SO_REUSEADDR, |
Jeremy Hylton | a8b5f7d | 2001-08-10 14:30:35 +0000 | [diff] [blame] | 264 | self.socket.getsockopt (socket.SOL_SOCKET, |
| 265 | socket.SO_REUSEADDR) | 1 |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 266 | ) |
Fred Drake | 9f9b593 | 2001-05-11 18:28:54 +0000 | [diff] [blame] | 267 | except socket.error: |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 268 | pass |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 269 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 270 | # ================================================== |
| 271 | # predicates for select() |
| 272 | # these are used as filters for the lists of sockets |
| 273 | # to pass to select(). |
| 274 | # ================================================== |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 275 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 276 | def readable (self): |
| 277 | return 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 278 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 279 | if os.name == 'mac': |
| 280 | # The macintosh will select a listening socket for |
| 281 | # write if you let it. What might this mean? |
| 282 | def writable (self): |
| 283 | return not self.accepting |
| 284 | else: |
| 285 | def writable (self): |
| 286 | return 1 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 287 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 288 | # ================================================== |
| 289 | # socket object methods. |
| 290 | # ================================================== |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 291 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 292 | def listen (self, num): |
| 293 | self.accepting = 1 |
| 294 | if os.name == 'nt' and num > 5: |
| 295 | num = 1 |
| 296 | return self.socket.listen (num) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 297 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 298 | def bind (self, addr): |
| 299 | self.addr = addr |
| 300 | return self.socket.bind (addr) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 301 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 302 | def connect (self, address): |
| 303 | self.connected = 0 |
Jeremy Hylton | e16e54f | 2001-10-29 16:44:37 +0000 | [diff] [blame] | 304 | err = self.socket.connect_ex(address) |
| 305 | if err in (EINPROGRESS, EALREADY, EWOULDBLOCK): |
| 306 | return |
| 307 | if err in (0, EISCONN): |
| 308 | self.addr = address |
| 309 | self.connected = 1 |
| 310 | self.handle_connect() |
Jeremy Hylton | f24339f | 2001-10-30 14:16:17 +0000 | [diff] [blame] | 311 | else: |
| 312 | raise socket.error, err |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 313 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 314 | def accept (self): |
| 315 | try: |
| 316 | conn, addr = self.socket.accept() |
| 317 | return conn, addr |
| 318 | except socket.error, why: |
| 319 | if why[0] == EWOULDBLOCK: |
| 320 | pass |
| 321 | else: |
| 322 | raise socket.error, why |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 323 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 324 | def send (self, data): |
| 325 | try: |
| 326 | result = self.socket.send (data) |
| 327 | return result |
| 328 | except socket.error, why: |
| 329 | if why[0] == EWOULDBLOCK: |
| 330 | return 0 |
| 331 | else: |
| 332 | raise socket.error, why |
| 333 | return 0 |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 334 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 335 | def recv (self, buffer_size): |
| 336 | try: |
| 337 | data = self.socket.recv (buffer_size) |
| 338 | if not data: |
| 339 | # a closed connection is indicated by signaling |
| 340 | # a read condition, and having recv() return 0. |
| 341 | self.handle_close() |
| 342 | return '' |
| 343 | else: |
| 344 | return data |
| 345 | except socket.error, why: |
| 346 | # winsock sometimes throws ENOTCONN |
| 347 | if why[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN]: |
| 348 | self.handle_close() |
| 349 | return '' |
| 350 | else: |
| 351 | raise socket.error, why |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 352 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 353 | def close (self): |
| 354 | self.del_channel() |
| 355 | self.socket.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 356 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 357 | # cheap inheritance, used to pass all other attribute |
| 358 | # references to the underlying socket object. |
| 359 | def __getattr__ (self, attr): |
| 360 | return getattr (self.socket, attr) |
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 | # log and log_info maybe overriden to provide more sophisitcated |
| 363 | # logging and warning methods. In general, log is for 'hit' logging |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 364 | # and 'log_info' is for informational, warning and error logging. |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 365 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 366 | def log (self, message): |
| 367 | sys.stderr.write ('log: %s\n' % str(message)) |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 368 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 369 | def log_info (self, message, type='info'): |
| 370 | if __debug__ or type != 'info': |
| 371 | print '%s: %s' % (type, message) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 372 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 373 | def handle_read_event (self): |
| 374 | if self.accepting: |
| 375 | # for an accepting socket, getting a read implies |
| 376 | # that we are connected |
| 377 | if not self.connected: |
| 378 | self.connected = 1 |
| 379 | self.handle_accept() |
| 380 | elif not self.connected: |
| 381 | self.handle_connect() |
| 382 | self.connected = 1 |
| 383 | self.handle_read() |
| 384 | else: |
| 385 | self.handle_read() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 386 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 387 | def handle_write_event (self): |
| 388 | # getting a write implies that we are connected |
| 389 | if not self.connected: |
| 390 | self.handle_connect() |
| 391 | self.connected = 1 |
| 392 | self.handle_write() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 393 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 394 | def handle_expt_event (self): |
| 395 | self.handle_expt() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 396 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 397 | def handle_error (self): |
Jeremy Hylton | a8b5f7d | 2001-08-10 14:30:35 +0000 | [diff] [blame] | 398 | nil, t, v, tbinfo = compact_traceback() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 399 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 400 | # sometimes a user repr method will crash. |
| 401 | try: |
| 402 | self_repr = repr (self) |
| 403 | except: |
| 404 | self_repr = '<__repr__ (self) failed for object at %0x>' % id(self) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 405 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 406 | self.log_info ( |
| 407 | 'uncaptured python exception, closing channel %s (%s:%s %s)' % ( |
| 408 | self_repr, |
| 409 | t, |
| 410 | v, |
| 411 | tbinfo |
| 412 | ), |
| 413 | 'error' |
| 414 | ) |
| 415 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 416 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 417 | def handle_expt (self): |
| 418 | self.log_info ('unhandled exception', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 419 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 420 | def handle_read (self): |
| 421 | self.log_info ('unhandled read event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 422 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 423 | def handle_write (self): |
| 424 | self.log_info ('unhandled write event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 425 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 426 | def handle_connect (self): |
| 427 | self.log_info ('unhandled connect event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 428 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 429 | def handle_accept (self): |
| 430 | self.log_info ('unhandled accept event', 'warning') |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 431 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 432 | def handle_close (self): |
| 433 | self.log_info ('unhandled close event', 'warning') |
| 434 | self.close() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 435 | |
| 436 | # --------------------------------------------------------------------------- |
| 437 | # adds simple buffered output capability, useful for simple clients. |
| 438 | # [for more sophisticated usage use asynchat.async_chat] |
| 439 | # --------------------------------------------------------------------------- |
| 440 | |
| 441 | class dispatcher_with_send (dispatcher): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 442 | def __init__ (self, sock=None): |
| 443 | dispatcher.__init__ (self, sock) |
| 444 | self.out_buffer = '' |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 445 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 446 | def initiate_send (self): |
| 447 | num_sent = 0 |
| 448 | num_sent = dispatcher.send (self, self.out_buffer[:512]) |
| 449 | self.out_buffer = self.out_buffer[num_sent:] |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 450 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 451 | def handle_write (self): |
| 452 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 453 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 454 | def writable (self): |
| 455 | return (not self.connected) or len(self.out_buffer) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 456 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 457 | def send (self, data): |
| 458 | if self.debug: |
| 459 | self.log_info ('sending %s' % repr(data)) |
| 460 | self.out_buffer = self.out_buffer + data |
| 461 | self.initiate_send() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 462 | |
| 463 | # --------------------------------------------------------------------------- |
| 464 | # used for debugging. |
| 465 | # --------------------------------------------------------------------------- |
| 466 | |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 467 | def compact_traceback (): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 468 | t,v,tb = sys.exc_info() |
| 469 | tbinfo = [] |
| 470 | while 1: |
| 471 | tbinfo.append (( |
| 472 | tb.tb_frame.f_code.co_filename, |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 473 | tb.tb_frame.f_code.co_name, |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 474 | str(tb.tb_lineno) |
| 475 | )) |
| 476 | tb = tb.tb_next |
| 477 | if not tb: |
| 478 | break |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 479 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 480 | # just to be safe |
| 481 | del tb |
Guido van Rossum | a8d0f4f | 1999-06-08 13:20:05 +0000 | [diff] [blame] | 482 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 483 | file, function, line = tbinfo[-1] |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 484 | info = '[' + '] ['.join(map(lambda x: '|'.join(x), tbinfo)) + ']' |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 485 | return (file, function, line), t, v, info |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 486 | |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 487 | def close_all (map=None): |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 488 | if map is None: |
| 489 | map=socket_map |
| 490 | for x in map.values(): |
| 491 | x.socket.close() |
| 492 | map.clear() |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 493 | |
| 494 | # Asynchronous File I/O: |
| 495 | # |
| 496 | # After a little research (reading man pages on various unixen, and |
| 497 | # digging through the linux kernel), I've determined that select() |
| 498 | # isn't meant for doing doing asynchronous file i/o. |
| 499 | # Heartening, though - reading linux/mm/filemap.c shows that linux |
| 500 | # supports asynchronous read-ahead. So _MOST_ of the time, the data |
| 501 | # will be sitting in memory for us already when we go to read it. |
| 502 | # |
| 503 | # What other OS's (besides NT) support async file i/o? [VMS?] |
| 504 | # |
| 505 | # Regardless, this is useful for pipes, and stdin/stdout... |
| 506 | |
| 507 | import os |
| 508 | if os.name == 'posix': |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 509 | import fcntl |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 510 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 511 | class file_wrapper: |
| 512 | # here we override just enough to make a file |
| 513 | # look like a socket for the purposes of asyncore. |
| 514 | def __init__ (self, fd): |
| 515 | self.fd = fd |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 516 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 517 | def recv (self, *args): |
| 518 | return apply (os.read, (self.fd,)+args) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 519 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 520 | def send (self, *args): |
| 521 | return apply (os.write, (self.fd,)+args) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 522 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 523 | read = recv |
| 524 | write = send |
Andrew M. Kuchling | da85a27 | 2000-09-08 20:30:39 +0000 | [diff] [blame] | 525 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 526 | def close (self): |
| 527 | return os.close (self.fd) |
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 | def fileno (self): |
| 530 | return self.fd |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 531 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 532 | class file_dispatcher (dispatcher): |
| 533 | def __init__ (self, fd): |
| 534 | dispatcher.__init__ (self) |
| 535 | self.connected = 1 |
| 536 | # set it to non-blocking mode |
Fred Drake | a94414a | 2001-05-10 15:33:31 +0000 | [diff] [blame] | 537 | flags = fcntl.fcntl (fd, fcntl.F_GETFL, 0) |
| 538 | flags = flags | os.O_NONBLOCK |
| 539 | fcntl.fcntl (fd, fcntl.F_SETFL, flags) |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 540 | self.set_file (fd) |
Guido van Rossum | 0039d7b | 1999-01-12 20:19:27 +0000 | [diff] [blame] | 541 | |
Fred Drake | 526a182 | 2000-09-11 04:00:46 +0000 | [diff] [blame] | 542 | def set_file (self, fd): |
| 543 | self._fileno = fd |
| 544 | self.socket = file_wrapper (fd) |
| 545 | self.add_channel() |