write.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#pragma once


#include <felspar/coro/task.hpp>
#include <felspar/io/warden.hpp>

#include <span>


namespace felspar::io {


    class warden;

Free standing version of write_some

17
18
19
20
21
22
23
24
25
26
    template<typename S>
    inline auto write_some(
            warden &w,
            S &&sock,
            std::span<std::byte const> const s,
            std::optional<std::chrono::nanoseconds> const timeout = {},
            felspar::source_location const &loc =
                    felspar::source_location::current()) {
        return w.write_some(sock, s, timeout, loc);
    }

Try to write data

This performs a synchronous write of as much data as the socket can take at the moment. Write errors results in thrown exceptions.

34
    std::size_t write_some(socket_descriptor, void const *, std::size_t);

Write all of the buffer to a file descriptor

 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
    template<typename S>
    inline warden::task<std::size_t> write_all(
            warden &ward,
            S &&sock,
            std::span<std::byte const> s,
            std::optional<std::chrono::nanoseconds> timeout = {},
            felspar::source_location const &loc =
                    felspar::source_location::current()) {
        auto out{s};
        while (out.size()) {
            auto const bytes =
                    co_await write_some(ward, sock, out, timeout, loc);
            if (not bytes) { co_return s.size() - out.size(); }
            out = out.subspan(bytes);
        }
        co_return s.size();
    }
    template<typename S>
    inline warden::task<std::size_t> write_all(
            warden &w,
            S &&fd,
            void const *buf,
            std::size_t count,
            std::optional<std::chrono::nanoseconds> timeout = {},
            felspar::source_location const &loc =
                    felspar::source_location::current()) {
        return write_all(
                w, std::forward<S>(fd),
                std::span<std::byte const>{
                        reinterpret_cast<std::byte const *>(buf), count},
                std::move(timeout), loc);
    }
    template<typename S>
    inline warden::task<std::size_t> write_all(
            warden &w,
            S &&fd,
            std::span<std::uint8_t const> s,
            std::optional<std::chrono::nanoseconds> timeout = {},
            felspar::source_location const &loc =
                    felspar::source_location::current()) {
        return write_all(
                w, std::forward<S>(fd),
                std::span<std::byte const>{
                        reinterpret_cast<std::byte const *>(s.data()), s.size()},
                std::move(timeout), loc);
    }
    template<typename S>
    inline warden::task<std::size_t> write_all(
            warden &w,
            S &&fd,
            std::string_view s,
            std::optional<std::chrono::nanoseconds> timeout = {},
            felspar::source_location const &loc =
                    felspar::source_location::current()) {
        return write_all(
                w, std::forward<S>(fd),
                std::span<std::byte const>{
                        reinterpret_cast<std::byte const *>(s.data()), s.size()},
                std::move(timeout), loc);
    }


}