Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
StreamAcceptor.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_NET_STREAMACCEPTOR_HPP
18#define TOOLBOX_NET_STREAMACCEPTOR_HPP
19
22
23namespace toolbox {
24inline namespace net {
25
26template <typename DerivedT>
28 public:
31
33 : serv_{ep.protocol()}
34 {
35 serv_.set_reuse_addr(true);
36 serv_.bind(ep);
37 serv_.listen(SOMAXCONN);
38 sub_ = r.subscribe(*serv_, EpollIn, bind<&StreamAcceptor::on_io_event>(this));
39 }
40
41 // Copy.
44
45 // Move.
48
49 protected:
50 ~StreamAcceptor() = default;
51
52 private:
53 void on_io_event(CyclTime now, int fd, unsigned /*events*/)
54 {
56 IoSock sock{os::accept(fd, ep), serv_.family()};
57 static_cast<DerivedT*>(this)->on_sock_prepare(now, sock);
58 sock.set_non_block();
59 if (sock.is_ip_family()) {
60 set_tcp_no_delay(sock.get(), true);
61 if (!is_tcp_no_delay(sock.get())) {
62 throw std::runtime_error{"TCP_NODELAY option not set"};
63 }
64 }
65 static_cast<DerivedT*>(this)->on_sock_accept(now, std::move(sock), ep);
66 }
67
68 StreamSockServ serv_;
69 Reactor::Handle sub_;
70};
71
72} // namespace net
73} // namespace toolbox
74
75#endif // TOOLBOX_NET_STREAMACCEPTOR_HPP
StreamAcceptor & operator=(StreamAcceptor &&)=delete
StreamAcceptor & operator=(const StreamAcceptor &)=delete
StreamAcceptor(Reactor &r, const Endpoint &ep)
StreamAcceptor(StreamAcceptor &&)=delete
StreamAcceptor(const StreamAcceptor &)=delete
@ EpollIn
The associated file is available for read(2) operations.
Definition Epoll.hpp:115
void set_tcp_no_delay(int sockfd, bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:736
BasicEndpoint< StreamProtocol > StreamEndpoint
Definition Endpoint.hpp:35
bool is_tcp_no_delay(int sockfd, std::error_code &ec) noexcept
Definition Socket.hpp:678
FileHandle accept(int sockfd, sockaddr &addr, socklen_t &addrlen, std::error_code &ec) noexcept
Accept a connection on a socket.
Definition Socket.hpp:194
constexpr auto bind() noexcept
Definition Slot.hpp:92
int family() const noexcept
Definition Socket.hpp:788
void set_reuse_addr(bool enabled, std::error_code &ec) noexcept
Definition Socket.hpp:826
Passive Server Stream Socket. All state is in base class, so object can be sliced.
void listen(int backlog, std::error_code &ec) noexcept
void bind(const Endpoint &ep, std::error_code &ec) noexcept