|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Apache License Version 2.0 which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + ********************************************************************************/ |
| 13 | +#ifndef SCORE_LIB_NETWORK_SOCK_ASYNC_I_SOCKET_H_ |
| 14 | +#define SCORE_LIB_NETWORK_SOCK_ASYNC_I_SOCKET_H_ |
| 15 | + |
| 16 | +#include "score/network/sock_async/net_endpoint.h" |
| 17 | +#include "score/os/socket.h" |
| 18 | +#include <score/callback.hpp> |
| 19 | +#include <score/span.hpp> |
| 20 | +#include <score/variant.hpp> |
| 21 | +#include <sys/time.h> /* KW_SUPPRESS:MISRA.INCL.UNSAFE:time.h libarary needed for TimeoutOption */ |
| 22 | +#include <cstdint> |
| 23 | + |
| 24 | +namespace score |
| 25 | +{ |
| 26 | + |
| 27 | +namespace os |
| 28 | +{ |
| 29 | +/* KW_SUPPRESS_START:AUTOSAR.STYLE.SINGLE_STMT_PER_LINE: False Positive */ |
| 30 | +/* KW_SUPPRESS_START:MISRA.ONEDEFRULE.VAR: False Positive */ |
| 31 | +/* KW_SUPPRESS_START:MISRA.VAR.NEEDS.CONST: False Positive */ |
| 32 | +constexpr const std::int32_t ENABLE_OPTION = 1; |
| 33 | +/* KW_SUPPRESS_END:MISRA.VAR.NEEDS.CONST: False Positive */ |
| 34 | +/* KW_SUPPRESS_END:MISRA.ONEDEFRULE.VAR: False Positive */ |
| 35 | +/* KW_SUPPRESS_END:AUTOSAR.STYLE.SINGLE_STMT_PER_LINE: False Positive */ |
| 36 | +class SockOption |
| 37 | +{ |
| 38 | + public: |
| 39 | + virtual ~SockOption() = default; |
| 40 | + virtual score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const = 0; |
| 41 | +}; |
| 42 | + |
| 43 | +class ReuseAddrOption : public SockOption |
| 44 | +{ |
| 45 | + public: |
| 46 | + /* KW_SUPPRESS_START:MISRA.CTOR.BASE: Pure virtual base, no ctor */ |
| 47 | + ReuseAddrOption() : optval_(ENABLE_OPTION) {} |
| 48 | + /* KW_SUPPRESS_END:MISRA.CTOR.BASE */ |
| 49 | + |
| 50 | + score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const override |
| 51 | + { |
| 52 | + return score::os::Socket::instance().setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval_, sizeof(optval_)); |
| 53 | + } |
| 54 | + |
| 55 | + private: |
| 56 | + std::int32_t optval_; |
| 57 | +}; |
| 58 | + |
| 59 | +class TimeoutOption : public SockOption |
| 60 | +{ |
| 61 | + public: |
| 62 | + TimeoutOption(const std::int32_t seconds) : SockOption(), seconds_(seconds) {} |
| 63 | + |
| 64 | + score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const override |
| 65 | + { |
| 66 | + struct timeval timeout; |
| 67 | + timeout.tv_sec = seconds_; |
| 68 | + timeout.tv_usec = 0; |
| 69 | + return score::os::Socket::instance().setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); |
| 70 | + } |
| 71 | + |
| 72 | + private: |
| 73 | + std::int32_t seconds_; |
| 74 | +}; |
| 75 | + |
| 76 | +class BufferSizeOption : public SockOption |
| 77 | +{ |
| 78 | + public: |
| 79 | + BufferSizeOption(const std::int32_t size) : SockOption(), size_(size) {} |
| 80 | + |
| 81 | + score::cpp::expected_blank<score::os::Error> apply(const std::int32_t sockfd) const override |
| 82 | + { |
| 83 | + return score::os::Socket::instance().setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &size_, sizeof(size_)); |
| 84 | + } |
| 85 | + |
| 86 | + private: |
| 87 | + std::int32_t size_; |
| 88 | +}; |
| 89 | + |
| 90 | +using AsyncCallback = score::cpp::callback<void(std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>>, ssize_t)>; |
| 91 | +using AsyncConnectCallback = score::cpp::callback<void(std::int16_t)>; |
| 92 | +using Endpoint = score::os::NetEndpoint; |
| 93 | +using SockOptionVariant = score::cpp::variant<ReuseAddrOption, TimeoutOption, BufferSizeOption>; |
| 94 | +class ISocket |
| 95 | +{ |
| 96 | + public: |
| 97 | + virtual void SetOption(const SockOptionVariant optionVariant) noexcept = 0; |
| 98 | + virtual std::int32_t WriteAsync(std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer, |
| 99 | + AsyncCallback cb) noexcept = 0; |
| 100 | + virtual score::cpp::expected<ssize_t, score::os::Error> WriteSync( |
| 101 | + std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer) const noexcept = 0; |
| 102 | + virtual std::int32_t ReadAsync(std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer, |
| 103 | + AsyncCallback cb) noexcept = 0; |
| 104 | + virtual score::cpp::variant<ssize_t, std::tuple<ssize_t, score::os::Ipv4Address>, score::os::Error> ReadSync( |
| 105 | + std::shared_ptr<std::vector<score::cpp::span<std::uint8_t>>> buffer) noexcept = 0; |
| 106 | + |
| 107 | + virtual std::int32_t ConnectAsync(AsyncConnectCallback cb) noexcept = 0; |
| 108 | + // virtual void Unblock() noexcept = 0; |
| 109 | + virtual ~ISocket() noexcept = default; |
| 110 | +}; |
| 111 | + |
| 112 | +} // namespace os |
| 113 | +} // namespace score |
| 114 | + |
| 115 | +#endif // SCORE_LIB_NETWORK_SOCK_ASYNC_I_SOCKET_H_ |
0 commit comments