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

#include "neh.h"
#include <library/cpp/neh/utils.h>

#include <library/cpp/http/io/headers.h>

#include <util/generic/singleton.h>
#include <library/cpp/deprecated/atomic/atomic.h>

namespace NNeh {
    class TNotifyHandle: public THandle {
    public:
        inline TNotifyHandle(IOnRecv* r, const TMessage& msg, TStatCollector* s = nullptr) noexcept
            : THandle(r, s)
            , Msg_(msg)
            , StartTime_(TInstant::Now())
        {
        }

        void NotifyResponse(const TString& resp, const TString& firstLine = {}, const THttpHeaders& headers = Default<THttpHeaders>()) {
            Notify(new TResponse(Msg_, resp, ExecDuration(), firstLine, headers));
        }

        void NotifyError(const TString& errorText) {
            Notify(TResponse::FromError(Msg_, new TError(errorText), ExecDuration()));
        }

        void NotifyError(TErrorRef error) {
            Notify(TResponse::FromError(Msg_, error, ExecDuration()));
        }

        /** Calls when asnwer is received and reponse has headers and first line.
         */
        void NotifyError(TErrorRef error, const TString& data, const TString& firstLine, const THttpHeaders& headers) {
            Notify(TResponse::FromError(Msg_, error, data, ExecDuration(), firstLine, headers));
        }

        const TMessage& Message() const noexcept {
            return Msg_;
        }

    private:
        inline TDuration ExecDuration() const {
            TInstant now = TInstant::Now();
            if (now > StartTime_) {
                return now - StartTime_;
            }

            return TDuration::Zero();
        }

        const TMessage Msg_;
        const TInstant StartTime_;
    };

    typedef TIntrusivePtr<TNotifyHandle> TNotifyHandleRef;

    class TSimpleHandle: public TNotifyHandle {
    public:
        inline TSimpleHandle(IOnRecv* r, const TMessage& msg, TStatCollector* s = nullptr) noexcept
            : TNotifyHandle(r, msg, s)
            , SendComplete_(false)
            , Canceled_(false)
        {
        }

        bool MessageSendedCompletely() const noexcept override {
            return SendComplete_;
        }

        void Cancel() noexcept override {
            Canceled_ = true;
            THandle::Cancel();
        }

        inline void SetSendComplete() noexcept {
            SendComplete_ = true;
        }

        inline bool Canceled() const noexcept {
            return Canceled_;
        }

        inline const TAtomicBool* CanceledPtr() const noexcept {
            return &Canceled_;
        }

        void ResetOnRecv() noexcept {
            F_ = nullptr;
        }

    private:
        TAtomicBool SendComplete_;
        TAtomicBool Canceled_;
    };

    typedef TIntrusivePtr<TSimpleHandle> TSimpleHandleRef;
}