libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
posix_connector.hpp
Go to the documentation of this file.
1/**
2 * @file posix_connector.hpp
3 * @brief POSIX socket-based E3 Connector
4 *
5 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef LIBE3_POSIX_CONNECTOR_HPP
10#define LIBE3_POSIX_CONNECTOR_HPP
11
13#include <atomic>
14#include <condition_variable>
15#include <cstddef>
16#include <deque>
17#include <memory>
18#include <mutex>
19#include <vector>
20
21namespace libe3 {
22
23/**
24 * @brief POSIX socket-based E3 connector: one endpoint per dApp
25 *
26 * Supports TCP, SCTP and IPC. A dApp opens one connection to the RAN's single
27 * endpoint and the setup, inbound and outbound traffic all use it; the three
28 * channels of E3Connector are logical. The first PDU on a connection is the
29 * dApp's setup request and goes to recv_setup_request(); every later PDU goes
30 * to receive(). The inbound thread is the only reader of the sockets, so the
31 * receive path costs the same as with one channel per socket.
32 *
33 * Each PDU is one message. SCTP and IPC (a sequenced-packet socket) keep message
34 * boundaries and carry no prefix. TCP is a byte stream and prefixes each PDU
35 * with its length as four bytes in network order, in both directions. SCTP
36 * stamps payload protocol id 0x00E3A001 and sends indications on stream 1,
37 * everything else on stream 0.
38 *
39 * The RAN names each accepted connection with a token that is never reused
40 * (see E3Connector::last_peer_token) and can send to one peer or to all.
41 */
43public:
44 /**
45 * @brief Create a POSIX connector
46 *
47 * @param transport_layer SCTP, TCP or IPC
48 * @param endpoint Endpoint of the link. IPC: a path, "ipc://" prefix optional,
49 * and the library's shared default is replaced by /tmp/dapps/e3.
50 * TCP and SCTP: "tcp://host:port" or "sctp://host:port" (a host in
51 * brackets for IPv6); anything else means the default host and @p port
52 * @param port Port of the link (SCTP and TCP) when the endpoint names none
53 * @param options Socket and admission options
54 */
57 const std::string& endpoint,
58 uint16_t port,
59 const PosixOptions& options
60 );
61
62 /** @brief Close every socket. */
63 ~PosixE3Connector() override;
64
66 int recv_setup_request(std::vector<uint8_t>& buffer) override;
67 ErrorCode send_response(const std::vector<uint8_t>& data) override;
69 int receive(std::vector<uint8_t>& buffer) override;
71 ErrorCode send(const std::vector<uint8_t>& data) override;
72 ErrorCode send_to(uint64_t peer_token, const std::vector<uint8_t>& data, PduClass kind) override;
73
74 // Client-side (dApp role) operations.
76 ErrorCode send_setup_request_client(const std::vector<uint8_t>& data) override;
77 int recv_setup_response_client(std::vector<uint8_t>& buffer) override;
80
81 void dispose() override;
82 void shutdown() override;
83 uint64_t last_peer_token() const override;
84 uint64_t last_setup_peer_token() const override;
85 std::vector<uint64_t> take_closed_peers() override;
86
87 E3LinkLayer link_layer() const noexcept override {
88 return E3LinkLayer::POSIX;
89 }
90
91 E3TransportLayer transport_layer() const noexcept override {
92 return transport_layer_;
93 }
94
95 bool is_connected() const noexcept override {
96 return connected_;
97 }
98
99private:
100 struct Peer;
101 struct Queued;
102
103 /// Which end of a connection a socket is, as the options it needs differ.
104 enum class SocketKind {
105 LISTENER, ///< A RAN's listening socket
106 CLIENT, ///< A dApp's socket, before it connects
107 ACCEPTED ///< A RAN's socket for one accepted connection
108 };
109
110 /// Where the endpoint string says to listen or connect.
111 struct Target {
112 std::string host; ///< Empty: the default (any address to listen, loopback to connect)
113 std::string port; ///< Decimal port
114 };
115
116 Target target() const;
117 int make_socket(int family) const;
118 void tune_socket(int fd, SocketKind kind) const;
119 int listen_on(const Target& where);
120 int connect_to(const Target& where);
121 bool admit(const void* address) const;
122 void accept_pending();
123 std::shared_ptr<Peer> find_peer(uint64_t token);
124 bool pump(std::vector<uint8_t>& out, uint64_t& token, int timeout_ms);
125 void drop_peer(const std::shared_ptr<Peer>& peer);
126 void push_setup(const std::shared_ptr<Peer>& peer, std::vector<uint8_t>&& pdu);
127 bool receiver_idle() const;
128 int receive_client(std::vector<uint8_t>& buffer);
129 ErrorCode reconnect_client();
130
131 E3TransportLayer transport_layer_;
132 std::string endpoint_uri_; ///< The endpoint as given, to read a host and port from
133 uint16_t port_;
134 PosixOptions options_;
135
136 int listener_{-1};
137 mutable std::mutex peers_mutex_; ///< Guards peers_
138 std::vector<std::shared_ptr<Peer>> peers_; ///< RAN role: accepted connections
139 uint64_t next_token_{1}; ///< Only the reader takes tokens
140 size_t round_robin_{0}; ///< Reader's rotating start, so no peer starves the rest
141
142 std::mutex reader_mutex_; ///< Held by whoever reads the sockets
143 std::atomic<int64_t> last_receive_ns_{0}; ///< When receive() was last called
144
145 std::mutex queue_mutex_; ///< Guards setup_queue_ and inbound_queue_
146 std::condition_variable setup_ready_; ///< Signals setup_queue_ and shutdown
147 std::deque<Queued> setup_queue_; ///< First PDUs of connections
148 std::deque<Queued> inbound_queue_; ///< PDUs read while the setup thread was the reader
149 std::shared_ptr<Peer> setup_peer_; ///< Sender of the last setup request, for send_response
150 uint64_t last_setup_token_{0};
151 uint64_t last_peer_token_{0};
152 std::mutex closed_mutex_;
153 std::vector<uint64_t> closed_peers_;
154
155 int client_fd_{-1}; ///< dApp role: the one connection
156 std::vector<uint8_t> client_rx_; ///< Stream bytes or message fragments not yet a PDU
157 std::mutex client_write_mutex_;
158 bool setup_request_sent_{false};
159 std::atomic<bool> awaiting_setup_response_{false};
160 std::mutex await_mutex_;
161 std::condition_variable await_cv_;
162
163 bool connected_{false};
164 std::atomic<bool> shutdown_requested_{false};
165};
166
167} // namespace libe3
168
169#endif // LIBE3_POSIX_CONNECTOR_HPP
E3Connector(const E3Connector &)=delete
E3LinkLayer link_layer() const noexcept override
Get the link layer type.
ErrorCode send_setup_request_client(const std::vector< uint8_t > &data) override
Send a SetupRequest on the setup channel (dApp role).
~PosixE3Connector() override
Close every socket.
uint64_t last_peer_token() const override
Which peer supplied the bytes the last receive() returned (RAN role).
int recv_setup_request(std::vector< uint8_t > &buffer) override
Receive a setup request.
uint64_t last_setup_peer_token() const override
Which peer sent the setup request the last recv_setup_request() returned (RAN role).
void dispose() override
Clean up and release all resources.
int receive(std::vector< uint8_t > &buffer) override
Receive data from the inbound channel.
ErrorCode send(const std::vector< uint8_t > &data) override
Send data on the outbound channel.
ErrorCode setup_initial_connection_client() override
Connect the setup channel (dApp role: REQ connect).
E3TransportLayer transport_layer() const noexcept override
Get the transport layer type.
ErrorCode send_to(uint64_t peer_token, const std::vector< uint8_t > &data, PduClass kind) override
Send data to one peer, or to all of them (RAN role).
int recv_setup_response_client(std::vector< uint8_t > &buffer) override
Receive a SetupResponse on the setup channel (dApp role).
ErrorCode send_response(const std::vector< uint8_t > &data) override
Send a setup response.
PosixE3Connector(E3TransportLayer transport_layer, const std::string &endpoint, uint16_t port, const PosixOptions &options)
Create a POSIX connector.
void shutdown() override
Interrupt blocking operations for shutdown.
ErrorCode setup_outbound_connection_client() override
Connect the outbound channel (dApp role: PUB connect to the RAN's subscriber endpoint).
ErrorCode setup_inbound_connection_client() override
Connect the inbound channel (dApp role: SUB connect to the RAN's publisher endpoint).
std::vector< uint64_t > take_closed_peers() override
Peers whose connection has closed since the last call (RAN role).
ErrorCode setup_initial_connection() override
Set up the initial connection (setup channel).
ErrorCode setup_inbound_connection() override
Set up the inbound connection (SUB channel).
ErrorCode setup_outbound_connection() override
Set up the outbound connection (PUB channel).
bool is_connected() const noexcept override
Check if connector is connected.
Abstract E3 Connector interface for transport layer.
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
Definition types.hpp:177
E3TransportLayer
Transport layer types (matches Python E3TransportLayer).
Definition types.hpp:85
E3LinkLayer
Link layer types (matches Python E3LinkLayer).
Definition types.hpp:62
@ POSIX
POSIX socket-based transport.
Definition types.hpp:64
PduClass
What a PDU is for, so that a link that has streams can pick one.
Socket and admission options of the POSIX link.