| 144 | self.noread() |
| 145 | |
| 146 | def try_connect(self): |
| 147 | if self.connect_to and self.shut_write: |
| 148 | self.noread() |
| 149 | self.connect_to = None |
| 150 | if not self.connect_to: |
| 151 | return # already connected |
| 152 | self.rsock.setblocking(False) |
| 153 | debug3('%r: trying connect to %r' % (self, self.connect_to)) |
| 154 | try: |
| 155 | self.rsock.connect(self.connect_to) |
| 156 | # connected successfully (Linux) |
| 157 | self.connect_to = None |
| 158 | except socket.error: |
| 159 | _, e = sys.exc_info()[:2] |
| 160 | debug3('%r: connect result: %s' % (self, e)) |
| 161 | if e.args[0] == errno.EINVAL: |
| 162 | # this is what happens when you call connect() on a socket |
| 163 | # that is now connected but returned EINPROGRESS last time, |
| 164 | # on BSD, on python pre-2.5.1. We need to use getsockopt() |
| 165 | # to get the "real" error. Later pythons do this |
| 166 | # automatically, so this code won't run. |
| 167 | realerr = self.rsock.getsockopt(socket.SOL_SOCKET, |
| 168 | socket.SO_ERROR) |
| 169 | e = socket.error(realerr, os.strerror(realerr)) |
| 170 | debug3('%r: fixed connect result: %s' % (self, e)) |
| 171 | if e.args[0] in [errno.EINPROGRESS, errno.EALREADY]: |
| 172 | pass # not connected yet |
| 173 | elif sys.platform == 'win32' and e.args[0] == errno.WSAEWOULDBLOCK: # 10035 |
| 174 | pass # not connected yet |
| 175 | elif e.args[0] == 0: |
| 176 | if sys.platform == 'win32': |
| 177 | # On Windows "real" error of EINVAL could be 0, when socket is in connecting state |
| 178 | pass |
| 179 | else: |
| 180 | # connected successfully (weird Linux bug?) |
| 181 | # Sometimes Linux seems to return EINVAL when it isn't |
| 182 | # invalid. This *may* be caused by a race condition |
| 183 | # between connect() and getsockopt(SO_ERROR) (ie. it |
| 184 | # finishes connecting in between the two, so there is no |
| 185 | # longer an error). However, I'm not sure of that. |
| 186 | # |
| 187 | # I did get at least one report that the problem went away |
| 188 | # when we added this, however. |
| 189 | self.connect_to = None |
| 190 | elif e.args[0] == errno.EISCONN: |
| 191 | # connected successfully (BSD + Windows) |
| 192 | self.connect_to = None |
| 193 | elif e.args[0] in NET_ERRS + [errno.EACCES, errno.EPERM]: |
| 194 | # a "normal" kind of error |
| 195 | self.connect_to = None |
| 196 | self.seterr(e) |
| 197 | else: |
| 198 | raise # error we've never heard of?! barf completely. |
| 199 | |
| 200 | def noread(self): |
| 201 | if not self.shut_read: |