def _ready_write(self): # Do the actual writing buffer = self._write_buffer tot_bytes = 0 if self._paused_writing: return tot_bytes if not buffer: self.logger.warning('handling write on a 0 length buffer') try: while buffer: try: sent = self._sock.send(buffer[0]) if sent == 0: break merge_prefix(buffer, sent) buffer.popleft() tot_bytes += sent except self.SocketError as e: if self._write_continue(e): break else: raise except Exception: failure = sys.exc_info() else: if not self._write_buffer: self._event_loop.remove_writer(self._sock_fd) if self._closing: self._event_loop.call_soon(self._shutdown) return tot_bytes if not self._closing: self.abort(failure)
def _handle_write(self): # keep count how many bytes we write tot_bytes = 0 while self._write_buffer: try: sent = self.sock.send(self._write_buffer[0]) if sent == 0: # With OpenSSL, after send returns EWOULDBLOCK, # the very same string object must be used on the # next call to send. Therefore we suppress # merging the write buffer after an EWOULDBLOCK. # A cleaner solution would be to set # SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER, but this is # not yet accessible from python # (http://bugs.python.org/issue8240) break merge_prefix(self._write_buffer, sent) self._write_buffer.popleft() tot_bytes += sent except socket.error as e: if async_error(e): break else: LOGGER.warning("Write error on %s: %s", self, e) self.close() return if not self._write_buffer and self._write_callback: callback = self._write_callback self._write_callback = None self._may_run_callback(callback, tot_bytes) return tot_bytes
def _ready_write(self): # Do the actual writing buffer = self._write_buffer tot_bytes = 0 if not buffer: self.logger.warning('handling write on a 0 length buffer') try: while buffer: try: sent = self._sock.send(buffer[0]) if sent == 0: break merge_prefix(buffer, sent) buffer.popleft() tot_bytes += sent except self.SocketError as e: if self._write_continue(e): break else: raise except Exception: failure = sys.exc_info() else: if not self._write_buffer: self._event_loop.remove_writer(self._sock_fd) if self._closing: self._event_loop.call_soon(self._shutdown) return tot_bytes if not self._closing: self.abort(failure)
def test_merge_prefix(self): d = deque([b'abc', b'de', b'fghi', b'j']) merge_prefix(d, 5) self.assertEqual(d, deque([b'abcde', b'fghi', b'j'])) d = deque([b'abc', b'de', b'fghi', b'j']) merge_prefix(d, 4) self.assertEqual(d, deque([b'abcd', b'e', b'fghi', b'j'])) merge_prefix(d, 7) self.assertEqual(d, deque([b'abcdefg', b'hi', b'j'])) merge_prefix(d, 3) self.assertEqual(d, deque([b'abc', b'defg', b'hi', b'j'])) merge_prefix(d, 100) self.assertEqual(d, deque([b'abcdefghij']))