Network

samples/advanced/network.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * Sample code for custom networking.
 */
#include <asio.hpp>

#include <cepton_sdk_api.hpp>

using asio::ip::udp;

class SocketListener {
 public:
  SocketListener() : m_socket(m_io_service, udp::v4()) {
    m_socket.set_option(asio::socket_base::reuse_address(true));
    m_socket.bind(udp::endpoint(udp::v4(), 8808));
  }

  void run() {
    listen();
    m_io_service.run_for(std::chrono::seconds(5));
  }

  void listen() {
    m_socket.async_receive_from(
        asio::buffer(m_buffer), m_end_point,
        [this](const asio::error_code& error, std::size_t buffer_size) {
          if (buffer_size == 0) return;
          if (error == asio::error::operation_aborted) return;
          const CeptonSensorHandle handle =
              m_end_point.address().to_v4().to_ulong();
          // For more accurate timestamps, a separate network receive thread
          // should be
          // used.
          const int64_t timestamp = cepton_sdk::util::get_timestamp_usec();
          CEPTON_CHECK_ERROR(cepton_sdk::mock_network_receive(
              handle, timestamp, m_buffer.data(), buffer_size));
          listen();
        });
  }

 private:
  asio::io_context m_io_service;
  udp::socket m_socket;
  udp::endpoint m_end_point;
  std::array<uint8_t, 4096> m_buffer;
};

int main() {
  // Initialize
  auto options = cepton_sdk::create_options();
  options.control_flags |= CEPTON_SDK_CONTROL_DISABLE_NETWORK;
  options.frame.mode = CEPTON_SDK_FRAME_COVER;
  CEPTON_CHECK_ERROR(cepton_sdk::api::initialize(options));

  // Listen for points
  cepton_sdk::api::SensorImageFrameCallback callback;
  CEPTON_CHECK_ERROR(callback.initialize());
  CEPTON_CHECK_ERROR(
      callback.listen([](cepton_sdk::SensorHandle handle, std::size_t n_points,
                         const cepton_sdk::SensorImagePoint* c_image_points) {
        std::printf("Received %i points from sensor %lli\n", (int)n_points,
                    (long long)handle);
      }));

  SocketListener listener;
  listener.run();

  // Deinitialize
  cepton_sdk::deinitialize().ignore();
}