Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
UdpReceiver.h
Go to the documentation of this file.
1 // This file is part of CoreLibrary containing useful reusable utility
2 // classes.
3 //
4 // Copyright (C) 2014 to present, Duncan Crutchley
5 // Contact <dac1976github@outlook.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License and GNU Lesser General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // and GNU Lesser General Public License along with this program. If
20 // not, see <http://www.gnu.org/licenses/>.
21 
27 #ifndef UDPRECEIVER
28 #define UDPRECEIVER
29 
30 #include <mutex>
31 #include "IoContextThreadGroup.h"
32 #include "Threads/SyncEvent.h"
33 
35 namespace core_lib
36 {
38 namespace asio
39 {
41 namespace udp
42 {
43 
49 class CORE_LIBRARY_DLL_SHARED_API UdpReceiver final
50 {
51 public:
53  UdpReceiver() = delete;
70  UdpReceiver(boost_iocontext_t& ioContext, uint16_t listenPort,
71  const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
72  const defs::message_received_handler_t& messageReceivedHandler,
73  eUdpOption receiveOptions = eUdpOption::broadcast,
74  size_t receiveBufferSize = DEFAULT_UDP_BUF_SIZE);
90  UdpReceiver(uint16_t listenPort, const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
91  const defs::message_received_handler_t& messageReceivedHandler,
92  eUdpOption receiveOptions = eUdpOption::broadcast,
93  size_t receiveBufferSize = DEFAULT_UDP_BUF_SIZE);
95  UdpReceiver(const UdpReceiver&) = delete;
97  UdpReceiver& operator=(const UdpReceiver&) = delete;
99  UdpReceiver(UdpReceiver&&) = delete;
101  UdpReceiver& operator=(UdpReceiver&&) = delete;
103  ~UdpReceiver();
108  uint16_t ListenPort() const;
110  void CloseSocket();
111 
112 private:
118  void CreateUdpSocket(eUdpOption receiveOptions, size_t receiveBufferSize);
120  void StartAsyncRead();
126  void ReadComplete(const boost_sys::error_code& error, size_t bytesReceived);
131  void SetClosing(bool closing);
136  bool IsClosing() const;
138  void ProcessCloseSocket();
139 
140 private:
142  mutable std::mutex m_closingMutex;
144  threads::SyncEvent m_closedEvent{};
146  bool m_closing{false};
148  std::unique_ptr<IoContextThreadGroup> m_ioThreadGroup{};
150  boost_iocontext_t::strand m_strand;
152  uint16_t m_listenPort{0};
154  defs::check_bytes_left_to_read_t m_checkBytesLeftToRead{};
156  defs::message_received_handler_t m_messageReceivedHandler{};
158  defs::char_buffer_t m_receiveBuffer{};
160  defs::char_buffer_t m_messageBuffer{};
162  boost_udp_t::endpoint m_senderEndpoint{};
164  boost_udp_t::socket m_socket;
165 };
166 
167 } // namespace udp
168 } // namespace asio
169 } // namespace core_lib
170 
171 #endif // UDPRECEIVER
A general purpose UDP receiver.
Definition: UdpReceiver.h:49
eUdpOption
The udp options enumeration.
Definition: AsioDefines.h:98
boost_iocontext_t::strand m_strand
I/O context strand.
Definition: UdpReceiver.h:150
std::function< size_t(const char_buffer_t &)> check_bytes_left_to_read_t
Typedef to bytes left to reading checking utility function object.
Definition: AsioDefines.h:288
File containing declarations relating the IoContextThreadGroup class.
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
Class defining a thread synchronisation event.
Definition: SyncEvent.h:70
The core_lib namespace.
Definition: AsioDefines.h:59
boost_udp_t::socket m_socket
UDP socket.
Definition: UdpReceiver.h:164
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
std::mutex m_closingMutex
Mutex to protect shutdown of receiver.
Definition: UdpReceiver.h:142
File containing declaration of SyncEvent class.
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239