Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

UdpTypedSender.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 UDPTYPEDSENDER
28 #define UDPTYPEDSENDER
29
30 #include <mutex>
31 #include "UdpSender.h"
32 #include "MessageUtils.h"
33
35 namespace core_lib
36 {
38 namespace asio
39 {
41 namespace udp
42 {
55 template <typename MsgBldr> class UdpTypedSender final
56 {
57 public:
59 UdpTypedSender() = delete;
73 UdpTypedSender(boost_iocontext_t& ioContext, const defs::connection_t& receiver,
74 const MsgBldr& messageBuilder, eUdpOption sendOption = eUdpOption::broadcast,
75 size_t sendBufferSize = DEFAULT_UDP_BUF_SIZE)
76 : m_messageBuilder{messageBuilder}
77 , m_udpSender{ioContext, receiver, sendOption, sendBufferSize}
78 {
79 }
92 UdpTypedSender(const defs::connection_t& receiver, const MsgBldr& messageBuilder,
93 eUdpOption sendOption = eUdpOption::broadcast,
94 size_t sendBufferSize = DEFAULT_UDP_BUF_SIZE)
95
96 : m_messageBuilder{messageBuilder}
97 , m_udpSender{receiver, sendOption, sendBufferSize}
98 {
99 }
101 UdpTypedSender(const UdpTypedSender&) = delete;
103 UdpTypedSender& operator=(const UdpTypedSender&) = delete;
105 UdpTypedSender(UdpTypedSender&&) = delete;
107 UdpTypedSender& operator=(UdpTypedSender&&) = delete;
109 ~UdpTypedSender() = default;
114 defs::connection_t ReceiverConnection() const
115 {
116 return m_udpSender.ReceiverConnection();
117 }
126 bool SendMessage(int32_t messageId,
127 const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
128 {
129 std::lock_guard<std::mutex> lock(m_sendMutex);
130 return m_udpSender.SendMessage(m_messageBuilder.Build(messageId, responseAddress));
131 }
141 bool SendMessage(const defs::char_buffer_t& message, int32_t messageId,
142 const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
143 {
144 std::lock_guard<std::mutex> lock(m_sendMutex);
145 return m_udpSender.SendMessage(m_messageBuilder.Build(message, messageId, responseAddress));
146 }
157 template <typename T, class A = serialize::archives::out_port_bin_t>
158 bool SendMessage(const T& message, int32_t messageId,
159 const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
160 {
161 std::lock_guard<std::mutex> lock(m_sendMutex);
162 return m_udpSender.SendMessage(
163 m_messageBuilder.template Build<T, A>(message, messageId, responseAddress));
164 }
170 bool SendMessage(const defs::char_buffer_t& message)
171 {
172 return m_udpSender.SendMessage(message);
173 }
174
175 private:
177 mutable std::mutex m_sendMutex;
179 const MsgBldr& m_messageBuilder;
181 UdpSender m_udpSender;
182 };
183
184 } // namespace udp
185 } // namespace asio
186 } // namespace core_lib
187
188 #endif // UDPTYPEDSENDER
core_lib::asio::udp::UdpTypedSender::UdpTypedSender
UdpTypedSender()=delete
Default constructor - deleted.
core_lib::asio::udp::UdpTypedSender::operator=
UdpTypedSender & operator=(const UdpTypedSender &)=delete
Copy assignment operator - deleted.
core_lib::asio::udp::eUdpOption
eUdpOption
The udp options enumeration.
Definition: AsioDefines.h:98
core_lib::asio::udp::UdpTypedSender::ReceiverConnection
defs::connection_t ReceiverConnection() const
Retrieve receiver connection details.
Definition: UdpTypedSender.h:114
core_lib::asio::udp::UdpTypedSender::~UdpTypedSender
~UdpTypedSender()=default
Default destructor.
core_lib::asio::udp::UdpTypedSender::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: UdpTypedSender.h:158
core_lib::asio::udp::UdpTypedSender::UdpTypedSender
UdpTypedSender(const defs::connection_t &receiver, const MsgBldr &messageBuilder, eUdpOption sendOption=eUdpOption::broadcast, size_t sendBufferSize=DEFAULT_UDP_BUF_SIZE)
Initialisation constructor.
Definition: UdpTypedSender.h:92
core_lib::asio::udp::UdpSender::SendMessage
bool SendMessage(const defs::char_buffer_t &message)
Send a message buffer to the receiver.
Definition: UdpSender.cpp:68
core_lib::asio::udp::UdpSender::ReceiverConnection
defs::connection_t ReceiverConnection() const
Retrieve receiver connection details.
Definition: UdpSender.cpp:63
core_lib::asio::udp::UdpTypedSender::m_messageBuilder
const MsgBldr & m_messageBuilder
Const reference to message builder object.
Definition: UdpTypedSender.h:179
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::udp::UdpTypedSender::UdpTypedSender
UdpTypedSender(boost_iocontext_t &ioContext, const defs::connection_t &receiver, const MsgBldr &messageBuilder, eUdpOption sendOption=eUdpOption::broadcast, size_t sendBufferSize=DEFAULT_UDP_BUF_SIZE)
Initialisation constructor.
Definition: UdpTypedSender.h:73
core_lib::asio::udp::UdpTypedSender::SendMessage
bool SendMessage(int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a header-only message to the receiver.
Definition: UdpTypedSender.h:126
core_lib::asio::udp::UdpTypedSender::SendMessage
bool SendMessage(const defs::char_buffer_t &message)
Send a message buffer to the receiver.
Definition: UdpTypedSender.h:170
core_lib::asio::udp::UdpTypedSender::m_udpSender
UdpSender m_udpSender
Underlying UDP sender object.
Definition: UdpTypedSender.h:181
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::UdpTypedSender
A generic UDP sender.
Definition: UdpTypedSender.h:55
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
core_lib::asio::udp::UdpTypedSender::m_sendMutex
std::mutex m_sendMutex
Send message mutex.
Definition: UdpTypedSender.h:177
MessageUtils.h
File containing message utils declaration.
core_lib::asio::udp::eUdpOption::broadcast
udp broadcasts.
UdpSender.h
File containing UDP sender class declaration.
core_lib::asio::udp::UdpSender
A general purpose UDP sender.
Definition: UdpSender.h:47
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::UdpTypedSender::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: UdpTypedSender.h:141
core_lib::asio::defs::NULL_CONNECTION
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).