Read an entire "mysql packet" in its entirety from the network and return a MysqlPacket type that represents the results. :raise OperationalError: If the connection to the MySQL server is lost. :raise InternalError: If the packet sequence number is wrong.
(self, packet_type=MysqlPacket)
| 761 | self._next_seq_id = (self._next_seq_id + 1) % 256 |
| 762 | |
| 763 | def _read_packet(self, packet_type=MysqlPacket): |
| 764 | """Read an entire "mysql packet" in its entirety from the network |
| 765 | and return a MysqlPacket type that represents the results. |
| 766 | |
| 767 | :raise OperationalError: If the connection to the MySQL server is lost. |
| 768 | :raise InternalError: If the packet sequence number is wrong. |
| 769 | """ |
| 770 | buff = bytearray() |
| 771 | while True: |
| 772 | packet_header = self._read_bytes(4) |
| 773 | # if DEBUG: dump_packet(packet_header) |
| 774 | |
| 775 | btrl, btrh, packet_number = struct.unpack("<HBB", packet_header) |
| 776 | bytes_to_read = btrl + (btrh << 16) |
| 777 | if packet_number != self._next_seq_id: |
| 778 | self._force_close() |
| 779 | if packet_number == 0: |
| 780 | # MariaDB sends error packet with seqno==0 when shutdown |
| 781 | raise err.OperationalError( |
| 782 | CR.CR_SERVER_LOST, |
| 783 | "Lost connection to MySQL server during query", |
| 784 | ) |
| 785 | raise err.InternalError( |
| 786 | "Packet sequence number wrong - got %d expected %d" |
| 787 | % (packet_number, self._next_seq_id) |
| 788 | ) |
| 789 | self._next_seq_id = (self._next_seq_id + 1) % 256 |
| 790 | |
| 791 | recv_data = self._read_bytes(bytes_to_read) |
| 792 | if DEBUG: |
| 793 | dump_packet(recv_data) |
| 794 | buff += recv_data |
| 795 | # https://dev.mysql.com/doc/internals/en/sending-more-than-16mbyte.html |
| 796 | if bytes_to_read < MAX_PACKET_LEN: |
| 797 | break |
| 798 | |
| 799 | packet = packet_type(bytes(buff), self.encoding) |
| 800 | if packet.is_error_packet(): |
| 801 | if self._result is not None and self._result.unbuffered_active is True: |
| 802 | self._result.unbuffered_active = False |
| 803 | packet.raise_for_error() |
| 804 | return packet |
| 805 | |
| 806 | def _read_bytes(self, num_bytes): |
| 807 | self._sock.settimeout(self._read_timeout) |