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;
76 MulticastTypedSender(boost_iocontext_t& ioContext,
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}
82 , m_multicastSender{
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}
106 , m_multicastSender{
107 multicastConnection, interfaceAddress, enableLoopback, ttl, sendBufferSize}
108 {
109 }
111 MulticastTypedSender(const MulticastTypedSender&) = delete;
113 MulticastTypedSender& operator=(const MulticastTypedSender&) = delete;
115 MulticastTypedSender(MulticastTypedSender&&) = delete;
117 MulticastTypedSender& operator=(MulticastTypedSender&&) = delete;
119 ~MulticastTypedSender() = default;
124 defs::connection_t MulticastConnection() const
125 {
126 return m_multicastSender.MulticastConnection();
127 }
132 std::string InterfaceAddress() const
133 {
134 return m_multicastSender.InterfaceAddress();
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);
163 return m_multicastSender.SendMessage(
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);
181 return m_multicastSender.SendMessage(
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;
200 MulticastSender m_multicastSender;
201 };
202
203 } // namespace udp
204 } // namespace asio
205 } // namespace core_lib
206
207 #endif // MULTICASTTYPEDSENDER
core_lib::asio::udp::MulticastSender::MulticastConnection
defs::connection_t MulticastConnection() const
Retrieve multicast connection details.
Definition: MulticastSender.cpp:72
MulticastSender.h
File containing multicast sender class declaration.
core_lib::asio::udp::MulticastTypedSender::SendMessage
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
core_lib::asio::udp::MulticastTypedSender::InterfaceAddress
std::string InterfaceAddress() const
Retrieve interface IP address.
Definition: MulticastTypedSender.h:132
core_lib::asio::udp::eMulticastTTL::sameSubnet
Multicast only to same subnet.
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::udp::MulticastTypedSender::MulticastTypedSender
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
core_lib::asio::udp::MulticastSender::SendMessage
bool SendMessage(const defs::char_buffer_t &message)
Send a message buffer to the receiver.
Definition: MulticastSender.cpp:82
core_lib::asio::udp::eMulticastTTL
eMulticastTTL
The multicast TTL enumeration.
Definition: AsioDefines.h:129
core_lib::asio::udp::MulticastSender
A general purpose multicast sender.
Definition: MulticastSender.h:47
core_lib::asio::udp::MulticastSender::InterfaceAddress
std::string InterfaceAddress() const
Retrieve interface IP address.
Definition: MulticastSender.cpp:77
core_lib::asio::defs::connection_t
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
core_lib::asio::udp::MulticastTypedSender::operator=
MulticastTypedSender & operator=(const MulticastTypedSender &)=delete
Copy assignment operator - deleted.
core_lib::asio::udp::MulticastTypedSender::MulticastConnection
defs::connection_t MulticastConnection() const
Retrieve multicast connection details.
Definition: MulticastTypedSender.h:124
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
core_lib::asio::udp::MulticastTypedSender
A generic UDP sender.
Definition: MulticastTypedSender.h:55
core_lib::asio::udp::MulticastTypedSender::m_sendMutex
std::mutex m_sendMutex
Send message mutex.
Definition: MulticastTypedSender.h:196
core_lib::asio::udp::MulticastTypedSender::SendMessage
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
MessageUtils.h
File containing message utils declaration.
core_lib::asio::udp::MulticastTypedSender::SendMessage
bool SendMessage(const defs::char_buffer_t &message)
Send a message buffer to the receiver.
Definition: MulticastTypedSender.h:189
core_lib::asio::udp::MulticastTypedSender::MulticastTypedSender
MulticastTypedSender()=delete
Default constructor - deleted.
core_lib::asio::udp::MulticastTypedSender::MulticastTypedSender
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
core_lib::asio::defs::char_buffer_t
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
core_lib::asio::udp::MulticastTypedSender::SendMessage
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
core_lib::asio::udp::MulticastTypedSender::m_multicastSender
MulticastSender m_multicastSender
Underlying UDP sender object.
Definition: MulticastTypedSender.h:200
core_lib::asio::udp::MulticastTypedSender::~MulticastTypedSender
~MulticastTypedSender()=default
Default destructor.
core_lib::asio::udp::MulticastTypedSender::m_messageBuilder
const MsgBldr & m_messageBuilder
Const reference to message builder object.
Definition: MulticastTypedSender.h:198
core_lib::asio::defs::NULL_CONNECTION
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).