Reads from the socket and appends the result to the read buffer. Returns the number of bytes read. Returns 0 if there is nothing to read (i.e. the read returns EWOULDBLOCK or equivalent). On error closes the socket and raises an exception.
(self)
| 843 | self._add_io_state(ioloop.IOLoop.READ) |
| 844 | |
| 845 | def _read_to_buffer(self) -> Optional[int]: |
| 846 | """Reads from the socket and appends the result to the read buffer. |
| 847 | |
| 848 | Returns the number of bytes read. Returns 0 if there is nothing |
| 849 | to read (i.e. the read returns EWOULDBLOCK or equivalent). On |
| 850 | error closes the socket and raises an exception. |
| 851 | """ |
| 852 | try: |
| 853 | while True: |
| 854 | try: |
| 855 | if self._user_read_buffer: |
| 856 | buf = memoryview(self._read_buffer)[ |
| 857 | self._read_buffer_size : |
| 858 | ] # type: Union[memoryview, bytearray] |
| 859 | else: |
| 860 | buf = bytearray(self.read_chunk_size) |
| 861 | bytes_read = self.read_from_fd(buf) |
| 862 | except OSError as e: |
| 863 | # ssl.SSLError is a subclass of socket.error |
| 864 | if self._is_connreset(e): |
| 865 | # Treat ECONNRESET as a connection close rather than |
| 866 | # an error to minimize log spam (the exception will |
| 867 | # be available on self.error for apps that care). |
| 868 | self.close(exc_info=e) |
| 869 | return None |
| 870 | self.close(exc_info=e) |
| 871 | raise |
| 872 | break |
| 873 | if bytes_read is None: |
| 874 | return 0 |
| 875 | elif bytes_read == 0: |
| 876 | self.close() |
| 877 | return 0 |
| 878 | if not self._user_read_buffer: |
| 879 | self._read_buffer += memoryview(buf)[:bytes_read] |
| 880 | self._read_buffer_size += bytes_read |
| 881 | finally: |
| 882 | # Break the reference to buf so we don't waste a chunk's worth of |
| 883 | # memory in case an exception hangs on to our stack frame. |
| 884 | del buf |
| 885 | if self._read_buffer_size > self.max_buffer_size: |
| 886 | gen_log.error("Reached maximum read buffer size") |
| 887 | self.close() |
| 888 | raise StreamBufferFullError("Reached maximum read buffer size") |
| 889 | return bytes_read |
| 890 | |
| 891 | def _read_from_buffer(self, pos: int) -> None: |
| 892 | """Attempts to complete the currently-pending read from the buffer. |
no test coverage detected