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