poll.iops.cpp

  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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "poll.hpp"

#include <felspar/exceptions.hpp>
#include <felspar/io/connect.hpp>


struct felspar::io::poll_warden::close_completion : public completion<void> {
    close_completion(
            poll_warden *s,
            socket_descriptor f,
            felspar::source_location const &loc)
    : completion<void>{s, {}, loc}, fd{f} {}
    socket_descriptor fd;
    void cancel_iop() override {}
    felspar::coro::coroutine_handle<> try_or_resume() override {
#ifdef FELSPAR_WINSOCK2
        ::closesocket(fd);
#else
        ::close(fd);
#endif
        return cancel_timeout_then_resume();
    }
};
felspar::io::iop<void> felspar::io::poll_warden::do_close(
        socket_descriptor fd, felspar::source_location const &loc) {
    return {new close_completion{this, fd, loc}};
}


struct felspar::io::poll_warden::sleep_completion : public completion<void> {
    sleep_completion(
            poll_warden *s,
            std::chrono::nanoseconds ns,
            felspar::source_location const &loc)
    : completion<void>{s, ns, loc} {}
    void cancel_iop() override {}
    felspar::coro::coroutine_handle<> iop_timedout() override {
        return io::completion<void>::handle;
    }
    felspar::coro::coroutine_handle<> try_or_resume() override {
        return felspar::coro::noop_coroutine();
    }
};
felspar::io::iop<void> felspar::io::poll_warden::do_sleep(
        std::chrono::nanoseconds ns, felspar::source_location const &loc) {
    return {new sleep_completion{this, ns, loc}};
}


