def handle_write(self): # # Deal with the case where send() is blocked by recv(), that happens # when we are using SSL and recv() returned WANT_WRITE. In the common # case, this costs just one extra if in the fast path. # if self.send_blocked: logging.debug('stream: handle_write() => handle_read()') POLLER.set_readable(self) if not self.send_octets: POLLER.unset_writable(self) self.send_blocked = False self.handle_read() return status, count = self.sock.sosend(self.send_octets) # # Optimisation: reorder if branches such that the ones more relevant # for better performance come first. Testing in early 2011 showed that # this arrangement allows to gain a little more speed. (And the code # is still readable.) # if status == SUCCESS and count > 0: self.bytes_out += count if count == len(self.send_octets): POLLER.unset_writable(self) self.send_octets = EMPTY_STRING self.send_complete(self) return if count < len(self.send_octets): self.send_octets = six.buff(self.send_octets, count) return raise RuntimeError('stream: invalid count') if status == WANT_WRITE: return if status == WANT_READ: logging.debug('stream: blocking recv()') POLLER.unset_writable(self) POLLER.set_readable(self) self.recv_blocked = True return if status == CONNRST and count == 0: logging.debug('stream: RST') self.conn_rst = True POLLER.close(self) return if status == SUCCESS and count < 0: raise RuntimeError('stream: negative count') raise RuntimeError('stream: invalid status')
def handle_read(self): # # Deal with the case where recv() is blocked by send(), that happens # when we are using SSL and write() returned WANT_READ. In the common # case, this costs just one extra if in the fast path. # if self.recv_blocked: logging.debug('stream: handle_read() => handle_write()') POLLER.set_writable(self) if self.recv_bytes <= 0: POLLER.unset_readable(self) self.recv_blocked = False self.handle_write() return status, octets = self.sock.sorecv(self.recv_bytes) # # Optimisation: reorder if branches such that the ones more relevant # for better performance come first. Testing in early 2011 showed that # this arrangement allows to gain a little more speed. (And the code # is still readable.) # if status == SUCCESS and octets: self.bytes_in += len(octets) self.recv_bytes = 0 POLLER.unset_readable(self) self.recv_complete(self, octets) return if status == WANT_READ: return if status == WANT_WRITE: logging.debug('stream: blocking send()') POLLER.unset_readable(self) POLLER.set_writable(self) self.send_blocked = True return if status == SUCCESS and not octets: logging.debug('stream: EOF') self.eof = True POLLER.close(self) return if status == CONNRST and not octets: logging.debug('stream: RST ') self.conn_rst = True POLLER.close(self) return raise RuntimeError('stream: invalid status')
def close(self): ''' Close the stream ''' POLLER.close(self)
def _connection_made_error(self, exception): ''' Invoked when connection_made() callback fails ''' logging.warning('stream: connection_made() failed: %s', str(exception)) POLLER.close(self)