Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
MulticastReceiver.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 MULTICASTRECEIVER
28 #define MULTICASTRECEIVER
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 MulticastReceiver final
50 {
51 public:
53  MulticastReceiver() = delete;
71  MulticastReceiver(boost_iocontext_t& ioContext, const defs::connection_t& multicastConnection,
72  const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
73  const defs::message_received_handler_t& messageReceivedHandler,
74  const std::string& interfaceAddress = "",
75  size_t receiveBufferSize = DEFAULT_UDP_BUF_SIZE);
92  MulticastReceiver(const defs::connection_t& multicastConnection,
93  const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
94  const defs::message_received_handler_t& messageReceivedHandler,
95  const std::string& interfaceAddress = "",
96  size_t receiveBufferSize = DEFAULT_UDP_BUF_SIZE);
98  MulticastReceiver(const MulticastReceiver&) = delete;
100  MulticastReceiver& operator=(const MulticastReceiver&) = delete;
104  MulticastReceiver& operator=(MulticastReceiver&&) = delete;
111  defs::connection_t MulticastConnection() const;
116  std::string InterfaceAddress() const;
118  void CloseSocket();
119 
120 private:
125  void CreateMulticastSocket(size_t receiveBufferSize);
127  void StartAsyncRead();
133  void ReadComplete(const boost_sys::error_code& error, size_t bytesReceived);
138  void SetClosing(bool closing);
143  bool IsClosing() const;
145  void ProcessCloseSocket();
146 
147 private:
149  mutable std::mutex m_closingMutex{};
151  threads::SyncEvent m_closedEvent{};
153  bool m_closing{false};
155  std::unique_ptr<IoContextThreadGroup> m_ioThreadGroup{};
157  boost_iocontext_t::strand m_strand;
159  defs::connection_t m_multicastConnection{};
161  std::string m_interfaceAddress{};
163  defs::check_bytes_left_to_read_t m_checkBytesLeftToRead{};
165  defs::message_received_handler_t m_messageReceivedHandler{};
167  defs::char_buffer_t m_receiveBuffer{};
169  defs::char_buffer_t m_messageBuffer{};
171  boost_udp_t::endpoint m_senderEndpoint{};
173  boost_udp_t::socket m_socket;
174 };
175 
176 } // namespace udp
177 } // namespace asio
178 } // namespace core_lib
179 
180 #endif // MULTICASTRECEIVER
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.
boost_udp_t::socket m_socket
The multicast socket.
Definition: MulticastReceiver.h:173
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
boost_iocontext_t::strand m_strand
I/O context strand.
Definition: MulticastReceiver.h:157
Class defining a thread synchronisation event.
Definition: SyncEvent.h:70
The core_lib namespace.
Definition: AsioDefines.h:59
A general purpose multicast receiver.
Definition: MulticastReceiver.h:49
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
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