ClickHouse/src/IO/MySQLPacketPayloadReadBuffer.cpp

59 lines
1.7 KiB
C++
Raw Normal View History

2020-08-13 12:41:36 +00:00
#include <IO/MySQLPacketPayloadReadBuffer.h>
2020-08-13 08:17:33 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int UNKNOWN_PACKET_FROM_CLIENT;
}
2020-08-13 12:41:36 +00:00
const size_t MAX_PACKET_LENGTH = (1 << 24) - 1; // 16 mb
2020-08-13 08:17:33 +00:00
2020-08-13 12:41:36 +00:00
MySQLPacketPayloadReadBuffer::MySQLPacketPayloadReadBuffer(ReadBuffer & in_, uint8_t & sequence_id_)
2020-08-13 08:17:33 +00:00
: ReadBuffer(in_.position(), 0), in(in_), sequence_id(sequence_id_) // not in.buffer().begin(), because working buffer may include previous packet
{
}
2020-08-13 12:41:36 +00:00
bool MySQLPacketPayloadReadBuffer::nextImpl()
2020-08-13 08:17:33 +00:00
{
2020-08-13 12:41:36 +00:00
if (!has_read_header || (payload_length == MAX_PACKET_LENGTH && offset == payload_length))
2020-08-13 08:17:33 +00:00
{
has_read_header = true;
working_buffer.resize(0);
offset = 0;
payload_length = 0;
in.readStrict(reinterpret_cast<char *>(&payload_length), 3);
2020-08-13 12:41:36 +00:00
if (payload_length > MAX_PACKET_LENGTH)
2020-11-07 00:14:53 +00:00
throw Exception(ErrorCodes::UNKNOWN_PACKET_FROM_CLIENT,
"Received packet with payload larger than max_packet_size: {}", payload_length);
2020-08-13 08:17:33 +00:00
size_t packet_sequence_id = 0;
in.read(reinterpret_cast<char &>(packet_sequence_id));
if (packet_sequence_id != sequence_id)
2020-11-07 00:14:53 +00:00
throw Exception(ErrorCodes::UNKNOWN_PACKET_FROM_CLIENT,
"Received packet with wrong sequence-id: {}. Expected: {}.", packet_sequence_id, static_cast<unsigned int>(sequence_id));
2020-08-13 08:17:33 +00:00
sequence_id++;
if (payload_length == 0)
return false;
}
else if (offset == payload_length)
{
return false;
}
in.nextIfAtEnd();
working_buffer = ReadBuffer::Buffer(in.position(), in.buffer().end());
size_t count = std::min(in.available(), payload_length - offset);
working_buffer.resize(count);
in.ignore(count);
offset += count;
return true;
}
}