aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/asio/asio.cpp
blob: 8b6cf383eac9a2ed575808483b4d94b434955d58 (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
#include "io_service_impl.h"
#include "deadline_timer_impl.h"
#include "tcp_socket_impl.h"
#include "tcp_acceptor_impl.h"

using namespace NDns;
using namespace NAsio;

namespace NAsio {
    TIOService::TWork::TWork(TWork& w)
        : Srv_(w.Srv_)
    {
        Srv_.GetImpl().WorkStarted();
    }

    TIOService::TWork::TWork(TIOService& srv)
        : Srv_(srv)
    {
        Srv_.GetImpl().WorkStarted();
    }

    TIOService::TWork::~TWork() {
        Srv_.GetImpl().WorkFinished();
    }

    TIOService::TIOService()
        : Impl_(new TImpl())
    {
    }

    TIOService::~TIOService() {
    }

    void TIOService::Run() {
        Impl_->Run();
    }

    void TIOService::Post(TCompletionHandler h) {
        Impl_->Post(std::move(h));
    }

    void TIOService::Abort() {
        Impl_->Abort();
    }

    TDeadlineTimer::TDeadlineTimer(TIOService& srv) noexcept
        : Srv_(srv)
        , Impl_(nullptr)
    {
    }

    TDeadlineTimer::~TDeadlineTimer() {
        if (Impl_) {
            Srv_.GetImpl().ScheduleOp(new TUnregisterTimerOperation(Impl_));
        }
    }

    void TDeadlineTimer::AsyncWaitExpireAt(TDeadline deadline, THandler h) {
        if (!Impl_) {
            Impl_ = new TDeadlineTimer::TImpl(Srv_.GetImpl());
            Srv_.GetImpl().ScheduleOp(new TRegisterTimerOperation(Impl_));
        }
        Impl_->AsyncWaitExpireAt(deadline, h);
    }

    void TDeadlineTimer::Cancel() {
        Impl_->Cancel();
    }

    TTcpSocket::TTcpSocket(TIOService& srv) noexcept
        : Srv_(srv)
        , Impl_(new TImpl(srv.GetImpl()))
    {
    }

    TTcpSocket::~TTcpSocket() {
    }

    void TTcpSocket::AsyncConnect(const TEndpoint& ep, TTcpSocket::TConnectHandler h, TDeadline deadline) {
        Impl_->AsyncConnect(ep, h, deadline);
    }

    void TTcpSocket::AsyncWrite(TSendedData& d, TTcpSocket::TWriteHandler h, TDeadline deadline) {
        Impl_->AsyncWrite(d, h, deadline);
    }

    void TTcpSocket::AsyncWrite(TContIOVector* vec, TWriteHandler h, TDeadline deadline) {
        Impl_->AsyncWrite(vec, h, deadline);
    }

    void TTcpSocket::AsyncWrite(const void* data, size_t size, TWriteHandler h, TDeadline deadline) {
        class TBuffers: public IBuffers {
        public:
            TBuffers(const void* theData, size_t theSize)
                : Part(theData, theSize)
                , IOVec(&Part, 1)
            {
            }

            TContIOVector* GetIOvec() override {
                return &IOVec;
            }

            IOutputStream::TPart Part;
            TContIOVector IOVec;
        };

        TSendedData d(new TBuffers(data, size));
        Impl_->AsyncWrite(d, h, deadline);
    }

    void TTcpSocket::AsyncRead(void* buff, size_t size, TTcpSocket::TReadHandler h, TDeadline deadline) {
        Impl_->AsyncRead(buff, size, h, deadline);
    }

    void TTcpSocket::AsyncReadSome(void* buff, size_t size, TTcpSocket::TReadHandler h, TDeadline deadline) {
        Impl_->AsyncReadSome(buff, size, h, deadline);
    }

    void TTcpSocket::AsyncPollRead(TTcpSocket::TPollHandler h, TDeadline deadline) {
        Impl_->AsyncPollRead(h, deadline);
    }

    void TTcpSocket::AsyncPollWrite(TTcpSocket::TPollHandler h, TDeadline deadline) {
        Impl_->AsyncPollWrite(h, deadline);
    }

    void TTcpSocket::AsyncCancel() {
        return Impl_->AsyncCancel();
    }

    size_t TTcpSocket::WriteSome(TContIOVector& d, TErrorCode& ec) noexcept {
        return Impl_->WriteSome(d, ec);
    }

    size_t TTcpSocket::WriteSome(const void* buff, size_t size, TErrorCode& ec) noexcept {
        return Impl_->WriteSome(buff, size, ec);
    }

    size_t TTcpSocket::ReadSome(void* buff, size_t size, TErrorCode& ec) noexcept {
        return Impl_->ReadSome(buff, size, ec);
    }

    bool TTcpSocket::IsOpen() const noexcept {
        return Native() != INVALID_SOCKET;
    }

    void TTcpSocket::Shutdown(TShutdownMode what, TErrorCode& ec) {
        return Impl_->Shutdown(what, ec);
    }

    SOCKET TTcpSocket::Native() const noexcept {
        return Impl_->Fd();
    }

    TEndpoint TTcpSocket::RemoteEndpoint() const {
        return Impl_->RemoteEndpoint();
    }

    //////////////////////////////////

    TTcpAcceptor::TTcpAcceptor(TIOService& srv) noexcept
        : Srv_(srv)
        , Impl_(new TImpl(srv.GetImpl()))
    {
    }

    TTcpAcceptor::~TTcpAcceptor() {
    }

    void TTcpAcceptor::Bind(TEndpoint& ep, TErrorCode& ec) noexcept {
        return Impl_->Bind(ep, ec);
    }

    void TTcpAcceptor::Listen(int backlog, TErrorCode& ec) noexcept {
        return Impl_->Listen(backlog, ec);
    }

    void TTcpAcceptor::AsyncAccept(TTcpSocket& s, TTcpAcceptor::TAcceptHandler h, TDeadline deadline) {
        return Impl_->AsyncAccept(s, h, deadline);
    }

    void TTcpAcceptor::AsyncCancel() {
        Impl_->AsyncCancel();
    }

}