struct felspar::io::poll_warden::read_some_completion :
public completion<std::size_t> {
    read_some_completion(
            poll_warden *s,
            socket_descriptor f,
            std::span<std::byte> b,
            std::optional<std::chrono::nanoseconds> timeout,
            felspar::source_location const &loc)
    : completion<std::size_t>{s, timeout, loc}, fd{f}, buf{b} {}
    socket_descriptor fd;
    std::span<std::byte> buf;
    void cancel_iop() override { std::erase(self->requests[fd].reads, this); }
    felspar::coro::coroutine_handle<> try_or_resume() override {
#ifdef FELSPAR_WINSOCK2
        if (auto const bytes = recv(
                    fd, reinterpret_cast<char *>(buf.data()), buf.size(), {});
            bytes != SOCKET_ERROR) {
#else
        if (auto const bytes = ::read(fd, buf.data(), buf.size()); bytes >= 0) {
#endif
            result = bytes;
            return cancel_timeout_then_resume();
        } else if (auto const error = get_error(); would_block(error)) {
            self->requests[fd].reads.push_back(this);
            return felspar::coro::noop_coroutine();
        } else {
            result = {{error, std::system_category()}, "read"};
            return cancel_timeout_then_resume();
        }
    }
};
felspar::io::iop<std::size_t> felspar::io::poll_warden::do_read_some(
        socket_descriptor fd,
        std::span<std::byte> buf,
        std::optional<std::chrono::nanoseconds> timeout,
        felspar::source_location const &loc) {
    return {new read_some_completion{this, fd, buf, timeout, loc}};
}


struct felspar::io::poll_warden::write_some_completion :
public completion<std::size_t> {
    write_some_completion(
            poll_warden *s,
            socket_descriptor f,
            std::span<std::byte const> b,
            std::optional<std::chrono::nanoseconds> t,
            felspar::source_location const &loc)
    : completion<std::size_t>{s, t, loc}, fd{f}, buf{b} {}
    socket_descriptor fd;
    std::span<std::byte const> buf;
    void cancel_iop() override { std::erase(self->requests[fd].writes, this); }
    felspar::coro::coroutine_handle<> try_or_resume() override {
#ifdef FELSPAR_WINSOCK2
        if (auto const bytes =
                    ::send(fd, reinterpret_cast<char const *>(buf.data()),
                           buf.size(), {});
            bytes != SOCKET_ERROR) {
#else
        if (auto const bytes = ::write(fd, buf.data(), buf.size());
            bytes >= 0) {
#endif
            result = bytes;
            return cancel_timeout_then_resume();
        } else if (auto const error = get_error(); would_block(error)) {
            self->requests[fd].writes.push_back(this);
            return felspar::coro::noop_coroutine();
        } else {
            result = {{error, std::system_category()}, "write"};
            return cancel_timeout_then_resume();
        }
    }
};
felspar::io::iop<std::size_t> felspar::io::poll_warden::do_write_some(
        socket_descriptor fd,
        std::span<std::byte const> buf,
        std::optional<std::chrono::nanoseconds> t,
        felspar::source_location const &loc) {
    return {new write_some_completion{this, fd, buf, t, loc}};
}


struct felspar::io::poll_warden::accept_completion :
public completion<socket_descriptor> {
    accept_completion(
            poll_warden *s,
            socket_descriptor f,
            std::optional<std::chrono::nanoseconds> t,
            felspar::source_location const &loc)
    : completion<socket_descriptor>{s, t, loc}, fd{f} {}
    socket_descriptor fd;
    void cancel_iop() override { std::erase(self->requests[fd].reads, this); }
    felspar::coro::coroutine_handle<> try_or_resume() override {
#ifdef FELSPAR_WINSOCK2
        if (auto const r = ::accept(fd, nullptr, nullptr);
            r != INVALID_SOCKET) {
#elif defined(FELSPAR_HAS_ACCEPT4)
        if (auto const r = ::accept4(fd, nullptr, nullptr, SOCK_NONBLOCK);
            r >= 0) {
#else
        if (auto const r = ::accept(fd, nullptr, nullptr); r >= 0) {
            posix::set_non_blocking(r, loc);
#endif
            result = r;
            return cancel_timeout_then_resume();
        } else if (auto const error = get_error(); would_block(error)) {
            self->requests[fd].reads.push_back(this);
            return felspar::coro::noop_coroutine();
        } else if (bad_fd(error)) {
            result = r;
            return cancel_timeout_then_resume();
        } else {
            result = {{error, std::system_category()}, "accept"};
            return cancel_timeout_then_resume();
        }
    }
};
felspar::io::iop<felspar::io::socket_descriptor>
        felspar::io::poll_warden::do_accept(
                socket_descriptor fd,
                std::optional<std::chrono::nanoseconds> timeout,
                felspar::source_location const &loc) {
    return {new accept_completion{this, fd, timeout, loc}};
}


struct felspar::io::poll_warden::connect_completion : public completion<void> {
    connect_completion(
            poll_warden *s,
            socket_descriptor f,
            sockaddr const *a,
            socklen_t l,
            std::optional<std::chrono::nanoseconds> t,
            felspar::source_location const &loc)
    : completion<void>{s, t, loc}, fd{f}, addr{a}, addrlen{l} {}
    socket_descriptor fd;
    sockaddr const *addr;
    socklen_t addrlen;
    void cancel_iop() override { std::erase(self->requests[fd].writes, this); }
    felspar::coro::coroutine_handle<>
            await_suspend(felspar::coro::coroutine_handle<> h) override {
        handle = h;
#ifdef FELSPAR_WINSOCK2
        if (auto err = ::connect(fd, addr, addrlen); err != SOCKET_ERROR) {
            return handle;
        } else if (auto const wsae = WSAGetLastError(); would_block(wsae)) {
            self->requests[fd].writes.push_back(this);
            insert_timeout();
            return felspar::coro::noop_coroutine();
        } else {
            result = {{wsae, std::system_category()}, "connect error"};
            return handle;
        }
#else
        if (auto err = ::connect(fd, addr, addrlen); err == 0) {
            return handle;
        } else if (errno == EINPROGRESS) {
            self->requests[fd].writes.push_back(this);
            insert_timeout();
            return felspar::coro::noop_coroutine();
        } else {
            result = {{errno, std::system_category()}, "connect"};
            return handle;
        }
#endif
    }
    felspar::coro::coroutine_handle<> try_or_resume() override {
        int errvalue{};
#if defined(FELSPAR_WINSOCK2)
        char *perr = reinterpret_cast<char *>(&errvalue);
#else
        int *perr = &errvalue;
#endif
        ::socklen_t length{sizeof(errvalue)};
        if (getsockopt(fd, SOL_SOCKET, SO_ERROR, perr, &length) == 0) {
            if (errvalue == 0) {
                return cancel_timeout_then_resume();
            } else {
                result = {{get_error(), std::system_category()}, "connect"};
                return cancel_timeout_then_resume();
            }
        } else {
            result = {
                    {get_error(), std::system_category()},
                    "connect/getsockopt"};
            return cancel_timeout_then_resume();
        }
    }
};
felspar::io::iop<void> felspar::io::poll_warden::do_connect(
        socket_descriptor fd,
        sockaddr const *addr,
        socklen_t addrlen,
        std::optional<std::chrono::nanoseconds> timeout,
        felspar::source_location const &loc) {
    return {new connect_completion{this, fd, addr, addrlen, timeout, loc}};
}


struct felspar::io::poll_warden::read_ready_completion :
public completion<void> {
    read_ready_completion(
            poll_warden *s,
            socket_descriptor f,
            std::optional<std::chrono::nanoseconds> t,
            felspar::source_location const &loc)
    : completion<void>{s, t, loc}, fd{f} {}
    socket_descriptor fd;
    void cancel_iop() override { std::erase(self->requests[fd].reads, this); }
    felspar::coro::coroutine_handle<>
            await_suspend(felspar::coro::coroutine_handle<> h) override {
        handle = h;
        self->requests[fd].reads.push_back(this);
        insert_timeout();
        return felspar::coro::noop_coroutine();
    }
    felspar::coro::coroutine_handle<> try_or_resume() override {
        return cancel_timeout_then_resume();
    }
};
felspar::io::iop<void> felspar::io::poll_warden::do_read_ready(
        socket_descriptor fd,
        std::optional<std::chrono::nanoseconds> timeout,
        felspar::source_location const &loc) {
    return {new read_ready_completion{this, fd, timeout, loc}};
}


struct felspar::io::poll_warden::write_ready_completion :
public completion<void> {
    write_ready_completion(
            poll_warden *s,
            socket_descriptor f,
            std::optional<std::chrono::nanoseconds> t,
            felspar::source_location const &loc)
    : completion<void>{s, t, loc}, fd{f} {}
    socket_descriptor fd;
    void cancel_iop() override { std::erase(self->requests[fd].writes, this); }
    felspar::coro::coroutine_handle<> await_suspend(
            felspar::coro::coroutine_handle<> h) noexcept override {
        handle = h;
        self->requests[fd].writes.push_back(this);
        insert_timeout();
        return felspar::coro::noop_coroutine();
    }
    felspar::coro::coroutine_handle<> try_or_resume() override {
        return cancel_timeout_then_resume();
    }
};
felspar::io::iop<void> felspar::io::poll_warden::do_write_ready(
        socket_descriptor fd,
        std::optional<std::chrono::nanoseconds> timeout,
        felspar::source_location const &loc) {
    return {new write_ready_completion{this, fd, timeout, loc}};
}