Skip to content

socket/socket_transport.hpp

Namespaces

Name
rlpx
rlpx::socket

Classes

Name
class rlpx::socket::SocketTransport

Functions

Name
Result< SocketTransport > connect_with_timeout(boost::asio::any_io_executor executor, std::string_view host, uint16_t port, std::chrono::milliseconds timeout, boost::asio::yield_context yield)
Connect to remote endpoint with timeout.

Functions Documentation

function connect_with_timeout

Result< SocketTransport > connect_with_timeout(
    boost::asio::any_io_executor executor,
    std::string_view host,
    uint16_t port,
    std::chrono::milliseconds timeout,
    boost::asio::yield_context yield
)

Connect to remote endpoint with timeout.

Parameters:

  • executor Asio executor to use for the connection.
  • host Remote hostname or IP.
  • port Remote TCP port.
  • timeout Connection timeout duration.
  • yield Boost.Asio stackful coroutine context.

Return: Connected SocketTransport on success, SessionError on failure.

Source code

// Copyright 2025 GeniusVentures
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "../rlpx_types.hpp"
#include "../rlpx_error.hpp"
#include <boost/asio/spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/strand.hpp>
#include <memory>

namespace rlpx::socket {

class SocketTransport {
public:
    using tcp = boost::asio::ip::tcp;
    using Strand = boost::asio::strand<boost::asio::any_io_executor>;

    explicit SocketTransport(tcp::socket socket) noexcept;

    // Non-copyable, moveable
    SocketTransport(const SocketTransport&) = delete;
    SocketTransport& operator=(const SocketTransport&) = delete;
    SocketTransport(SocketTransport&&) noexcept = default;
    SocketTransport& operator=(SocketTransport&&) noexcept = default;

    [[nodiscard]] Result<ByteBuffer>
    read_exact(size_t num_bytes, boost::asio::yield_context yield) noexcept;

    [[nodiscard]] VoidResult
    write_all(ByteView data, boost::asio::yield_context yield) noexcept;

    [[nodiscard]] VoidResult close() noexcept;

    [[nodiscard]] bool is_open() const noexcept;

    // Get remote endpoint info
    [[nodiscard]] std::string remote_address() const noexcept;
    [[nodiscard]] uint16_t remote_port() const noexcept;

    // Get local endpoint info
    [[nodiscard]] std::string local_address() const noexcept;
    [[nodiscard]] uint16_t local_port() const noexcept;

private:
    tcp::socket socket_;
    Strand strand_; 
};

[[nodiscard]] Result<SocketTransport>
connect_with_timeout(
    boost::asio::any_io_executor executor,
    std::string_view host,
    uint16_t port,
    std::chrono::milliseconds timeout,
    boost::asio::yield_context yield
) noexcept;

} // namespace rlpx::socket

Updated on 2026-04-13 at 23:22:46 -0700