aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Core/MySQL/PacketEndpoint.cpp
blob: 97b5d3b4d1188ad2b52003b3e4d53b1b8908ff17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <Core/MySQL/PacketEndpoint.h>
#include <IO/ReadBufferFromPocoSocket.h>
#include <Common/typeid_cast.h>

namespace DB
{

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}

namespace MySQLProtocol
{

PacketEndpoint::PacketEndpoint(WriteBuffer & out_, uint8_t & sequence_id_)
    : sequence_id(sequence_id_), in(nullptr), out(&out_)
{
}

PacketEndpoint::PacketEndpoint(ReadBuffer & in_, WriteBuffer & out_, uint8_t & sequence_id_)
    : sequence_id(sequence_id_), in(&in_), out(&out_)
{
}

MySQLPacketPayloadReadBuffer PacketEndpoint::getPayload()
{
    return MySQLPacketPayloadReadBuffer(*in, sequence_id);
}

void PacketEndpoint::receivePacket(IMySQLReadPacket & packet)
{
    packet.readPayload(*in, sequence_id);
}

bool PacketEndpoint::tryReceivePacket(IMySQLReadPacket & packet, UInt64 millisecond)
{
    if (millisecond != 0)
    {
        ReadBufferFromPocoSocket * socket_in = typeid_cast<ReadBufferFromPocoSocket *>(in);

        if (!socket_in)
            throw Exception(ErrorCodes::LOGICAL_ERROR, "LOGICAL ERROR: Attempt to pull the duration in a non socket stream");

        if (!socket_in->poll(millisecond * 1000))
            return false;
    }

    packet.readPayload(*in, sequence_id);
    return true;
}

void PacketEndpoint::resetSequenceId()
{
    sequence_id = 0;
}

String PacketEndpoint::packetToText(const String & payload)
{
    String result;
    for (auto c : payload)
    {
        result += ' ';
        result += std::to_string(static_cast<unsigned char>(c));
    }
    return result;
}

}

}