cppnet
C++ network utilities for asynchronous servers.
Loading...
Searching...
No Matches
async_tcp_service.hpp
Go to the documentation of this file.
1// Copyright 2025 Kevin Exton
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
18#pragma once
19#ifndef CPPNET_ASYNC_TCP_SERVICE_HPP
20#define CPPNET_ASYNC_TCP_SERVICE_HPP
21#include "async_context.hpp"
22namespace net::service {
71// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
72template <typename TCPStreamHandler, std::size_t Size = 64 * 1024UL>
74public:
76 template <typename T> using socket_address = io::socket::socket_address<T>;
84 using socket_handle = io::socket::socket_handle;
86 using socket_dialog = io::socket::socket_dialog<multiplexer_type>;
89
91 struct read_context {
93 using buffer_type = std::array<std::byte, Size>;
95 using socket_message = io::socket::socket_message<>;
96
100 std::span<std::byte> buffer{read_buffer};
103 };
104
109 auto signal_handler(int signum) noexcept -> void;
115 auto start(async_context &ctx) noexcept -> std::error_code;
123 std::shared_ptr<read_context> rctx) -> void;
124
125protected:
127 async_tcp_service() = default;
133 template <typename T>
135
136private:
138 using socket_type = io::socket::native_socket_type;
139
145 auto acceptor(async_context &ctx, const socket_dialog &socket) -> void;
155 auto emit(async_context &ctx, const socket_dialog &socket,
156 std::shared_ptr<read_context> rctx = {},
157 std::span<const std::byte> buf = {}) -> void;
158
168 [[nodiscard]] auto
169 initialize_(const socket_handle &socket) -> std::error_code;
170
172 auto stop_() -> void;
180 std::atomic<socket_type> acceptor_sockfd_ = io::socket::INVALID_SOCKET;
181};
182
183} // namespace net::service
184
185#include "impl/async_tcp_service_impl.hpp" // IWYU pragma: export
186#endif // CPPNET_ASYNC_TCP_SERVICE_HPP
This file declares an asynchronous execution context.
A ServiceLike Async TCP Service.
Definition async_tcp_service.hpp:73
async_context::async_scope async_scope
The async scope type.
Definition async_tcp_service.hpp:80
async_tcp_service(socket_address< T > address) noexcept
Socket address constructor.
io::socket::socket_dialog< multiplexer_type > socket_dialog
The socket dialog type.
Definition async_tcp_service.hpp:86
auto start(async_context &ctx) noexcept -> std::error_code
Start the service on the context.
io::socket::socket_address< T > socket_address
Templated socket address type.
Definition async_tcp_service.hpp:76
io::socket::socket_handle socket_handle
The socket handle type.
Definition async_tcp_service.hpp:84
auto signal_handler(int signum) noexcept -> void
handle signals.
auto submit_recv(async_context &ctx, const socket_dialog &socket, std::shared_ptr< read_context > rctx) -> void
Submits an asynchronous socket recv.
async_tcp_service()=default
Default constructor.
async_context::multiplexer_type multiplexer_type
The io multiplexer type.
Definition async_tcp_service.hpp:82
This namespace is for network services.
Definition async_context.hpp:31
An asynchronous execution context.
Definition async_context.hpp:34
io::execution::poll_multiplexer multiplexer_type
The io multiplexer type.
Definition async_context.hpp:38
exec::async_scope async_scope
Asynchronous scope type.
Definition async_context.hpp:36
signals
An enum of all valid async context signals.
Definition async_context.hpp:57
A read context.
Definition async_tcp_service.hpp:91
socket_message msg
The read socket message.
Definition async_tcp_service.hpp:102
std::span< std::byte > buffer
An assignable read buffer span.
Definition async_tcp_service.hpp:100
io::socket::socket_message<> socket_message
The socket message type.
Definition async_tcp_service.hpp:95
std::array< std::byte, Size > buffer_type
The read buffer type.
Definition async_tcp_service.hpp:93
buffer_type read_buffer
The read buffer.
Definition async_tcp_service.hpp:98