Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
MulticastTypedSender.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 MULTICASTTYPEDSENDER
28 #define MULTICASTTYPEDSENDER
29 
30 #include <mutex>
31 #include "MulticastSender.h"
32 #include "MessageUtils.h"
33 
35 namespace core_lib
36 {
38 namespace asio
39 {
41 namespace udp
42 {
55 template <typename MsgBldr> class MulticastTypedSender final
56 {
57 public:
59  MulticastTypedSender() = delete;
77  const defs::connection_t& multicastConnection,
78  const MsgBldr& messageBuilder, const std::string& interfaceAddress = "",
79  bool enableLoopback = true, eMulticastTTL ttl = eMulticastTTL::sameSubnet,
80  size_t sendBufferSize = DEFAULT_UDP_BUF_SIZE)
81  : m_messageBuilder{messageBuilder}
83  ioContext, multicastConnection, interfaceAddress, enableLoopback, ttl, sendBufferSize}
84  {
85  }
101  MulticastTypedSender(const defs::connection_t& multicastConnection,
102  const MsgBldr& messageBuilder, const std::string& interfaceAddress = "",
103  bool enableLoopback = true, eMulticastTTL ttl = eMulticastTTL::sameSubnet,
104  size_t sendBufferSize = DEFAULT_UDP_BUF_SIZE)
105  : m_messageBuilder{messageBuilder}
107  multicastConnection, interfaceAddress, enableLoopback, ttl, sendBufferSize}
108  {
109  }
119  ~MulticastTypedSender() = default;
125  {
127  }
132  std::string InterfaceAddress() const
133  {
135  }
144  bool SendMessage(int32_t messageId,
145  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
146  {
147  std::lock_guard<std::mutex> lock(m_sendMutex);
148  return m_multicastSender.SendMessage(m_messageBuilder.Build(messageId, responseAddress));
149  }
159  bool SendMessage(const defs::char_buffer_t& message, int32_t messageId,
160  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
161  {
162  std::lock_guard<std::mutex> lock(m_sendMutex);
164  m_messageBuilder.Build(message, messageId, responseAddress));
165  }
176  template <typename T, class A = serialize::archives::out_port_bin_t>
177  bool SendMessage(const T& message, int32_t messageId,
178  const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
179  {
180  std::lock_guard<std::mutex> lock(m_sendMutex);
182  m_messageBuilder.template Build<T, A>(message, messageId, responseAddress));
183  }
189  bool SendMessage(const defs::char_buffer_t& message)
190  {
191  return m_multicastSender.SendMessage(message);
192  }
193 
194 private:
196  mutable std::mutex m_sendMutex;
198  const MsgBldr& m_messageBuilder;
201 };
202 
203 } // namespace udp
204 } // namespace asio
205 } // namespace core_lib
206 
207 #endif // MULTICASTTYPEDSENDER
defs::connection_t MulticastConnection() const
Retrieve multicast connection details.
Definition: MulticastSender.cpp:72
File containing multicast sender class declaration.
bool SendMessage(const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a full message to the server.
Definition: MulticastTypedSender.h:177
std::string InterfaceAddress() const
Retrieve interface IP address.
Definition: MulticastTypedSender.h:132
Multicast only to same subnet.
The core_lib namespace.
Definition: AsioDefines.h:59
MulticastTypedSender(boost_iocontext_t &ioContext, const defs::connection_t &multicastConnection, const MsgBldr &messageBuilder, const std::string &interfaceAddress="", bool enableLoopback=true, eMulticastTTL ttl=eMulticastTTL::sameSubnet, size_t sendBufferSize=DEFAULT_UDP_BUF_SIZE)
Initialisation constructor.
Definition: MulticastTypedSender.h:76
bool SendMessage(const defs::char_buffer_t &message)
Send a message buffer to the receiver.
Definition: MulticastSender.cpp:82
eMulticastTTL
The multicast TTL enumeration.
Definition: AsioDefines.h:129
A general purpose multicast sender.
Definition: MulticastSender.h:47
std::string InterfaceAddress() const
Retrieve interface IP address.
Definition: MulticastSender.cpp:77
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
MulticastTypedSender & operator=(const MulticastTypedSender &)=delete
Copy assignment operator - deleted.
defs::connection_t MulticastConnection() const
Retrieve multicast connection details.
Definition: MulticastTypedSender.h:124
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
A generic UDP sender.
Definition: MulticastTypedSender.h:55
std::mutex m_sendMutex
Send message mutex.
Definition: MulticastTypedSender.h:196
bool SendMessage(const defs::char_buffer_t &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a header plus message buffer to the receiver.
Definition: MulticastTypedSender.h:159
File containing message utils declaration.
bool SendMessage(const defs::char_buffer_t &message)
Send a message buffer to the receiver.
Definition: MulticastTypedSender.h:189
MulticastTypedSender()=delete
Default constructor - deleted.
MulticastTypedSender(const defs::connection_t &multicastConnection, const MsgBldr &messageBuilder, const std::string &interfaceAddress="", bool enableLoopback=true, eMulticastTTL ttl=eMulticastTTL::sameSubnet, size_t sendBufferSize=DEFAULT_UDP_BUF_SIZE)
Initialisation constructor.
Definition: MulticastTypedSender.h:101
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
bool SendMessage(int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a header-only message to the receiver.
Definition: MulticastTypedSender.h:144
MulticastSender m_multicastSender
Underlying UDP sender object.
Definition: MulticastTypedSender.h:200
~MulticastTypedSender()=default
Default destructor.
const MsgBldr & m_messageBuilder
Const reference to message builder object.
Definition: MulticastTypedSender.h:198
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).