Skip to content

UdpTransport

class nmeasim::io::UdpTransport · Library API

Sends every line as one UDP datagram to the destination of a UdpConfig.

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

Inherits nmeasim::io::Transport

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp (line 65)

UDP has no connection, so the transport knows nothing about receivers: client_count is 1 while open, whether or not anyone listens. Socket errors, such as a datagram the system refuses to send, are reported through error_occurred and leave the transport open.

Summary

Public member functions

Name Description
UdpTransport() Creates a closed transport; the destination is resolved and the socket bound in open.
~UdpTransport() Destroys the transport, closing the socket first.
description() Returns the mode, destination and port.
open() Resolves the destination, binds the socket and applies the multicast settings.
close() Closes the socket and moves to State::Closed; the resolved destination is kept.
write() Sends line as one datagram to the destination.
destination() Returns the address datagrams are sent to.
config() Returns the settings the transport was created with.

Private member functions

Name Description
resolve_destination() Sets destination_ from config_, applying the broadcast default for an empty address.

Private data members

Name Description
socket_ The sending socket, bound in open.
config_ Settings passed to the constructor.
destination_ Destination resolved by resolve_destination; null until then.

Public member functions

UdpTransport()

explicit UdpTransport(UdpConfig config, QObject* parent = nullptr)

Creates a closed transport; the destination is resolved and the socket bound in open.

Parameters

Name Type Description
config UdpConfig Addressing mode, destination, source interface and multicast settings, copied into the transport.
parent QObject* Qt parent that owns the transport; null leaves ownership to the caller.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:75 · defined in src/io/src/transports/udp_transport.cpp:26

~UdpTransport()

~UdpTransport() override

Destroys the transport, closing the socket first.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:77 · defined in src/io/src/transports/udp_transport.cpp:32

description()

QString description() const override

Returns the mode, destination and port.

Returns QString: For example UDP unicast to 127.0.0.1:10110; the destination is the resolved address once open has resolved it, and UdpConfig::address before.

Overrides description · Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:83 · defined in src/io/src/transports/udp_transport.cpp:36

open()

bool open() override

Resolves the destination, binds the socket and applies the multicast settings.

Returns true at once when already open. The socket is bound to an ephemeral port on the first IPv4 address of UdpConfig::interface_name, or on every IPv4 address when no interface is set. Fails when the destination address does not parse or is not an IPv4 address, when the interface is unknown, not up and running, or has no IPv4 address, and when the socket cannot be bound; last_error names the address or interface concerned.

Returns bool: True when the transport is open, false when any step failed.

Overrides open · Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:94 · defined in src/io/src/transports/udp_transport.cpp:65

close()

void close() override

Closes the socket and moves to State::Closed; the resolved destination is kept.

Overrides close · Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:96 · defined in src/io/src/transports/udp_transport.cpp:107

write()

void write(const QByteArray& line) override

Sends line as one datagram to the destination.

Parameters

Name Type Description
line const QByteArray& One complete line, terminator included; it must fit in one datagram.

Overrides write · Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:100 · defined in src/io/src/transports/udp_transport.cpp:114

destination()

QHostAddress destination() const

Returns the address datagrams are sent to.

Returns QHostAddress: The address resolved by the last open, with the broadcast default applied; a null address before the first open or when the last one found the address invalid or not IPv4.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:107

config()

const UdpConfig& config() const noexcept

Returns the settings the transport was created with.

Returns const UdpConfig&: A reference valid for the lifetime of the transport.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:111

Private member functions

resolve_destination()

bool resolve_destination()

Sets destination_ from config_, applying the broadcast default for an empty address.

Calls fail when the address does not parse or is not an IPv4 address.

Returns bool: True when a destination was set, false after a failure.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:120 · defined in src/io/src/transports/udp_transport.cpp:43

Private data members

socket_

QUdpSocket socket_

The sending socket, bound in open.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:123

config_

UdpConfig config_

Settings passed to the constructor.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:125

destination_

QHostAddress destination_

Destination resolved by resolve_destination; null until then.

Declared in src/io/include/nmeasim/io/transports/udp_transport.hpp:127

Inherited members

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