aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/tourist/trace/src/protocol.cpp
blob: 5297048ec8916e669da068dc3dd94b9ad3ae4421 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
#include <foundation/types.h>
#include <foundation/buffer.h>
#include <trace/detail/protocol.h>
#include <trace/detail/transport.h>
#include <trace/detail/type.h>

#include "constants.h"

//------------------------------------------------------------------------------
class TypeDesc
{
public:
    uint16                          size : 13;
    uint16                          important : 1;
    uint16                          has_serial : 1;
    uint16                          maybe_aux : 1;
    uint16                          _unused[3];
    const Type*                     type;
    static Tuple<uint32, TypeDesc>  parse(BufferStream& stream);
};

//------------------------------------------------------------------------------
Tuple<uint32, TypeDesc> TypeDesc::parse(BufferStream& stream)
{
    uint32 zero_uid = stream.read<uint16>();
    if (zero_uid != 0)
        fatal("non-zero type uid");

    uint32 info_size = stream.read<uint16>();
    const uint8* type_info = stream.read(info_size);
    auto* type = (Type*)type_info;
    type->patch();

    uint32 uid = type->get_uid();

    uint32 type_size = 0;
    for (uint32 i = 0, n = type->get_field_count(); i < n; ++i)
        type_size += type->get_field(i).get_size();

    bool important_ = type->has_flag(TYPE_FLAG_IMPORTANT);
    bool has_serial_ = !type->has_flag(TYPE_FLAG_NO_SERIAL);
    bool maybe_aux_ = type->has_flag(TYPE_FLAG_AUX);

    TypeDesc desc = {
        uint16(type_size),
        important_,
        has_serial_,
        maybe_aux_,
        {},
        type
    };
    return { uid, desc };
}



//------------------------------------------------------------------------------
class Types
{
public:
    void                parse(Buffer& buffer, Vector<const Type*>& new_types);
    const TypeDesc*     lookup(uint32 uid) const;

private:
    Vector<BufferRef>   _buffer_refs;
    Vector<TypeDesc>    _descs;
};

//------------------------------------------------------------------------------
void Types::parse(Buffer& buffer, Vector<const Type*>& new_types)
{
    BufferStream stream = buffer.create_stream();
    do
    {
        auto [uid, desc] = TypeDesc::parse(stream);
        if (_descs.size() <= uid)
        {
            uint32 new_size = (uid + 32) & ~31;
            _descs.resize(new_size, TypeDesc{});
        }
        _descs[uid] = desc;

        new_types.push_back(desc.type);
    }
    while (stream.has_data());

    BufferRef buffer_ref = buffer.create_ref();
    _buffer_refs.push_back(std::move(buffer_ref));
}

//------------------------------------------------------------------------------
const TypeDesc* Types::lookup(uint32 uid) const
{
    if (uid >= _descs.size())
        return nullptr;

    const TypeDesc* ret = _descs.data() + uid;
    return (ret->type != nullptr) ? ret : nullptr;
}



//------------------------------------------------------------------------------
class Serial
{
public:
    enum NoSync  : uint32 { NO_SYNC = 0x0100'0000 };
    enum Pending : uint32 { PENDING = 0xff00'0000 };

