CCF
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6
7#include <uv.h>
8
9namespace asynchost
10{
18 template <class ConnType>
20 {
21 protected:
22 const char* name;
23 const char* conn_name;
24
25 public:
26 SocketBehaviour(const char* name, const char* conn_name) :
27 name(name),
29 {}
30 virtual ~SocketBehaviour() = default;
31
34 virtual bool on_read(
35 size_t /*unused*/, uint8_t*& /*unused*/, sockaddr /*unused*/)
36 {
37 return true;
38 }
39
41 virtual void on_accept(ConnType& /*unused*/) {}
42
44 virtual void on_start(int64_t /*unused*/) {}
45
47 virtual void on_listening(
48 const std::string& host, const std::string& service)
49 {
50 LOG_INFO_FMT("{} {} listening on {}:{}", conn_name, name, host, service);
51 }
52 virtual void on_connect()
53 {
54 LOG_INFO_FMT("{} {} connected", conn_name, name);
55 }
56 virtual void on_disconnect()
57 {
58 LOG_TRACE_FMT("{} {} disconnected", conn_name, name);
59 }
60
62 virtual void on_bind_failed()
63 {
64 LOG_INFO_FMT("{} {} bind failed", conn_name, name);
65 }
66 virtual void on_connect_failed()
67 {
68 LOG_INFO_FMT("{} {} connect failed", conn_name, name);
69 }
70
72 virtual void on_resolve_failed()
73 {
74 LOG_FATAL_FMT("{} {} resolve failed", conn_name, name);
75 abort();
76 }
77 virtual void on_listen_failed()
78 {
79 LOG_FATAL_FMT("{} {} listen failed", conn_name, name);
80 abort();
81 }
82 };
83
84 inline std::pair<std::string, std::string> addr_to_str(
85 const sockaddr* addr, int address_family = AF_INET)
86 {
87 constexpr auto buf_len = INET6_ADDRSTRLEN;
88 char buf[buf_len] = {};
89 int rc = 0;
90
91 if (address_family == AF_INET6)
92 {
93 const auto* const in6 = reinterpret_cast<const sockaddr_in6*>(addr);
94 if ((rc = uv_ip6_name(in6, buf, buf_len)) != 0)
95 {
96 LOG_FAIL_FMT("uv_ip6_name failed: {}", uv_strerror(rc));
97 }
98
99 return {
100 fmt::format("[{}]", buf), fmt::format("{}", ntohs(in6->sin6_port))};
101 }
102
103 assert(address_family == AF_INET);
104 const auto* const in4 = reinterpret_cast<const sockaddr_in*>(addr);
105 if ((rc = uv_ip4_name(in4, buf, buf_len)) != 0)
106 {
107 LOG_FAIL_FMT("uv_ip4_name failed: {}", uv_strerror(rc));
108 }
109
110 return {buf, fmt::format("{}", ntohs(in4->sin_port))};
111 }
112}
Callback service for user-specific behaviour for TCP and UDP connections.
Definition socket.h:20
virtual bool on_read(size_t, uint8_t *&, sockaddr)
Definition socket.h:34
virtual void on_connect()
Definition socket.h:52
virtual ~SocketBehaviour()=default
virtual void on_connect_failed()
Definition socket.h:66
virtual void on_bind_failed()
Failure loggers for when things go wrong, but not fatal.
Definition socket.h:62
virtual void on_disconnect()
Definition socket.h:56
virtual void on_start(int64_t)
To be implemented by all servers (after registration)
Definition socket.h:44
const char * name
Definition socket.h:22
virtual void on_resolve_failed()
Failure loggers for when things go wrong, fataly.
Definition socket.h:72
virtual void on_accept(ConnType &)
To be implemented by servers with connections.
Definition socket.h:41
virtual void on_listen_failed()
Definition socket.h:77
virtual void on_listening(const std::string &host, const std::string &service)
Generic loggers for common reactions.
Definition socket.h:47
SocketBehaviour(const char *name, const char *conn_name)
Definition socket.h:26
const char * conn_name
Definition socket.h:23
#define LOG_INFO_FMT
Definition internal_logger.h:15
#define LOG_TRACE_FMT
Definition internal_logger.h:13
#define LOG_FATAL_FMT
Definition internal_logger.h:17
#define LOG_FAIL_FMT
Definition internal_logger.h:16
Definition after_io.h:8
std::pair< std::string, std::string > addr_to_str(const sockaddr *addr, int address_family=AF_INET)
Definition socket.h:84
Definition configuration.h:14