libcluon  0.0.148
UDPSender.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017-2018 Christian Berger
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 #ifndef CLUON_UDPSENDER_HPP
10 #define CLUON_UDPSENDER_HPP
11 
12 #include "cluon/cluon.hpp"
13 
14 // clang-format off
15 #ifdef WIN32
16  #include <Winsock2.h> // for WSAStartUp
17  #include <ws2tcpip.h> // for SOCKET
18 #else
19  #include <netinet/in.h>
20 #endif
21 // clang-format on
22 
23 #include <cstdint>
24 #include <mutex>
25 #include <string>
26 #include <utility>
27 
28 namespace cluon {
57  private:
58  UDPSender(const UDPSender &) = delete;
59  UDPSender(UDPSender &&) = delete;
60  UDPSender &operator=(const UDPSender &) = delete;
61  UDPSender &operator=(UDPSender &&) = delete;
62 
63  public:
70  UDPSender(const std::string &sendToAddress, uint16_t sendToPort) noexcept;
71  ~UDPSender() noexcept;
72 
79  std::pair<ssize_t, int32_t> send(std::string &&data) const noexcept;
80 
81  public:
85  uint16_t getSendFromPort() const noexcept;
86 
87  private:
88  mutable std::mutex m_socketMutex{};
89  int32_t m_socket{-1};
90  uint16_t m_portToSentFrom{0};
91  struct sockaddr_in m_sendToAddress {};
92 };
93 } // namespace cluon
94 
95 #endif
Definition: cluon.hpp:65
#define LIBCLUON_API
Definition: cluon.hpp:56
Definition: UDPSender.hpp:56