SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
UDPSocket.h
Go to the documentation of this file.
1 // Remote - send I/Q samples read from a SDR device over the network via UDP. //
3 // //
4 // Copyright (C) 2015 Edouard Griffiths, F4EXB //
5 // //
6 // This program is free software; you can redistribute it and/or modify //
7 // it under the terms of the GNU General Public License as published by //
8 // the Free Software Foundation as version 3 of the License, or //
9 // (at your option) any later version. //
10 // //
11 // This program is distributed in the hope that it will be useful, //
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
14 // GNU General Public License V3 for more details. //
15 // //
16 // You should have received a copy of the GNU General Public License //
17 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
19 
20 // Original code is posted at: https://cppcodetips.wordpress.com/2014/01/29/udp-socket-class-in-c/
21 
22 #ifndef INCLUDE_UDPSOCKET_H_
23 #define INCLUDE_UDPSOCKET_H_
24 
25 #include <cstring> // For string
26 #include <exception> // For exception class
27 #include <string>
28 #include <sys/types.h> // For data types
29 #include <sys/socket.h> // For socket(), connect(), send(), and recv()
30 #include <netdb.h> // For gethostbyname()
31 #include <arpa/inet.h> // For inet_addr()
32 #include <unistd.h> // For close()
33 #include <netinet/in.h> // For sockaddr_in
34 #include <errno.h>
35 #include <climits>
36 
37 using namespace std;
38 
43 class CSocketException: public std::exception
44 {
45 public:
52  CSocketException(const string &message, bool bSysMsg = false) throw();
53 
54 
58  virtual ~CSocketException() throw ();
59 
65  virtual const char* what() const throw (){ return m_sMsg.c_str(); }
66 
67 protected:
70  std::string m_sMsg;
71 };
72 
77 class CSocket
78 {
79 public:
80  virtual ~CSocket();
81 
86  {
87  TcpSocket = SOCK_STREAM,
88  UdpSocket = SOCK_DGRAM,
89  UnknownSocketType =-1
90  };
91 
96  {
97  IPv4Protocol = AF_INET,
98  IPv6Protocol = AF_INET6,
99  UnknownNetworkLayerProtocol = -1
100  };
101 
106  {
107  DATA_ARRIVED = 0,
108  DATA_TIMED_OUT = ETIMEDOUT,
109  DATA_EXCEPTION = 255
110  };
111 
118  string GetLocalAddress();
119 
126  unsigned short GetLocalPort();
127 
128 
136  void BindLocalPort(unsigned short localPort);
137 
147  void BindLocalAddressAndPort(const string &localAddress, unsigned short localPort = 0);
148 
153  unsigned long int GetReadBufferSize ();
154 
159  void SetReadBufferSize(unsigned int nSize);
160 
165  void SetNonBlocking(bool bBlocking);
166 
174  void ConnectToHost(const string &foreignAddress, unsigned short foreignPort);
175 
183  void Send(const void *buffer, int bufferLen);
184 
193  int Recv(void *buffer, int bufferLen);
194 
200  string GetPeerAddress();
201 
207  unsigned short GetPeerPort();
208 
212  CSocket& operator<<(const string& sStr );
213 
217  CSocket& operator>>(string& sStr);
218 
223  virtual int OnDataRead(unsigned long timeToWait = ULONG_MAX);
224 
229  void SetBindToDevice(const string& sInterface);
230 
231 protected:
236 
237  CSocket(SocketType type, NetworkLayerProtocol protocol);
238  CSocket(int sockDesc);
239  static void FillAddr( const string & localAddress, unsigned short localPort, sockaddr_in& localAddr );
240 
241 private:
242  // Prevent the user from trying to use Exact copy of this object
243  CSocket(const CSocket &sock);
244  void operator=(const CSocket &sock);
245 };
246 
251 class UDPSocket : public CSocket
252 {
253 public:
258  UDPSocket();
264  UDPSocket(unsigned short localPort);
265 
272  UDPSocket(const string &localAddress, unsigned short localPort);
273 
285  void DisconnectFromHost();
286 
297  void SendDataGram(const void *buffer, int bufferLen, const string &foreignAddress,
298  unsigned short foreignPort);
299 
310  int RecvDataGram(void *buffer, int bufferLen, string &sourceAddress,
311  unsigned short &sourcePort);
312 
318  void SetMulticastTTL(unsigned char multicastTTL);
319 
325  void JoinGroup(const string &multicastGroup);
326 
332  void LeaveGroup(const string &multicastGroup);
333 
334 private:
335  void SetBroadcast();
336 
337 };
338 
339 
340 #endif /* INCLUDE_UDPSOCKET_H_ */
NetworkLayerProtocol
Definition: UDPSocket.h:95
virtual const char * what() const
Definition: UDPSocket.h:65
QDataStream & operator>>(QDataStream &ds, AudioDeviceManager::InputDeviceInfo &info)
QDataStream & operator<<(QDataStream &ds, const AudioDeviceManager::InputDeviceInfo &info)
std::string m_sMsg
Definition: UDPSocket.h:70
SocketType
Definition: UDPSocket.h:85
int m_sockDesc
Definition: UDPSocket.h:235