Crow  1.1
A C++ microframework for the web
 
Loading...
Searching...
No Matches
socket_acceptors.h
1#pragma once
2#ifdef CROW_USE_BOOST
3#include <boost/asio.hpp>
4#ifdef CROW_ENABLE_SSL
5#include <boost/asio/ssl.hpp>
6#endif
7#else
8#ifndef ASIO_STANDALONE
9#define ASIO_STANDALONE
10#endif
11#include <asio.hpp>
12#ifdef CROW_ENABLE_SSL
13#include <asio/ssl.hpp>
14#endif
15#endif
16
17#include "crow/logging.h"
18
19namespace crow
20{
21#ifdef CROW_USE_BOOST
22 namespace asio = boost::asio;
23 using error_code = boost::system::error_code;
24#else
25 using error_code = asio::error_code;
26#endif
27 using tcp = asio::ip::tcp;
28 using stream_protocol = asio::local::stream_protocol;
29
31 {
32 using endpoint = tcp::endpoint;
33 tcp::acceptor acceptor_;
34 TCPAcceptor(asio::io_context& io_context):
35 acceptor_(io_context) {}
36
37 int16_t port() const
38 {
39 return acceptor_.local_endpoint().port();
40 }
41 std::string address() const
42 {
43 return acceptor_.local_endpoint().address().to_string();
44 }
45 std::string url_display(bool ssl_used) const
46 {
47 auto address = acceptor_.local_endpoint().address();
48 return (ssl_used ? "https://" : "http://") + (address.is_v4() ? address.to_string() : "[" + address.to_string() + "]") + ":" + std::to_string(acceptor_.local_endpoint().port());
49 }
50 tcp::acceptor& raw_acceptor()
51 {
52 return acceptor_;
53 }
54 endpoint local_endpoint() const
55 {
56 return acceptor_.local_endpoint();
57 }
58 inline static tcp::acceptor::reuse_address reuse_address_option() { return tcp::acceptor::reuse_address(true); }
59 };
60
62 {
63 using endpoint = stream_protocol::endpoint;
64 stream_protocol::acceptor acceptor_;
65 UnixSocketAcceptor(asio::io_context& io_context):
66 acceptor_(io_context) {}
67
68 int16_t port() const
69 {
70 return 0;
71 }
72 std::string address() const
73 {
74 return acceptor_.local_endpoint().path();
75 }
76 std::string url_display(bool) const
77 {
78 return acceptor_.local_endpoint().path();
79 }
80 stream_protocol::acceptor& raw_acceptor()
81 {
82 return acceptor_;
83 }
84 endpoint local_endpoint() const
85 {
86 return acceptor_.local_endpoint();
87 }
88 inline static stream_protocol::acceptor::reuse_address reuse_address_option()
89 {
90 // reuse addr must be false (https://github.com/chriskohlhoff/asio/issues/622)
91 return stream_protocol::acceptor::reuse_address(false);
92 }
93 };
94} // namespace crow
The main namespace of the library. In this namespace is defined the most important classes and functi...
Definition socket_acceptors.h:31
Definition socket_acceptors.h:62