aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/event.cpp
blob: b963dfb4ea00568d93853072b4726b030ba96c61 (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
#include "event.h"
#include "event_pb.h"

namespace NActors {

    const TScopeId TScopeId::LocallyGenerated{
        Max<ui64>(), Max<ui64>()
    };

    TAutoPtr<IEventHandle>& IEventHandle::Forward(TAutoPtr<IEventHandle>& ev, TActorId recipient) {
        if (ev->IsEventLight()) {
            IEventHandleLight::GetLight(ev.Get())->Forward(recipient);
        } else {
            ev = IEventHandleFat::GetFat(ev.Get())->Forward(recipient);
        }
        return ev;
    }

    THolder<IEventHandle>& IEventHandle::Forward(THolder<IEventHandle>& ev, TActorId recipient) {
        if (ev->IsEventLight()) {
            IEventHandleLight::GetLight(ev.Get())->Forward(recipient);
        } else {
            ev = IEventHandleFat::GetFat(ev.Get())->Forward(recipient);
        }
        return ev;
    }

    TString IEventHandle::GetTypeName() const {
        if (IsEventFat()) {
            auto* ev = const_cast<IEventHandleFat*>(static_cast<const IEventHandleFat*>(this));
            return ev->HasEvent() ? TypeName(*(ev->GetBase())) : TypeName(*this);
        } else {
            return TypeName(*this);
        }
    }

    TString IEventHandle::ToString() const {
        if (IsEventFat()) {
            auto* ev = const_cast<IEventHandleFat*>(static_cast<const IEventHandleFat*>(this));
            return ev->HasEvent() ? ev->GetBase()->ToString().data() : "serialized?";
        } else {
            // TODO(xenoxeno):
            return TypeName(*this);
        }
    }

    bool IEventHandle::HasEvent() const {
        if (IsEventLight()) {
            return true;
        } else {
            return IEventHandleFat::GetFat(this)->HasEvent();
        }
    }

    bool IEventHandle::HasBuffer() const {
        if (IsEventLight()) {
            return false;
        } else {
            return IEventHandleFat::GetFat(this)->HasBuffer();
        }
    }

    TActorId IEventHandle::GetForwardOnNondeliveryRecipient() const {
        if (IsEventLight()) {
            return {};
        } else {
            return IEventHandleFat::GetFat(this)->GetForwardOnNondeliveryRecipient();
        }
    }

    size_t IEventHandle::GetSize() const {
        if (IsEventLight()) {
            if (IsEventSerializable()) {
                return IEventHandleLightSerializable::GetLightSerializable(this)->GetSize();
            }
        } else {
            return IEventHandleFat::GetFat(this)->GetSize();
        }
        return 0;
    }

    TIntrusivePtr<TEventSerializedData> IEventHandleFat::ReleaseChainBuffer() {
        if (Buffer) {
            TIntrusivePtr<TEventSerializedData> result;
            DoSwap(result, Buffer);
            Event.Reset();
            return result;
        }
        if (Event) {
            TAllocChunkSerializer serializer;
            Event->SerializeToArcadiaStream(&serializer);
            auto chainBuf = serializer.Release(Event->CreateSerializationInfo());
            Event.Reset();
            return chainBuf;
        }
        return new TEventSerializedData;
    }

    TIntrusivePtr<TEventSerializedData> IEventHandleFat::GetChainBuffer() {
        if (Buffer) {
            return Buffer;
        }
        if (Event) {
            TAllocChunkSerializer serializer;
            Event->SerializeToArcadiaStream(&serializer);
            Buffer = serializer.Release(Event->CreateSerializationInfo());
            return Buffer;
        }
        return new TEventSerializedData;
    }

    std::vector<std::vector<IEventFactory*>*> TEventFactories::EventFactories;
}