cppnet
C++ network utilities for asynchronous servers.
Loading...
Searching...
No Matches
async_udp_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_UDP_SERVICE_HPP
20#define CPPNET_ASYNC_UDP_SERVICE_HPP
21#include "async_context.hpp"
22namespace net::service {
64// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
65template <typename UDPStreamHandler, std::size_t Size = 64 * 1024UL>
67public:
69 template <typename T> using socket_address = io::socket::socket_address<T>;
77 using socket_handle = io::socket::socket_handle;
79 using socket_dialog = io::socket::socket_dialog<multiplexer_type>;
82
84 struct read_context {
86 using buffer_type = std::array<std::byte, Size>;
92 using socket_address = io::socket::socket_address<sockaddr_in6>;
98 using socket_message = io::socket::socket_message<sockaddr_in6>;
99
103 std::span<std::byte> buffer{read_buffer};
105 socket_message msg{.address = socket_address{}, .buffers = buffer};
106 };
107
112 auto signal_handler(int signum) noexcept -> void;
118 auto start(async_context &ctx) noexcept -> std::error_code;
126 std::shared_ptr<read_context> rctx) -> void;
127
128protected:
130 async_udp_service() = default;
136 template <typename T>
138
139private:
141 using socket_type = io::socket::native_socket_type;
142
152 auto emit(async_context &ctx, const socket_dialog &socket,
153 std::shared_ptr<read_context> rctx = {},
154 std::span<const std::byte> buf = {}) -> void;
155
165 [[nodiscard]] auto
166 initialize_(const socket_handle &socket) -> std::error_code;
167
169 auto stop_() -> void;
177 std::atomic<socket_type> server_sockfd_ = io::socket::INVALID_SOCKET;
178};
179
180} // namespace net::service
181
182#include "impl/async_udp_service_impl.hpp" // IWYU pragma: export
183#endif // CPPNET_ASYNC_UDP_SERVICE_HPP
This file declares an asynchronous execution context.
A ServiceLike Async UDP Service.
Definition async_udp_service.hpp:66
async_udp_service()=default
Default constructor.
io::socket::socket_address< T > socket_address
Templated socket address type.
Definition async_udp_service.hpp:69
async_context::async_scope async_scope
The async scope type.
Definition async_udp_service.hpp:73
io::socket::socket_dialog< multiplexer_type > socket_dialog
The socket dialog type.
Definition async_udp_service.hpp:79
auto submit_recv(async_context &ctx, const socket_dialog &socket, std::shared_ptr< read_context > rctx) -> void
Submits an asynchronous socket recv.
io::socket::socket_handle socket_handle
The socket handle type.
Definition async_udp_service.hpp:77
async_context::multiplexer_type multiplexer_type
The io multiplexer type.
Definition async_udp_service.hpp:75
auto start(async_context &ctx) noexcept -> std::error_code
Start the service on the context.
async_udp_service(socket_address< T > address) noexcept
Socket address constructor.
auto signal_handler(int signum) noexcept -> void
handle signals.
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_udp_service.hpp:84
std::array< std::byte, Size > buffer_type
The read buffer type.
Definition async_udp_service.hpp:86
buffer_type read_buffer
The read buffer.
Definition async_udp_service.hpp:101
io::socket::socket_message< sockaddr_in6 > socket_message
The socket message type.
Definition async_udp_service.hpp:98
io::socket::socket_address< sockaddr_in6 > socket_address
Socket address type.
Definition async_udp_service.hpp:92
socket_message msg
The read socket message.
Definition async_udp_service.hpp:105
std::span< std::byte > buffer
An assignable read buffer span.
Definition async_udp_service.hpp:103