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 <cstdint>
18#include <string>
19
20namespace uvco {
21
24
26class TcpClient {
27 struct ConnectAwaiter_;
28
29public:
32 TcpClient(const Loop &loop, std::string target_host_address,
33 uint16_t target_host_port, int af_hint = AF_UNSPEC);
35 TcpClient(const Loop &loop, AddressHandle address);
36
37 TcpClient(TcpClient &&other) noexcept;
38 TcpClient(const TcpClient &) = delete;
39 TcpClient &operator=(TcpClient &&other) noexcept;
40 TcpClient &operator=(const TcpClient &) = delete;
41 ~TcpClient() = default;
42
46
47private:
48 const Loop *loop_;
49
50 std::optional<AddressHandle> resolvedAddress_;
51 // May be a name or address; resolved upon connect().
52 std::string host_;
53 int af_hint_{};
54 uint16_t port_{};
55
56 static void onConnect(uv_connect_t *req, uv_status status);
57};
58
61class TcpServer : public StreamServerBase<uv_tcp_t, TcpStream> {
62public:
64 TcpServer(const Loop &loop, AddressHandle bindAddress, bool ipv6Only = false);
65
66 TcpServer(const TcpServer &) = delete;
67 TcpServer(TcpServer &&) = default;
68 TcpServer &operator=(const TcpServer &) = delete;
70 ~TcpServer() = default;
71
72private:
73 void bind(const struct sockaddr *addr, int flags);
74};
75
77
78} // namespace uvco
AddressHandle is a light-weight wrapper around a struct / sockaddr_in(6), and is therefore cheap to c...
Definition name_resolution.h:32
Definition loop.h:26
Definition promise.h:49
StreamServerBase(const StreamServerBase &)=delete
Promise< TcpStream > connect()
Definition tcp.cc:120
std::optional< AddressHandle > resolvedAddress_
Definition tcp.h:50
TcpClient & operator=(const TcpClient &)=delete
TcpClient & operator=(TcpClient &&other) noexcept
Definition tcp.cc:113
static void onConnect(uv_connect_t *req, uv_status status)
Definition tcp.cc:159
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:101
uint16_t port_
Definition tcp.h:54
~TcpClient()=default
int af_hint_
Definition tcp.h:53
const Loop * loop_
Definition tcp.h:48
std::string host_
Definition tcp.h:52
TcpServer(const Loop &loop, AddressHandle bindAddress, bool ipv6Only=false)
Sets up and bind socket to address.
Definition tcp.cc:172
void bind(const struct sockaddr *addr, int flags)
Definition tcp.cc:180
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:18