                Serial()                        {}
    explicit    Serial(int32 v) : _v(v)         {}
    explicit    Serial(NoSync)  : _v(NO_SYNC)   {}
    explicit    Serial(Pending) : _v(PENDING)   {}
    bool        is_sync() const                 { return _v  < NO_SYNC; }
    int32       get_value() const               { return _v & 0x00ff'ffff; }
    explicit    operator bool () const          { return _v <= NO_SYNC; }
    bool        operator == (Pending) const     { return _v == PENDING; }
    bool        operator == (Serial rhs) const  { return !(_v - rhs._v); }
    bool        operator != (Serial rhs) const  { return ! operator == (rhs); }
    void        operator ++ ()                  { _v = (_v + 1) & 0x00ff'ffff; }

    bool less(Serial lhs, Serial rhs) const
    {
        return (!lhs || !rhs)
            ? lhs._v < rhs._v
            : (lhs._v - _v) < (rhs._v - _v);
    }

private:
    uint32 _v = 0xffff'ffff;
};

//------------------------------------------------------------------------------
class EventParser
    : public NoCopy
{
public:
                        EventParser() = default;
                        ~EventParser();
                        EventParser(EventParser&& rhs)  { move(std::move(rhs)); }
    EventParser&        operator = (EventParser&& rhs)  { move(std::move(rhs)); return *this; }
    Serial              parse_normal(BufferStream& stream, const Types& types);
    Serial              parse_important(BufferStream& stream, const Types& types);
    Event               consume();
    void                pin();
    bool                is_empty() const { return _stack.empty(); }

private:
    struct State
    {
        BufferStream&   stream;
        const Types&    types;
    };

    void                move(EventParser&& rhs);
    Serial              parse_normal(const State& state);
    Serial              parse_important(const State& state);
    Serial              parse_continue(const State& state);
    Serial              parse_uid(const State& state);
    Serial              parse_type(const State& state);
    Serial              parse_well_known(const State& state, uint32 uid);
    void                parse_aux(const State& state);
    Serial              parse_important_aux(const State& state);

    struct StackItem
        : public Event
    {
        Serial          serial;
    };

    friend class        ProtocolImpl;
    Vector<StackItem>   _stack;
    MutableBuffer       _fragment;
    uint32              _missing = 0;
    int32               _stage = 0;
    uint32              _last_uid = 0;
    uint8               _protocol_version = 7;
};

//------------------------------------------------------------------------------
EventParser::~EventParser()
{
    // Don't throw from destructors — this causes std::terminate during
    // stack unwinding (e.g. when the trace stream ends mid-parse).
}

//------------------------------------------------------------------------------
void EventParser::move(EventParser&& rhs)
{
    std::swap(_stack, rhs._stack);
    std::swap(_fragment, rhs._fragment);
    std::swap(_missing, rhs._missing);
    std::swap(_stage, rhs._stage);
    std::swap(_last_uid, rhs._last_uid);
    std::swap(_protocol_version, rhs._protocol_version);
}

//------------------------------------------------------------------------------
Serial EventParser::parse_normal(BufferStream& stream, const Types& types)
{
    State state = { stream, types };
    return parse_normal(state);
}

//------------------------------------------------------------------------------
Serial EventParser::parse_important(BufferStream& stream, const Types& types)
{
    if (_missing == 0)
    {
        State state = { stream, types };
        Serial ret = parse_important(state);
        if (!ret && _stage != 1 && _missing == 0)
            fatal("important parse should only fail on unknown uid");
        return ret;
    }

    uint32 remaining = stream.get_remaining();
    uint32 read_size = std::min(remaining, _missing);
    uint8* dest = _fragment.get_pointer() + _fragment.get_size() - _missing;
    std::memcpy(dest, stream.read(read_size), read_size);
    if (_missing -= read_size)
        return Serial();

    BufferStream missing_stream = _fragment.create_stream();
    State state = { missing_stream, types };

    if (!_stack.empty())
        return parse_important(state);

    parse_important(state);
    _missing = 0;
    return parse_important(stream, types);
}

//------------------------------------------------------------------------------
Event EventParser::consume()
{
    _stage = 0;
    Event event = std::move(_stack.back());
    _stack.pop_back();
    return event;
}

//------------------------------------------------------------------------------
void EventParser::pin()
{
    for (StackItem& item : _stack)
    {
        if (item.data.is_valid())
            item.data.pin();

        for (Aux& aux : item.aux)
            aux.data.pin();
    }
}

//------------------------------------------------------------------------------
Serial EventParser::parse_normal(const State& state)
{
    switch (_stage)
    {
    case 0:  return parse_uid(state);
    case 1:  return parse_type(state);
    default: fatal("unexpected _stage value");
    }
    return Serial();
}

//------------------------------------------------------------------------------
Serial EventParser::parse_important(const State& state)
{
    if (_stage == 1)
        return parse_type(state);

    BufferStream& stream = state.stream;

    auto ok_or_capture_fragment = [this, &stream] (uint32 required) {
        uint32 remaining = stream.get_remaining();
        if (remaining >= required)
            return true;

        if (required >= (64 << 10)) // size field is uint16 so 64 KB is the hard upper bound
            fatal("an important event seems to be rather too large");

        Allocator& allocator = Allocator::get_from(stream);
        _fragment = allocator.create_buffer(required);
        std::memcpy(
            _fragment.get_pointer(),
            stream.read(remaining),
            remaining
        );
        _missing = required - remaining;

        return false;
    };

    if (!ok_or_capture_fragment(EVENT_IMPORTANT_SIZE))
        return Serial(Serial::PENDING);

    _stage = 1;

    Event& top = _stack.emplace_back();
    top.uid = stream.read<uint16>();

    uint32 size = stream.read<uint16>();
    if (!ok_or_capture_fragment(size))
        return Serial(Serial::PENDING);

    // Track how many bytes parse_type consumes so we can skip any
    // remaining bytes in the declared important-event payload. The UE
    // writer may include trailing data (e.g. attachment metadata) that
    // our type parser does not consume.
    uint32 before = stream.get_remaining();
    Serial ret = parse_type(state);
    uint32 consumed = before - stream.get_remaining();
    if (consumed < size)
        stream.read(size - consumed);

    return ret;
}

//------------------------------------------------------------------------------
Serial EventParser::parse_continue(const State& state)
{
    _stage = 0;
    if (state.stream.has_data())
        return parse_uid(state);

    return _stack.empty() ? Serial() : Serial(Serial::PENDING);
}

//------------------------------------------------------------------------------
Serial EventParser::parse_uid(const State& state)
{
    BufferStream& stream = state.stream;

    uint32 uid = stream.read<uint8>();
    if (uid & EVENT_LARGE_UID_BIT)
        uid |= uint32(stream.read<uint8>()) << 8;
    _last_uid = uid >>= 1;

    if (uid <= EVENT_UID_WELL_KNOWN)
        return parse_well_known(state, uid);

    _stack.emplace_back().uid = uint16(uid);

    _stage = 1;
    return parse_type(state);
}

//------------------------------------------------------------------------------
Serial EventParser::parse_type(const State& state)
{
    StackItem& top = _stack.back();

    const TypeDesc* type_desc = state.types.lookup(top.uid);
    if (type_desc == nullptr)
        return Serial(Serial::PENDING);

    BufferStream& stream = state.stream;

    Serial serial(Serial::NO_SYNC);
    if (type_desc->has_serial)
    {
        uint32 low_serial = stream.read<uint8>();
        uint32 high_serial = stream.read<uint16>();
        serial = Serial((high_serial << 8) | low_serial);
    }

    uint32 event_size = type_desc->size;
    top.data = stream.read_ptr(event_size);
    top.serial = serial;

    if (type_desc->maybe_aux)
        return type_desc->important
            ? parse_important_aux(state)
            : parse_continue(state);

    _stage = -1;
    return serial;
}

//------------------------------------------------------------------------------
Serial EventParser::parse_well_known(const State& state, uint32 uid)
{
    BufferStream& stream = state.stream;

    // AuxData
    if (uid == 1)
    {
        parse_aux(state);
        return parse_continue(state);
    }

    // AuxDataTerminal
    if (uid == 3)
    {
        _stage = -1;
        return Serial(_stack.back().serial);
    }

    // EnterScope
    if (uid == 4)
        return parse_continue(state);

    // LeaveScope
    if (uid == 5)
        return parse_continue(state);

    if (_protocol_version >= 7)
    {
        // EnterScope_T (protocol 7)
        if (uid == 6 || uid == 8)
        {
            /*const uint8* timestamp =*/ stream.read(7);
            return parse_continue(state);
        }

        // LeaveScope_T (protocol 7)
        if (uid == 7 || uid == 9)
        {
            /*const uint8* timestamp =*/ stream.read(7);
            return parse_continue(state);
        }
    }
    else
    {
        // EnterScope_T (protocol 6)
        if (uid == 8 || uid == 12)
        {
            /*const uint8* timestamp =*/ stream.read(7);
            return parse_continue(state);
        }
    }

    fatal("Unexpected uid");
    return Serial();
}

//------------------------------------------------------------------------------
void EventParser::parse_aux(const State& state)
{
    uint32 low_size = state.stream.read<uint8>();
    uint32 high_size = state.stream.read<uint16>();
    uint32 size = (low_size | (high_size << 8)) >> 5;

    Event& top = _stack.back();
    Aux& aux = top.aux.emplace_back();
    aux.size = size;
    aux.index = low_size & 0x1f;
    aux.partial = 0;
    if (top.aux.size() > 1)
    {
        Aux& prev = *(top.aux.rbegin() + 1);
        prev.partial = (prev.index == aux.index);
    }

    BufferStream& stream = state.stream;
    uint32 remaining = stream.get_remaining();
    if (remaining < size)
        fatal("aux size too large");

    aux.data = stream.read_ptr(size);
}

//------------------------------------------------------------------------------
Serial EventParser::parse_important_aux(const State& state)
{
    uint32 uid = state.stream.read<uint8>();

    if (uid == 1) // AuxData
    {
        parse_aux(state);
        return parse_important_aux(state);
    }

    if (uid == 3) // AuxDataTerminal
    {
        _stage = -1;
        return Serial(Serial::NO_SYNC);
    }

    fatal("unsupported important sub-uid");
    return Serial();
}


//------------------------------------------------------------------------------
class PacketNodePool
{
public:
    struct PacketNode
    {
        PacketNode*         get_next() const        { return (PacketNode*)next; }
        void                set_next(PacketNode* n) { next = uintptr(n); }
        Buffer              payload;
        uintptr             compressed : 1;
        uintptr             _unused : 15;
        uintptr             next : 48;
    };

                            ~PacketNodePool();
    PacketNode*             alloc_pnode();
    void                    free_pnode(PacketNode* node);

private:
    PacketNode*             _free_nodes = nullptr;
};

//------------------------------------------------------------------------------
PacketNodePool::~PacketNodePool()
{
    while (_free_nodes != nullptr)
    {
        PacketNode* next = _free_nodes->get_next();
        delete _free_nodes;
        _free_nodes = next;
    }
}

//------------------------------------------------------------------------------
PacketNodePool::PacketNode* PacketNodePool::alloc_pnode()
{
    if (_free_nodes == nullptr)
        return new PacketNode();

    PacketNode* ret = _free_nodes;
    _free_nodes = ret->get_next();
    return new (ret) PacketNode();
}

//------------------------------------------------------------------------------
void PacketNodePool::free_pnode(PacketNode* node)
{
    node->payload = Buffer();
    node->set_next(_free_nodes);
    _free_nodes = node;
}



//------------------------------------------------------------------------------
class ParserPool
{
public:
    EventParser&        get_parser(uint32 index);
    uint16              alloc_parser();
    void                free_parser(uint32 index);

private:
    Vector<EventParser> _parsers;
    Vector<uint16>      _frees;
};

//------------------------------------------------------------------------------
EventParser& ParserPool::get_parser(uint32 index)
{
    return _parsers[index];
}

//------------------------------------------------------------------------------
uint16 ParserPool::alloc_parser()
{
    if (_frees.empty())
    {
        _parsers.emplace_back();
        return uint16(_parsers.size() - 1);
    }

    uint16 index = _frees.back();
    _frees.pop_back();
    return index;
}

//------------------------------------------------------------------------------
void ParserPool::free_parser(uint32 index)
{
    _parsers[index] = EventParser();
    _frees.push_back(uint16(index));
}



//------------------------------------------------------------------------------
class ProtocolImpl
    : public PacketNodePool
    , protected ParserPool
    , public NoCopy
    , public NoMove
{
public:
    explicit                ProtocolImpl(uint8 protocol_version);
    void                    enable_unordered();
    void                    read(EventParcel& parcel, Bundle& bundle);

private:
    struct Thread
    {
        Serial              serial;
        uint16              id;
        uint16              parser_index;
        PacketNode*         head = nullptr;
        PacketNode*         tail = nullptr;
    };

    template <bool> bool    read(EventParcel& parcel, Thread& thread);
    void                    scatter(EventParcel& parcel, Packet& packet);
    EventParser&            get_parser(const Thread& thread);
    Types                   _types;
    Vector<Thread>          _threads;
    Serial                  _next_serial = Serial(0);
    bool                    _serialised = true;
    uint8                   _protocol_version;
};

//------------------------------------------------------------------------------
ProtocolImpl::ProtocolImpl(uint8 protocol_version)
: _protocol_version(protocol_version)
{
    Thread& thread = _threads.emplace_back();
    thread.id = TID_IMPORTANT;
    thread.parser_index = alloc_parser();
    get_parser(thread)._protocol_version = protocol_version;
}

//------------------------------------------------------------------------------
void ProtocolImpl::enable_unordered()
{
    _serialised = false;
}

//------------------------------------------------------------------------------
void ProtocolImpl::read(EventParcel& parcel, Bundle& bundle)
{
    // scatter packets to their respective threads
    for (Packet& packet : bundle)
        scatter(parcel, packet);

    // important
    read<false>(parcel, _threads[0]);

    // read as many events as we can into the parcel
    for (uint32 i = 1, n = uint32(_threads.size()); i < n; ++i)
    {
        Thread& thread = _threads[i];
        if (thread.serial.is_sync())
            continue;

        if (!read<true>(parcel, thread))
            continue;

        thread = std::move(_threads.back());
        _threads.pop_back();
        --i, --n;
    }

    for (const bool do_gather = _serialised; do_gather;)
    {
        Serial prev_serial = _next_serial;
        for (uint32 i = 1, n = uint32(_threads.size()); i < n; ++i)
        {
            Thread& thread = _threads[i];
            if (thread.serial != _next_serial)
                continue;

            ++_next_serial;

            Event event = get_parser(thread).consume();
            event.thread_id = thread.id;
            event.serial = thread.serial.is_sync() ? thread.serial.get_value() : -1;
            parcel.events.push_back(std::move(event));

            if (!read<true>(parcel, thread))
                continue;

            thread = std::move(_threads.back());
            _threads.pop_back();
            --i, --n;
        }

        if (prev_serial == _next_serial)
            break;
    }

    for (Thread& thread : _threads)
        get_parser(thread).pin();
}

//------------------------------------------------------------------------------
void ProtocolImpl::scatter(EventParcel& parcel, Packet& packet)
{
    uint32 thread_id = packet.get_thread_id();

    if (thread_id == TID_SYNC)
        return;

    Buffer& payload = packet.get_payload();

    if (thread_id == TID_TYPE)
    {
        packet.decompress();
        _types.parse(payload, parcel.new_types);
        return;
    }

    PacketNode* node = alloc_pnode();
    node->payload = std::move(payload);
    node->compressed = packet.is_compressed();

    Thread* thread = (thread_id == TID_IMPORTANT) ? _threads.data() : nullptr;
    for (int32 i = 1, n = uint32(_threads.size()); i < n && !thread; ++i)
        if (Thread& lookup = _threads[i]; lookup.id == thread_id)
            thread = &lookup;

    if (thread == nullptr)
    {
        thread = &(_threads.emplace_back());
        thread->id = uint16(thread_id);
        thread->parser_index = alloc_parser();
        get_parser(*thread)._protocol_version = _protocol_version;
    }

    if (thread->tail != nullptr)
    {
        thread->tail->set_next(node);
        thread->tail = node;
    }
    else
        thread->head = thread->tail = node;
}

//------------------------------------------------------------------------------
template <bool is_normal>
bool ProtocolImpl::read(EventParcel& parcel, Thread& thread)
{
    EventParser& parser = get_parser(thread);

    PacketNode* node = thread.head;
    while (node != nullptr)
    {
        if (node->compressed)
        {
            Packet::decompress(node->payload);
            node->compressed = 0;
        }

        BufferRef buffer_ref = node->payload.create_ref();
        parcel.buffer_refs.push_back(std::move(buffer_ref));

        BufferStream stream = node->payload.create_stream();
        while (true)
        {
            Serial serial = is_normal
                ? parser.parse_normal(stream, _types)
                : parser.parse_important(stream, _types);

            thread.serial = serial;

            if (!serial)
                break;

            if (int32 cond = is_normal && serial.is_sync(); cond)
            {
                if ((_serialised == true) & (serial != _next_serial))
                    break;

                ++_next_serial;
                thread.serial = Serial(Serial::NO_SYNC);
            }

            Event event = parser.consume();
            event.thread_id = thread.id;
            event.serial = serial.is_sync() ? serial.get_value() : -1;
            parcel.events.push_back(std::move(event));

            if (!stream.has_data())
                break;
        }

        if (stream.has_data())
        {
            Buffer& buffer = node->payload;
            node->payload = buffer.create_sub_buffer(stream.get_consumed());
            thread.head = node;
            return false;
        }

        PacketNode* next = node->get_next();
        free_pnode(node);

        if ((thread.head = node = next) == nullptr)
            thread.tail = nullptr;

        if (bool cond = is_normal; cond)
            if (thread.serial.is_sync())
                return false;
    }

    if (bool cond = !is_normal; cond)
        return false;

    if (thread.serial == Serial::PENDING)
        return false;

    free_parser(thread.parser_index);
    thread.serial = Serial();
    return true;
}

//------------------------------------------------------------------------------
EventParser& ProtocolImpl::get_parser(const Thread& thread)
{
    return ParserPool::get_parser(thread.parser_index);
}



//------------------------------------------------------------------------------
void EventParcel::reset()
{
    events.clear();
    new_types.clear();
    buffer_refs.clear();
}



//------------------------------------------------------------------------------
Protocol::Protocol(uint8 version)
{
    _impl = new ProtocolImpl(version);
}

//------------------------------------------------------------------------------
Protocol::~Protocol()
{
    delete _impl;
}

//------------------------------------------------------------------------------
void Protocol::enable_unordered()
{
    return _impl->enable_unordered();
}

//------------------------------------------------------------------------------
void Protocol::read(EventParcel& parcel, Bundle& bundle)
{
    return _impl->read(parcel, bundle);
}