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