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
5#include "ccf/ds/logger.h"
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() {}
31
34 virtual bool on_read(size_t, uint8_t*&, sockaddr)
35 {
36 return true;
37 }
38
40 virtual void on_accept(ConnType&) {}
41
43 virtual void on_start(int64_t) {}
44
46 virtual void on_listening(
47 const std::string& host, const std::string& service)
48 {
49 LOG_INFO_FMT("{} {} listening on {}:{}", conn_name, name, host, service);
50 }
51 virtual void on_connect()
52 {
53 LOG_INFO_FMT("{} {} connected", conn_name, name);
54 }
55 virtual void on_disconnect()
56 {
57 LOG_TRACE_FMT("{} {} disconnected", conn_name, name);
58 }
59
61 virtual void on_bind_failed()
62 {
63 LOG_INFO_FMT("{} {} bind failed", conn_name, name);
64 }
65 virtual void on_connect_failed()
66 {
67 LOG_INFO_FMT("{} {} connect failed", conn_name, name);
68 }
69
71 virtual void on_resolve_failed()
72 {
73 LOG_FATAL_FMT("{} {} resolve failed", conn_name, name);
74 abort();
75 }
76 virtual void on_listen_failed()
77 {
78 LOG_FATAL_FMT("{} {} listen failed", conn_name, name);
79 abort();
80 }
81 };
82
83 std::pair<std::string, std::string> addr_to_str(
84 const sockaddr* addr, int address_family = AF_INET)
85 {
86 constexpr auto buf_len = INET6_ADDRSTRLEN;
87 char buf[buf_len] = {};
88 int rc;
89
90 if (address_family == AF_INET6)
91 {
92 const auto in6 = (const sockaddr_in6*)addr;
93 if ((rc = uv_ip6_name(in6, buf, buf_len)) != 0)
94 {
95 LOG_FAIL_FMT("uv_ip6_name failed: {}", uv_strerror(rc));
96 }
97
98 return {
99 fmt::format("[{}]", buf), fmt::format("{}", ntohs(in6->sin6_port))};
100 }
101
102 assert(address_family == AF_INET);
103 const auto in4 = (const sockaddr_in*)addr;
104 if ((rc = uv_ip4_name(in4, buf, buf_len)) != 0)
105 {
106 LOG_FAIL_FMT("uv_ip4_name failed: {}", uv_strerror(rc));
107 }
108
109 return {buf, fmt::format("{}", ntohs(in4->sin_port))};
110 }
111}
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:51
virtual void on_connect_failed()
Definition socket.h:65
virtual void on_bind_failed()
Failure loggers for when things go wrong, but not fatal.
Definition socket.h:61
virtual void on_disconnect()
Definition socket.h:55
virtual void on_start(int64_t)
To be implemented by all servers (after registration)
Definition socket.h:43
virtual ~SocketBehaviour()
Definition socket.h:30
const char * name
Definition socket.h:22
virtual void on_resolve_failed()
Failure loggers for when things go wrong, fataly.
Definition socket.h:71
virtual void on_accept(ConnType &)
To be implemented by servers with connections.
Definition socket.h:40
virtual void on_listen_failed()
Definition socket.h:76
virtual void on_listening(const std::string &host, const std::string &service)
Generic loggers for common reactions.
Definition socket.h:46
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 logger.h:362
#define LOG_TRACE_FMT
Definition logger.h:356
#define LOG_FATAL_FMT
Definition logger.h:364
#define LOG_FAIL_FMT
Definition logger.h:363
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:83
Definition configuration.h:14