aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/connection.h
blob: b1df64ddc1e41a2d89af9f58f9e9fe9990481968 (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
#pragma once

#include "defs.h"
#include "message.h"

#include <util/generic/ptr.h>

namespace NBus {
    struct TBusClientConnection {
        /// if you want to open connection early
        virtual void OpenConnection() = 0;

        /// Send message to the destination
        /// If addr is set then use it as destination.
        /// Takes ownership of addr (see ClearState method).
        virtual EMessageStatus SendMessage(TBusMessage* pMes, bool wait = false) = 0;

        virtual EMessageStatus SendMessageOneWay(TBusMessage* pMes, bool wait = false) = 0;

        /// Like SendMessage but cares about message
        template <typename T /* <: TBusMessage */>
        EMessageStatus SendMessageAutoPtr(const TAutoPtr<T>& mes, bool wait = false) {
            EMessageStatus status = SendMessage(mes.Get(), wait);
            if (status == MESSAGE_OK)
                Y_UNUSED(mes.Release());
            return status;
        }

        /// Like SendMessageOneWay but cares about message
        template <typename T /* <: TBusMessage */>
        EMessageStatus SendMessageOneWayAutoPtr(const TAutoPtr<T>& mes, bool wait = false) {
            EMessageStatus status = SendMessageOneWay(mes.Get(), wait);
            if (status == MESSAGE_OK)
                Y_UNUSED(mes.Release());
            return status;
        }

        EMessageStatus SendMessageMove(TBusMessageAutoPtr message, bool wait = false) {
            return SendMessageAutoPtr(message, wait);
        }

        EMessageStatus SendMessageOneWayMove(TBusMessageAutoPtr message, bool wait = false) {
            return SendMessageOneWayAutoPtr(message, wait);
        }

        // TODO: implement similar one-way methods

        virtual ~TBusClientConnection() {
        }
    };

    namespace NPrivate {
        struct TBusClientConnectionPtrOps {
            static void Ref(TBusClientConnection*);
            static void UnRef(TBusClientConnection*);
        };
    }

    using TBusClientConnectionPtr = TIntrusivePtr<TBusClientConnection, NPrivate::TBusClientConnectionPtrOps>;

}