15 #ifndef PSEN_SCAN_V2_STANDALONE_UDP_CLIENT_H 16 #define PSEN_SCAN_V2_STANDALONE_UDP_CLIENT_H 27 #include <arpa/inet.h> 34 #include <boost/asio.hpp> 35 #include <boost/bind.hpp> 46 namespace communication_layer
49 std::function<void(const data_conversion_layer::RawDataConstPtr&, const std::size_t&, const int64_t& timestamp)>;
110 const unsigned short& host_port,
111 const unsigned int& endpoint_ip,
112 const unsigned short& endpoint_port);
153 boost::asio::ip::address_v4 hostIp();
158 void sendCompleteHandler(
const boost::system::error_code&
error, std::size_t bytes_transferred);
163 boost::asio::io_service::work work_{ io_service_ };
177 const unsigned short& host_port,
178 const unsigned int& endpoint_ip,
179 const unsigned short& endpoint_port)
180 : message_callback_(message_callback)
181 , error_callback_(error_callback)
182 , socket_(io_service_,
boost::asio::ip::udp::endpoint(
boost::asio::ip::udp::v4(), host_port))
183 , endpoint_(
boost::asio::ip::address_v4(endpoint_ip), endpoint_port)
185 if (!message_callback)
187 throw std::invalid_argument(
"New message callback is invalid");
192 throw std::invalid_argument(
"Error callback is invalid");
202 catch (
const boost::system::system_error& ex)
235 catch (
const boost::system::system_error& ex)
244 return socket_.local_endpoint().address().to_v4();
266 if (error || bytes_transferred == 0)
268 PSENSCAN_ERROR(
"UdpClient",
"Failed to send data. Error message: {}", error.message());
278 socket_.async_send(boost::asio::buffer(data.data(), data.size()),
281 boost::asio::placeholders::error,
282 boost::asio::placeholders::bytes_transferred));
288 std::promise<void> post_done_barrier;
289 const auto post_done_future{ post_done_barrier.get_future() };
293 io_service_.post([
this, modi, &post_done_barrier]() {
295 post_done_barrier.set_value();
297 post_done_future.wait();
303 [
this, modi](
const boost::system::error_code& error_code,
const std::size_t& bytes_received) {
304 if (error_code || bytes_received == 0)
328 #endif // PSEN_SCAN_V2_STANDALONE_UDP_CLIENT_H Helper for asynchronously sending and receiving data via UDP.
static auto getCurrentTime()
std::function< void(const std::string &)> ErrorCallback
boost::asio::ip::udp::endpoint endpoint_
OpenConnectionFailure(const std::string &msg="Failure while opening connection")
std::function< void(const data_conversion_layer::RawDataConstPtr &, const std::size_t &, const int64_t ×tamp)> NewMessageCallback
ReceiveMode
Lists the different possible receive modi.
~UdpClientImpl()
Closes the UDP connection and stops all pending asynchronous operation.
void close()
Closes the UDP connection and stops all pending asynchronous operation.
void write(std::ostringstream &os, const T &data)
void sendCompleteHandler(const boost::system::error_code &error, std::size_t bytes_transferred)
void stop()
Stops the underlying io_service so that no messages are received anymore.
#define PSENSCAN_DEBUG(name,...)
Exception thrown if the UDP socket cannot be opened.
ErrorCallback error_callback_
Wait for one message and then stop listening.
std::vector< char > RawData
void write(const data_conversion_layer::RawData &data)
Asynchronously sends the specified data to the other endpoint.
data_conversion_layer::RawDataPtr received_data_
Root namespace in which the software components to communicate with the scanner (firmware-version: 2)...
boost::asio::ip::address_v4 hostIp()
Returns local ip address of current socket connection.
#define PSENSCAN_ERROR(name,...)
std::shared_ptr< RawData > RawDataPtr
Continuously wait for new messages. In other words, after a message is received, automatically start ...
void asyncReceive(const ReceiveMode &modi)
boost::asio::ip::udp::socket socket_
boost::asio::io_service io_service_
void startAsyncReceiving(const ReceiveMode &modi=ReceiveMode::continuous)
Starts an asynchronous process listing to (a) new message(s) from the other endpoint. If the function continuously listens to new messages or only listens for one message is specified by the receive modi specified.
UdpClientImpl(const NewMessageCallback &msg_callback, const ErrorCallback &error_callback, const unsigned short &host_port, const unsigned int &endpoint_ip, const unsigned short &endpoint_port)
Opens an UDP connection.
static constexpr std::size_t MAX_UDP_PAKET_SIZE
std::thread io_service_thread_
CloseConnectionFailure(const std::string &msg="Failure while closing connection")
NewMessageCallback message_callback_
Exception thrown if the UDP socket cannot be closed.