aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/tourist/trace/src/preamble.cpp
blob: 7b54dbc8e2e097b78a4750a4b5e265d2693e029c (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
#include <foundation/types.h>

#include <trace/detail/exceptions.h>
#include <trace/detail/preamble.h>
#include <trace/detail/protocol.h>
#include <trace/detail/transport.h>

//------------------------------------------------------------------------------
Preamble::Preamble(DataSource& source, Allocator& allocator)
: _stream(source, allocator)
{
}

//------------------------------------------------------------------------------
Transport Preamble::get_transport()
{
    if (!_parsed)
        parse_header();

    return Transport(std::move(_stream));
}

//------------------------------------------------------------------------------
Protocol Preamble::get_protocol()
{
    if (!_parsed)
        parse_header();

    return Protocol(_protocol_version);
}

//------------------------------------------------------------------------------
void Preamble::parse_header()
{
    // magic
    uint32 magic = _stream.read<uint32>();
    if (magic != 'TRC2')
        throw Exception::StreamError("Unexpected magic value", 0, magic);

    // meta
    uint32 meta_size = _stream.read<uint16>();
    _stream.read(meta_size);

    // transport
    _transport_version = _stream.read<uint8>();
    if (_transport_version != 4)
        throw Exception::StreamError("Unexpected transport version", 0, _transport_version);

    // protocol
    _protocol_version = _stream.read<uint8>();
    if (_protocol_version != 6 && _protocol_version != 7)
        throw Exception::StreamError("Unexpected protocol version", 0, _protocol_version);

    _parsed = true;
}