Skip to content

TcpClientTransport

class nmeasim::io::TcpClientTransport · Library API

Connects to a TCP server, sends it every line, and reconnects automatically after a failed attempt or a dropped connection.

#include <nmeasim/io/transports/tcp_client_transport.hpp>

Inherits nmeasim::io::Transport

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp (line 30)

The transport is in State::Opening during the first attempt and after the server dropped the connection, and moves to State::Open when a connection is established. An attempt that fails (the connection is refused, the host is not found, the attempt times out) moves it to State::Failed, with last_error saying why; it stays there, retrying, until an attempt succeeds. Every error, a peer closing the connection included, is reported through error_occurred and followed by a new attempt after the reconnect interval, until close is called. Lines written while disconnected are dropped, and data received from the server is discarded.

Summary

Public member functions

Name Description
TcpClientTransport() Creates a closed transport; no connection is attempted until open.
~TcpClientTransport() Destroys the transport, closing the connection first.
description() Returns the server's host and port.
open() Starts connecting to the server and keeps trying until close.
close() Stops reconnecting, aborts the connection and moves to State::Closed.
write() Queues line on the connected socket.

Private member functions

Name Description
connect_now() Starts a connection attempt, unless the transport is closed or the socket is not in the unconnected state.
schedule_reconnect() Starts the reconnect timer, unless the transport is closed or the timer is already running; a disconnection and an error reported for the same event therefore cause one attempt only.

Private data members

Name Description
socket_ The connection to the server; reused for every attempt.
reconnect_timer_ Single-shot timer whose interval is the reconnect delay; its timeout calls connect_now.
host_ Host name or IP address of the server.
port_ TCP port of the server.
wanted_open_ True between open and close: the transport should be connected and keeps reconnecting.

Public member functions

TcpClientTransport()

TcpClientTransport(
    QString host,
    quint16 port,
    int reconnect_interval_ms = 2000,
    QObject* parent = nullptr)

Creates a closed transport; no connection is attempted until open.

Parameters

Name Type Description
host QString Host name or IP address of the server; a name is looked up when connecting, and a failed lookup is retried like a refused connection.
port quint16 TCP port of the server, for example 10110 for NMEA 0183.
reconnect_interval_ms int Delay in milliseconds between a failed attempt or a dropped connection and the next attempt. The default of 2000 matches the profile default of reconnect_ms.
parent QObject* Qt parent that owns the transport; null leaves ownership to the caller.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:43 · defined in src/io/src/transports/tcp_client_transport.cpp:10

~TcpClientTransport()

~TcpClientTransport() override

Destroys the transport, closing the connection first.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:46 · defined in src/io/src/transports/tcp_client_transport.cpp:52

description()

QString description() const override

Returns the server's host and port.

Returns QString: For example TCP client to 127.0.0.1:10110.

Overrides description · Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:51 · defined in src/io/src/transports/tcp_client_transport.cpp:56

open()

bool open() override

Starts connecting to the server and keeps trying until close.

Moves to State::Open at once when the socket is still connected, otherwise to State::Opening and starts a connection attempt; the move to State::Open follows asynchronously once the server accepts.

Returns bool: Always true: connection failures are asynchronous; they are reported through state_changed and error_occurred, and trigger a new attempt.

Overrides open · Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:60 · defined in src/io/src/transports/tcp_client_transport.cpp:60

close()

void close() override

Stops reconnecting, aborts the connection and moves to State::Closed.

Aborting discards data queued in the socket that has not been sent yet.

Overrides close · Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:64 · defined in src/io/src/transports/tcp_client_transport.cpp:71

write()

void write(const QByteArray& line) override

Queues line on the connected socket.

The line is dropped unless the transport is open and the socket connected. The transport sets no limit on how much can be queued when the server reads slowly.

Parameters

Name Type Description
line const QByteArray& One complete line, terminator included.

Overrides write · Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:71 · defined in src/io/src/transports/tcp_client_transport.cpp:80

Private member functions

connect_now()

void connect_now()

Starts a connection attempt, unless the transport is closed or the socket is not in the unconnected state.

Connected to the timeout of reconnect_timer_.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:76 · defined in src/io/src/transports/tcp_client_transport.cpp:86

schedule_reconnect()

void schedule_reconnect()

Starts the reconnect timer, unless the transport is closed or the timer is already running; a disconnection and an error reported for the same event therefore cause one attempt only.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:80 · defined in src/io/src/transports/tcp_client_transport.cpp:93

Private data members

socket_

QTcpSocket socket_

The connection to the server; reused for every attempt.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:83

reconnect_timer_

QTimer reconnect_timer_

Single-shot timer whose interval is the reconnect delay; its timeout calls connect_now.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:86

host_

QString host_

Host name or IP address of the server.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:88

port_

quint16 port_

TCP port of the server.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:90

wanted_open_

bool wanted_open_{false}

True between open and close: the transport should be connected and keeps reconnecting.

Declared in src/io/include/nmeasim/io/transports/tcp_client_transport.hpp:93

Inherited members

From nmeasim::io::TcpClientTransport: bytes_written(), client_count(), client_count_changed(), count_bytes(), error_occurred(), fail(), is_open(), last_error(), set_state(), State, state(), state_changed(), Transport()