uvco 0.1
Loading...
Searching...
No Matches
tcp.h
Go to the documentation of this file.
1// uvco (c) 2024 Lewin Bormann. See LICENSE for specific terms.
2
3#pragma once
4
5#include <uv.h>
6
7#include <boost/assert.hpp>
8#include <fmt/format.h>
9
13#include "uvco/run.h"
15#include "uvco/tcp_stream.h"
16
17#include <coroutine>
18#include <cstdint>
19#include <optional>
20#include <string>
21#include <sys/socket.h>
22
23namespace uvco {
24
27
29class TcpClient {
30public:
33 TcpClient(const Loop &loop, std::string target_host_address,
34 uint16_t target_host_port, int af_hint = AF_UNSPEC);
36 TcpClient(const Loop &loop, AddressHandle address);
37
38 TcpClient(TcpClient &&other) noexcept;
39 TcpClient(const TcpClient &) = delete;
40 TcpClient &operator=(TcpClient &&other) noexcept;
41 TcpClient &operator=(const TcpClient &) = delete;
42 ~TcpClient() = default;
43
47
48private:
49 const Loop *loop_;
50
51 // May be a name or address; resolved upon connect().
52 std::string host_;
54 uint16_t port_;
55
56 static void onConnect(uv_connect_t *req, uv_status status);
57
59 [[nodiscard]] bool await_ready() const;
60 bool await_suspend(std::coroutine_handle<> handle);
62
63 void onConnect(uv_status status);
64
65 std::optional<std::coroutine_handle<>> handle_;
66 std::optional<uv_status> status_;
67 };
68};
69
72class TcpServer : public StreamServerBase<uv_tcp_t, TcpStream> {
73public:
75 TcpServer(const Loop &loop, AddressHandle bindAddress, bool ipv6Only = false);
76
77 TcpServer(const TcpServer &) = delete;
78 TcpServer(TcpServer &&) = default;
79 TcpServer &operator=(const TcpServer &) = delete;
81 ~TcpServer() = default;
82
83private:
84 void bind(const struct sockaddr *addr, int flags);
85};
86
88
89} // namespace uvco
AddressHandle is a light-weight wrapper around a struct sockaddr_in(6), and is therefore cheap to cop...
Definition name_resolution.h:38
Definition loop.h:26
Definition promise.h:76
Definition stream_server_base.h:38
A client for connecting to a TCP peer.
Definition tcp.h:29
Promise< TcpStream > connect()
Definition tcp.cc:48
TcpClient & operator=(const TcpClient &)=delete
TcpClient & operator=(TcpClient &&other) noexcept
Definition tcp.cc:41
static void onConnect(uv_connect_t *req, uv_status status)
Definition tcp.cc:79
TcpClient(const TcpClient &)=delete
TcpClient(const Loop &loop, std::string target_host_address, uint16_t target_host_port, int af_hint=AF_UNSPEC)
Definition tcp.cc:28
uint16_t port_
Definition tcp.h:54
~TcpClient()=default
int af_hint_
Definition tcp.h:53
const Loop * loop_
Definition tcp.h:49
std::string host_
Definition tcp.h:52
Definition tcp.h:72
TcpServer(const Loop &loop, AddressHandle bindAddress, bool ipv6Only=false)
Sets up and bind socket to address.
Definition tcp.cc:107
void bind(const struct sockaddr *addr, int flags)
Definition tcp.cc:115
TcpServer(const TcpServer &)=delete
TcpServer & operator=(TcpServer &&)=default
TcpServer(TcpServer &&)=default
~TcpServer()=default
TcpServer & operator=(const TcpServer &)=delete
int uv_status
Result of a libuv operation, an errno error code.
Definition internal_utils.h:22
Definition async_work.cc:17
void onConnect(uv_status status)
Definition tcp.cc:99
bool await_suspend(std::coroutine_handle<> handle)
Definition tcp.cc:88
std::optional< std::coroutine_handle<> > handle_
Definition tcp.h:65
uv_status await_resume()
Definition tcp.cc:94
std::optional< uv_status > status_
Definition tcp.h:66
bool await_ready() const
Definition tcp.cc:84