libcluon
0.0.148
|
#include <UDPReceiver.hpp>
Public Member Functions | |
UDPReceiver (const std::string &receiveFromAddress, uint16_t receiveFromPort, std::function< void(std::string &&, std::string &&, std::chrono::system_clock::time_point &&)> delegate, uint16_t localSendFromPort=0) noexcept | |
~UDPReceiver () noexcept | |
bool | isRunning () const noexcept |
To receive data from a UDP socket, simply include the header #include <cluon/UDPReceiver.hpp>
.
Next, create an instance of class cluon::UDPReceiver
as follows: cluon::UDPReceiver receiver("127.0.0.1", 1234, delegate);
. The first parameter is of type std::string
expecting a numerical IPv4 address, the second parameter specifies the UDP port, from which data shall be received from, and the last parameter is of type std::function
that is called whenever new bytes are available to be processed.
The complete signature for the delegate function is std::function<void(std::string &&, std::string &&, std::chrono::system_clock::time_point &&) noexcept>
: The first parameter contains the bytes that have been received, the second parameter containes the human-readable representation of the sender (X.Y.Z.W:ABCD), and the last parameter is the time stamp when the data has been received. An example using a C++ lambda expression would look as follows:
After creating an instance of class cluon::UDPReceiver
, it is immediately activated and concurrently waiting for data in a separate thread. To check whether the instance was created successfully and running, the method isRunning()
should be called.
A complete example is available here.
|
noexcept |
Constructor.
receiveFromAddress | Numerical IPv4 address to receive UDP packets from. |
receiveFromPort | Port to receive UDP packets from. |
delegate | Functional (noexcept) to handle received bytes; parameters are received data, sender, timestamp. |
localSendFromPort | Port that an application is using to send data. This port (> 0) is ignored when data is received. |
References cluon::getIPv4FromHostname(), and retVal.
|
noexcept |
References retVal.
|
noexcept |
References cluon::TerminateHandler::instance(), cluon::TerminateHandler::isTerminated, cluon::MAX_SIZE_UDP_PACKET, cluon::time::now(), cluon::SIZE_IPv4_HEADER, and cluon::SIZE_UDP_HEADER.