LivoxProto1: Add UdpCommandDemuxer.

We haven't genericized it with an unordered_map or integrated it
into device.cpp's async methods yet.
This commit is contained in:
2025-10-22 06:17:42 -04:00
parent 66a9db13c3
commit 10afec2532
7 changed files with 391 additions and 1 deletions
@@ -0,0 +1,66 @@
#ifndef UDP_COMMAND_DEMUXER_H
#define UDP_COMMAND_DEMUXER_H
#include <atomic>
#include <memory>
#include <boost/asio.hpp>
#include <componentThread.h>
namespace livoxProto1 {
// Forward declarations
class DeviceManager;
namespace comms {
/**
* UdpCommandDemuxer - Routes UDP command datagrams to appropriate devices
*
* This class listens on the command port (65000) for incoming UDP datagrams
* from Livox devices and routes them to the appropriate Device based on
* the source IP address.
*/
class UdpCommandDemuxer
{
public:
UdpCommandDemuxer(
const std::shared_ptr<smo::ComponentThread>& componentThread,
DeviceManager& deviceManager,
uint16_t commandPort = 65000);
~UdpCommandDemuxer();
void start();
void stop();
bool isRunning() const { return isActive.load(); }
private:
void setupSocket();
void startAsyncReceive();
void onDataReady(const boost::system::error_code& error);
void onTimerTick(const boost::system::error_code& error);
void processIncomingData();
std::shared_ptr<smo::ComponentThread> componentThread;
DeviceManager& deviceManager;
uint16_t commandPort;
// Socket and async objects
std::unique_ptr<boost::asio::posix::stream_descriptor> socketDesc;
boost::asio::deadline_timer timer;
// State management
std::atomic<bool> isActive{false};
std::atomic<bool> shouldStop{false};
// Receive buffer
uint8_t receiveBuffer[1024];
struct sockaddr_in senderAddr;
socklen_t senderAddrLen;
ssize_t bytesReceived;
};
} // namespace comms
} // namespace livoxProto1
#endif // UDP_COMMAND_DEMUXER_H