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 return (ssl_used ? "https://" : "http://") + acceptor_.local_endpoint().address().to_string() + ":" + std::to_string(acceptor_.local_endpoint().port());
48 }
49 tcp::acceptor& raw_acceptor()
50 {
51 return acceptor_;
52 }
53 endpoint local_endpoint() const
54 {
55 return acceptor_.local_endpoint();
56 }
57 inline static tcp::acceptor::reuse_address reuse_address_option() { return tcp::acceptor::reuse_address(true); }
58 };
59
61 {
62 using endpoint = stream_protocol::endpoint;
63 stream_protocol::acceptor acceptor_;
64 UnixSocketAcceptor(asio::io_context& io_context):
65 acceptor_(io_context) {}
66
67 int16_t port() const
68 {
69 return 0;
70 }
71 std::string address() const
72 {
73 return acceptor_.local_endpoint().path();
74 }
75 std::string url_display(bool) const
76 {
77 return acceptor_.local_endpoint().path();
78 }
79 stream_protocol::acceptor& raw_acceptor()
80 {
81 return acceptor_;
82 }
83 endpoint local_endpoint() const
84 {
85 return acceptor_.local_endpoint();
86 }
87 inline static stream_protocol::acceptor::reuse_address reuse_address_option()
88 {
89 // reuse addr must be false (https://github.com/chriskohlhoff/asio/issues/622)
90 return stream_protocol::acceptor::reuse_address(false);
91 }
92 };
93} // 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:61