libcluon  0.0.148
UDPReceiver.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2018 Christian Berger
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 #ifndef CLUON_UDPRECEIVER_HPP
10 #define CLUON_UDPRECEIVER_HPP
11 
13 #include "cluon/cluon.hpp"
14 
15 // clang-format off
16 #ifdef WIN32
17  #include <Winsock2.h> // for WSAStartUp
18  #include <ws2tcpip.h> // for SOCKET
19 #else
20  #include <netinet/in.h>
21 #endif
22 // clang-format on
23 
24 #include <cstdint>
25 #include <atomic>
26 #include <condition_variable>
27 #include <deque>
28 #include <chrono>
29 #include <functional>
30 #include <memory>
31 #include <mutex>
32 #include <set>
33 #include <string>
34 #include <thread>
35 
36 namespace cluon {
75  private:
76  UDPReceiver(const UDPReceiver &) = delete;
77  UDPReceiver(UDPReceiver &&) = delete;
78  UDPReceiver &operator=(const UDPReceiver &) = delete;
79  UDPReceiver &operator=(UDPReceiver &&) = delete;
80 
81  public:
90  UDPReceiver(const std::string &receiveFromAddress,
91  uint16_t receiveFromPort,
92  std::function<void(std::string &&, std::string &&, std::chrono::system_clock::time_point &&)> delegate,
93  uint16_t localSendFromPort = 0) noexcept;
94  ~UDPReceiver() noexcept;
95 
99  bool isRunning() const noexcept;
100 
101  private:
107  void closeSocket(int errorCode) noexcept;
108 
109  void readFromSocket() noexcept;
110 
111  private:
112  int32_t m_socket{-1};
113  bool m_isBlockingSocket{true};
114  std::set<unsigned long> m_listOfLocalIPAddresses{};
115  uint16_t m_localSendFromPort;
116  struct sockaddr_in m_receiveFromAddress {};
117  struct ip_mreq m_mreq {};
118  bool m_isMulticast{false};
119 
120  std::atomic<bool> m_readFromSocketThreadRunning{false};
121  std::thread m_readFromSocketThread{};
122 
123  private:
124  std::function<void(std::string &&, std::string &&, std::chrono::system_clock::time_point)> m_delegate{};
125 
126  private:
127  class PipelineEntry {
128  public:
129  std::string m_data;
130  std::string m_from;
131  std::chrono::system_clock::time_point m_sampleTime;
132  };
133 
134  std::shared_ptr<cluon::NotifyingPipeline<PipelineEntry>> m_pipeline{};
135 };
136 } // namespace cluon
137 
138 #endif
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: UDPReceiver.hpp:74