aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/multiclient.h
blob: e12b73dcd98f19acf04ca83566d598089339b8a4 (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
#pragma once

#include "neh.h"

namespace NNeh {
    /// thread-safe dispacher for processing multiple neh requests
    /// (method Wait() MUST be called from single thread, methods Request and Interrupt are thread-safe)
    class IMultiClient {
    public:
        virtual ~IMultiClient() {
        }

        struct TRequest {
            TRequest()
                : Deadline(TInstant::Max())
                , UserData(nullptr)
            {
            }

            TRequest(const TMessage& msg, TInstant deadline = TInstant::Max(), void* userData = nullptr)
                : Msg(msg)
                , Deadline(deadline)
                , UserData(userData)
            {
            }

            TMessage Msg;
            TInstant Deadline;
            void* UserData;
        };

        /// WARNING:
        ///  Wait(event) called from another thread can return Event
        ///  for this request before this call return control
        virtual THandleRef Request(const TRequest& req) = 0;

        virtual size_t QueueSize() = 0;

        struct TEvent {
            enum TType {
                Timeout,
                Response,
                SizeEventType
            };

            TEvent()
                : Type(SizeEventType)
                , UserData(nullptr)
            {
            }

            TEvent(TType t, void* userData)
                : Type(t)
                , UserData(userData)
            {
            }

            TType Type;
            THandleRef Hndl;
            void* UserData;
        };

        /// return false if interrupted
        virtual bool Wait(TEvent&, TInstant = TInstant::Max()) = 0;
        /// interrupt guaranteed breaking execution Wait(), but few interrupts can be handled as one
        virtual void Interrupt() = 0;
    };

    typedef TAutoPtr<IMultiClient> TMultiClientPtr;

    TMultiClientPtr CreateMultiClient();
}