#pragma once #include "evo/network/tls_channel.hpp" #include #include #include #include #include // Minimal non-blocking TCP for the M9.7 dev session (ADR-0083), Windows and Linux. // Plaintext bytes only: every session byte travels inside TlsChannel. No socket // handle crosses this header. One owner thread per object. namespace evo::network { class TcpStream final : public ByteTransport { public: // Resolves `host` (blocking, bounded by the system resolver) and starts a // non-blocking connect. Reads/writes return 0 until the connection completes. [[nodiscard]] static std::expected, TransportError> connect(const std::string& host, std::uint16_t port); TcpStream(const TcpStream&) = delete; TcpStream& operator=(const TcpStream&) = delete; ~TcpStream() override; [[nodiscard]] std::expected write(std::span bytes) override; [[nodiscard]] std::expected read(std::span bytes) override; private: friend class TcpListener; TcpStream(std::intptr_t socket, bool connecting) : socket_{socket}, connecting_{connecting} {} [[nodiscard]] std::expected ready(); std::intptr_t socket_; bool connecting_; }; class TcpListener { public: // Numeric address only ("127.0.0.1:7400", "[::1]:7400", "0.0.0.0:7400"); port 0 // picks a free port. [[nodiscard]] static std::expected, TransportError> listen(std::string_view address); TcpListener(const TcpListener&) = delete; TcpListener& operator=(const TcpListener&) = delete; ~TcpListener(); // A pending connection, or nullptr when none is waiting. [[nodiscard]] std::expected, TransportError> accept(); [[nodiscard]] std::uint16_t port() const noexcept { return port_; } private: TcpListener(std::intptr_t socket, std::uint16_t port) : socket_{socket}, port_{port} {} std::intptr_t socket_; std::uint16_t port_; }; } // namespace evo::network