| 87 | |
| 88 | _swcount = 0 |
| 89 | class SockWrapper: |
| 90 | def __init__(self, rsock, wsock, connect_to=None, peername=None): |
| 91 | global _swcount |
| 92 | _swcount += 1 |
| 93 | debug3('creating new SockWrapper (%d now exist)\n' % _swcount) |
| 94 | self.exc = None |
| 95 | self.rsock = rsock |
| 96 | self.wsock = wsock |
| 97 | self.shut_read = self.shut_write = False |
| 98 | self.buf = [] |
| 99 | self.connect_to = connect_to |
| 100 | self.peername = peername or _try_peername(self.rsock) |
| 101 | self.try_connect() |
| 102 | |
| 103 | def __del__(self): |
| 104 | global _swcount |
| 105 | _swcount -= 1 |
| 106 | debug1('%r: deleting (%d remain)\n' % (self, _swcount)) |
| 107 | if self.exc: |
| 108 | debug1('%r: error was: %s\n' % (self, self.exc)) |
| 109 | |
| 110 | def __repr__(self): |
| 111 | if self.rsock == self.wsock: |
| 112 | fds = '#%d' % self.rsock.fileno() |
| 113 | else: |
| 114 | fds = '#%d,%d' % (self.rsock.fileno(), self.wsock.fileno()) |
| 115 | return 'SW%s:%s' % (fds, self.peername) |
| 116 | |
| 117 | def seterr(self, e): |
| 118 | if not self.exc: |
| 119 | self.exc = e |
| 120 | self.nowrite() |
| 121 | self.noread() |
| 122 | |
| 123 | def try_connect(self): |
| 124 | if self.connect_to and self.shut_write: |
| 125 | self.noread() |
| 126 | self.connect_to = None |
| 127 | if not self.connect_to: |
| 128 | return # already connected |
| 129 | self.rsock.setblocking(False) |
| 130 | debug3('%r: trying connect to %r\n' % (self, self.connect_to)) |
| 131 | if socket.inet_aton(self.connect_to[0])[0] == '\0': |
| 132 | self.seterr(Exception("Can't connect to %r: " |
| 133 | "IP address starts with zero\n" |
| 134 | % (self.connect_to,))) |
| 135 | self.connect_to = None |
| 136 | return |
| 137 | try: |
| 138 | self.rsock.connect(self.connect_to) |
| 139 | # connected successfully (Linux) |
| 140 | self.connect_to = None |
| 141 | except socket.error, e: |
| 142 | debug3('%r: connect result: %s\n' % (self, e)) |
| 143 | if e.args[0] == errno.EINVAL: |
| 144 | # this is what happens when you call connect() on a socket |
| 145 | # that is now connected but returned EINPROGRESS last time, |
| 146 | # on BSD, on python pre-2.5.1. We need to use getsockopt() |
no outgoing calls
no test coverage detected