libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
UdpSocket.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef UDPSOCKETOPERATIONS_H_
32 #define UDPSOCKETOPERATIONS_H_
33 
34 #include "SocketExceptions.h"
35 #include "../Managers/FileDescriptorManager.h"
36 
37 namespace BaseLib {
38 
39 class SharedObjects;
40 
41 class UdpSocket {
42  public:
44  UdpSocket(BaseLib::SharedObjects *baseLib, std::string listenPort);
45  UdpSocket(BaseLib::SharedObjects *baseLib, std::string hostname, std::string port);
46  UdpSocket(BaseLib::SharedObjects *baseLib, std::string hostname, std::string port, std::string listenPort);
47  virtual ~UdpSocket();
48 
49  void setReadTimeout(int64_t timeout) { _readTimeout = timeout; }
51  void setHostname(std::string hostname) {
52  close();
53  _hostname = hostname;
54  }
55  void setPort(std::string port) {
56  close();
57  _port = port;
58  }
59  std::string getClientIp() { return _clientIp; }
60  std::string getListenIp() { return _listenIp; }
61  uint16_t getListenPort() { return _listenPort; }
62 
63  bool isOpen();
64 
76  int32_t proofread(char *buffer, int32_t bufferSize, std::string &senderIp);
77 
78  int32_t proofwrite(const std::shared_ptr<std::vector<char>>& data);
79  int32_t proofwrite(const std::vector<char> &data);
80  int32_t proofwrite(const std::string &data);
81  int32_t proofwrite(const char *buffer, int32_t bytesToWrite);
82  void open();
83  void close();
84  protected:
86  int64_t _readTimeout = 15000000;
87  bool _autoConnect = true;
88  std::string _hostname;
89  std::string _clientIp;
90  std::string _port;
91  std::string _listenIp;
92  uint16_t _listenPort = 0;
93  struct addrinfo *_serverInfo = nullptr;
94  std::mutex _readMutex;
95  std::mutex _writeMutex;
96 
97  std::shared_ptr<FileDescriptor> _socketDescriptor;
98 
99  void getSocketDescriptor();
100  void getConnection();
101  void autoConnect();
102 };
103 
104 typedef std::shared_ptr<BaseLib::UdpSocket> PUdpSocket;
105 
106 }
107 
108 #endif
int64_t _readTimeout
Definition: UdpSocket.h:86
std::string _clientIp
Definition: UdpSocket.h:89
void getConnection()
Definition: UdpSocket.cpp:262
void getSocketDescriptor()
Definition: UdpSocket.cpp:238
uint16_t _listenPort
Definition: UdpSocket.h:92
std::string getListenIp()
Definition: UdpSocket.h:60
void setHostname(std::string hostname)
Definition: UdpSocket.h:51
struct addrinfo * _serverInfo
Definition: UdpSocket.h:93
This is the base library main class.
Definition: BaseLib.h:95
bool _autoConnect
Definition: UdpSocket.h:87
BaseLib::SharedObjects * _bl
Definition: UdpSocket.h:85
virtual ~UdpSocket()
Definition: UdpSocket.cpp:57
void setPort(std::string port)
Definition: UdpSocket.h:55
void close()
Definition: UdpSocket.cpp:72
uint16_t getListenPort()
Definition: UdpSocket.h:61
int32_t proofread(char *buffer, int32_t bufferSize, std::string &senderIp)
Reads bytes from UDP socket into "buffer".
Definition: UdpSocket.cpp:84
Definition: BaseLib.cpp:34
std::string getClientIp()
Definition: UdpSocket.h:59
int32_t proofwrite(const std::shared_ptr< std::vector< char >> &data)
Definition: UdpSocket.cpp:138
Definition: UdpSocket.h:41
void autoConnect()
Definition: UdpSocket.cpp:66
std::string _port
Definition: UdpSocket.h:90
std::shared_ptr< BaseLib::UdpSocket > PUdpSocket
Definition: UdpSocket.h:104
std::shared_ptr< FileDescriptor > _socketDescriptor
Definition: UdpSocket.h:97
UdpSocket(BaseLib::SharedObjects *baseLib)
Definition: UdpSocket.cpp:36
std::string _listenIp
Definition: UdpSocket.h:91
void setReadTimeout(int64_t timeout)
Definition: UdpSocket.h:49
void setAutoConnect(bool autoConnect)
Definition: UdpSocket.h:50
std::mutex _readMutex
Definition: UdpSocket.h:94
void open()
Definition: UdpSocket.cpp:61
std::string _hostname
Definition: UdpSocket.h:88
bool isOpen()
Definition: UdpSocket.cpp:233
std::mutex _writeMutex
Definition: UdpSocket.h:95