Transport¶
class nmeasim::io::Transport · Library API
Abstract output channel: TCP server or client, UDP, WebSocket server, serial port, file, log or standard output.
#include <nmeasim/io/transport.hpp>
Inherits QObject
Inherited by nmeasim::io::FileTransport, nmeasim::io::LogTransport, nmeasim::io::SerialTransport, nmeasim::io::StdoutTransport, nmeasim::io::TcpClientTransport, nmeasim::io::TcpServerTransport, nmeasim::io::UdpTransport, nmeasim::io::WebSocketServerTransport
Declared in src/io/include/nmeasim/io/transport.hpp (line 35)
A transport moves through the life cycle State::Closed, State::Opening, State::Open and back to State::Closed, or to State::Failed when opening fails or the device goes away. Only an open transport delivers what write is given; every implementation silently drops lines in any other state. Transports send the payload as given, byte for byte, except LogTransport, which records each line with a timestamp in front and a single line feed at the end.
Implementations use Qt sockets, devices and timers, so a transport is used from the thread that created it, and that thread runs a Qt event loop: asynchronous events (a client connecting, a connection dropping, a reconnection timer) are processed there and the corresponding signals are emitted there.
Summary¶
Public types
| Name | Description |
|---|---|
State |
Life-cycle state of a transport, reported through state_changed. |
Public member functions
| Name | Description |
|---|---|
Transport() |
Creates a transport in the State::Closed state. |
description() |
Returns a human-readable summary of the channel and its settings. |
open() |
Starts listening, connecting or opening the device or file. |
close() |
Stops listening, disconnects or closes the file, then moves to State::Closed. |
write() |
Sends one line to every connected consumer. |
state() |
Returns the current life-cycle state. |
is_open() |
Returns whether the transport is in State::Open. |
last_error() |
Returns the message of the most recent failure. |
client_count() |
Returns the number of consumers currently attached. |
bytes_written() |
Returns the total number of bytes written to consumers since construction. |
Signals
| Name | Description |
|---|---|
state_changed() |
Emitted when the life-cycle state changes, synchronously from inside open, close or fail, or from an asynchronous socket or device event. |
error_occurred() |
Emitted for every error, whether or not it moves the transport to State::Failed. |
client_count_changed() |
Emitted when a client connects or disconnects. |
Protected member functions
| Name | Description |
|---|---|
set_state() |
Moves to state. |
fail() |
Records an error and moves to State::Failed. |
count_bytes() |
Adds a write result to bytes_written. |
Private data members
| Name | Description |
|---|---|
state_ |
Current life-cycle state; changed only through set_state. |
last_error_ |
Message of the most recent failure; empty until fail is first called. |
bytes_written_ |
Bytes written since construction, summed over every consumer. |
Public types¶
State¶
enum class State
Life-cycle state of a transport, reported through state_changed.
| Enumerator | Value | Description |
|---|---|---|
Closed |
Not opened yet, or closed by close. The initial state. |
|
Opening |
Opening, or waiting to reconnect (TCP client) after the peer dropped the connection. | |
Open |
Ready: write delivers data. |
|
Failed |
Opening failed or the device went away; last_error says why. open may be called again. A TCP client keeps retrying by itself in this state. |
Declared in src/io/include/nmeasim/io/transport.hpp:40
Public member functions¶
Transport()¶
explicit Transport(QObject* parent = nullptr)
Creates a transport in the State::Closed state.
Parameters
| Name | Type | Description |
|---|---|---|
parent |
QObject* |
Qt parent that owns the transport; null leaves ownership to the caller, as SimulationRunner does with its std::unique_ptr. |
Declared in src/io/include/nmeasim/io/transport.hpp:58 · defined in src/io/src/transport.cpp:10
description()¶
virtual QString description() const = 0
Returns a human-readable summary of the channel and its settings.
Returns QString: A one-line text such as TCP server on 127.0.0.1:10110 or Serial port /dev/ttyUSB0 at 4800 baud, used in error reports and status displays.
Declared in src/io/include/nmeasim/io/transport.hpp:64
open()¶
virtual bool open() = 0
Starts listening, connecting or opening the device or file.
On success the transport ends in State::Open, or in State::Opening when the connection is made asynchronously (TCP client). On failure it moves to State::Failed and emits error_occurred. Emits state_changed for every state it passes through.
Returns bool: False when opening fails immediately; true otherwise. Asynchronous failures are reported later through error_occurred and state_changed.
Declared in src/io/include/nmeasim/io/transport.hpp:75
close()¶
virtual void close() = 0
Stops listening, disconnects or closes the file, then moves to State::Closed.
Stops any automatic reconnection and detaches every client. Safe to call in any state, including when already closed. Emits state_changed when the state changes.
Declared in src/io/include/nmeasim/io/transport.hpp:80
write()¶
virtual void write(const QByteArray& line) = 0
Sends one line to every connected consumer.
Silently drops the line while the transport is not open. Write errors are not reported by the return value; transports that detect them report them through error_occurred.
Parameters
| Name | Type | Description |
|---|---|---|
line |
const QByteArray& |
One complete line, terminator included. |
Declared in src/io/include/nmeasim/io/transport.hpp:89
state()¶
State state() const noexcept
Returns the current life-cycle state.
Returns State: The state last set by the implementation; State::Closed initially.
Declared in src/io/include/nmeasim/io/transport.hpp:94
is_open()¶
bool is_open() const noexcept
Returns whether the transport is in State::Open.
Returns bool: True in State::Open, false in every other state.
Declared in src/io/include/nmeasim/io/transport.hpp:98
last_error()¶
QString last_error() const
Returns the message of the most recent failure.
Returns QString: The message passed to the last call of fail; empty when none has occurred. It is not cleared when the transport opens again.
Declared in src/io/include/nmeasim/io/transport.hpp:103
client_count()¶
virtual int client_count() const
Returns the number of consumers currently attached.
The default suits point-to-point transports; the TCP and WebSocket servers override it with their number of connected clients.
Returns int: 1 while open and 0 otherwise for point-to-point transports; the number of connected clients for servers.
Declared in src/io/include/nmeasim/io/transport.hpp:111
bytes_written()¶
qint64 bytes_written() const noexcept
Returns the total number of bytes written to consumers since construction.
Returns qint64: Bytes summed over every consumer, so a server with three clients counts each line three times. Greetings and log headers are included; failed writes are not.
Declared in src/io/include/nmeasim/io/transport.hpp:116
Signals¶
state_changed()¶
void state_changed(nmeasim::io::Transport::State state)
Emitted when the life-cycle state changes, synchronously from inside open, close or fail, or from an asynchronous socket or device event.
Not emitted when the new state equals the current one.
Parameters
| Name | Type | Description |
|---|---|---|
state |
nmeasim::io::Transport::State |
The new state. |
Declared in src/io/include/nmeasim/io/transport.hpp:125
error_occurred()¶
void error_occurred(const QString& message)
Emitted for every error, whether or not it moves the transport to State::Failed.
Emitted synchronously by fail, and by implementations for socket errors that do not end the transport, such as a TCP client losing its peer before reconnecting.
Parameters
| Name | Type | Description |
|---|---|---|
message |
const QString& |
Human-readable description of the error. |
Declared in src/io/include/nmeasim/io/transport.hpp:132
client_count_changed()¶
void client_count_changed(int count)
Emitted when a client connects or disconnects.
Only the servers (TCP and WebSocket) emit it; they also emit it with 0 from close, even when no client was attached.
Parameters
| Name | Type | Description |
|---|---|---|
count |
int |
The new number of connected clients. |
Declared in src/io/include/nmeasim/io/transport.hpp:139
Protected member functions¶
set_state()¶
void set_state(State state)
Moves to state.
Emits state_changed only when state differs from the current one.
Parameters
| Name | Type | Description |
|---|---|---|
state |
State |
The new life-cycle state. |
Declared in src/io/include/nmeasim/io/transport.hpp:147 · defined in src/io/src/transport.cpp:12
fail()¶
void fail(const QString& message)
Records an error and moves to State::Failed.
Stores message as last_error, then emits state_changed (unless the transport had already failed) and error_occurred, in that order.
Parameters
| Name | Type | Description |
|---|---|---|
message |
const QString& |
Human-readable description of the failure. |
Declared in src/io/include/nmeasim/io/transport.hpp:154 · defined in src/io/src/transport.cpp:20
count_bytes()¶
void count_bytes(qint64 count) noexcept
Adds a write result to bytes_written.
Parameters
| Name | Type | Description |
|---|---|---|
count |
qint64 |
Bytes written, as returned by a Qt write call; zero and negative values (write errors) are ignored. |
Declared in src/io/include/nmeasim/io/transport.hpp:159 · defined in src/io/src/transport.cpp:26
Private data members¶
state_¶
State state_{State::Closed}
Current life-cycle state; changed only through set_state.
Declared in src/io/include/nmeasim/io/transport.hpp:163
last_error_¶
QString last_error_
Message of the most recent failure; empty until fail is first called.
Declared in src/io/include/nmeasim/io/transport.hpp:165
bytes_written_¶
qint64 bytes_written_{0}
Bytes written since construction, summed over every consumer.
Declared in src/io/include/nmeasim/io/transport.hpp:167