Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
UdpSender.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 UDPSENDER
28 #define UDPSENDER
29 
30 #include "IoContextThreadGroup.h"
31 
33 namespace core_lib
34 {
36 namespace asio
37 {
39 namespace udp
40 {
41 
47 class CORE_LIBRARY_DLL_SHARED_API UdpSender final
48 {
49 public:
51  UdpSender() = delete;
65  UdpSender(boost_iocontext_t& ioContext, const defs::connection_t& receiver,
66  eUdpOption sendOption = eUdpOption::broadcast,
67  size_t sendBufferSize = DEFAULT_UDP_BUF_SIZE);
79  explicit UdpSender(const defs::connection_t& receiver,
80  eUdpOption sendOption = eUdpOption::broadcast,
81  size_t sendBufferSize = DEFAULT_UDP_BUF_SIZE);
82 
84  UdpSender(const UdpSender&) = delete;
86  UdpSender& operator=(const UdpSender&) = delete;
88  UdpSender(UdpSender&&) = delete;
90  UdpSender& operator=(UdpSender&&) = delete;
92  ~UdpSender() = default;
97  defs::connection_t ReceiverConnection() const;
103  bool SendMessage(const defs::char_buffer_t& message);
104 
105 private:
111  void CreateUdpSocket(eUdpOption sendOption, size_t sendBufferSize);
117  bool SyncSendTo(const defs::char_buffer_t& message);
118 
119 private:
121  std::unique_ptr<IoContextThreadGroup> m_ioThreadGroup{};
123  defs::connection_t m_receiver{};
125  boost_udp_t::endpoint m_receiverEndpoint{};
127  boost_udp_t::resolver m_receiverResolver;
129  boost_udp_t::socket m_socket;
130 };
131 
132 } // namespace udp
133 } // namespace asio
134 } // namespace core_lib
135 
136 #endif // UDPSENDER
boost_udp_t::resolver m_receiverResolver
End-point resolver.
Definition: UdpSender.h:127
eUdpOption
The udp options enumeration.
Definition: AsioDefines.h:98
File containing declarations relating the IoContextThreadGroup class.
boost_udp_t::socket m_socket
UDP socket.
Definition: UdpSender.h:129
The core_lib namespace.
Definition: AsioDefines.h:59
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
A general purpose UDP sender.
Definition: UdpSender.h:47
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239