Go to the documentation of this file.
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
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);
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)
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)
306 error_callback_(error_code.message());
310 message_callback_(received_data_, bytes_received, util::getCurrentTime());
319 inline UdpClientImpl::OpenConnectionFailure::OpenConnectionFailure(
const std::string& msg) :
std::runtime_error(msg)
328 #endif // PSEN_SCAN_V2_STANDALONE_UDP_CLIENT_H
Helper for asynchronously sending and receiving data via UDP.
boost::asio::io_service io_service_
CloseConnectionFailure(const std::string &msg="Failure while closing connection")
ErrorCallback error_callback_
void write(const data_conversion_layer::RawData &data)
Asynchronously sends the specified data to the other endpoint.
NewMessageCallback message_callback_
std::shared_ptr< const RawData > RawDataConstPtr
void stop()
Stops the underlying io_service so that no messages are received anymore.
OpenConnectionFailure(const std::string &msg="Failure while opening connection")
#define PSENSCAN_DEBUG(name,...)
~UdpClientImpl()
Closes the UDP connection and stops all pending asynchronous operation.
@ single
Wait for one message and then stop listening.
std::shared_ptr< RawData > RawDataPtr
boost::asio::io_service::work work_
std::thread io_service_thread_
boost::asio::ip::udp::socket socket_
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.
void asyncReceive(const ReceiveMode &modi)
static constexpr std::size_t MAX_UDP_PAKET_SIZE
void close()
Closes the UDP connection and stops all pending asynchronous operation.
Exception thrown if the UDP socket cannot be opened.
data_conversion_layer::RawDataPtr received_data_
ReceiveMode
Lists the different possible receive modi.
boost::asio::ip::udp::endpoint endpoint_
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::function< void(const std::string &)> ErrorCallback
std::function< void(const data_conversion_layer::RawDataConstPtr &, const std::size_t &, const int64_t ×tamp)> NewMessageCallback
std::vector< char > RawData
@ continuous
Continuously wait for new messages. In other words, after a message is received, automatically start ...
Exception thrown if the UDP socket cannot be closed.
void sendCompleteHandler(const boost::system::error_code &error, std::size_t bytes_transferred)
void startAsyncReceiving(const ReceiveMode &modi=ReceiveMode::continuous)
Starts an asynchronous process listing to (a) new message(s) from the other endpoint....
def error(*args, **kwargs)
psen_scan_v2
Author(s): Pilz GmbH + Co. KG
autogenerated on Sat Jun 22 2024 02:46:12