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.
Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
CSocket Class Reference

#include <UDPSocket.h>

+ Inheritance diagram for CSocket:

Public Types

enum  SocketType { TcpSocket = SOCK_STREAM, UdpSocket = SOCK_DGRAM, UnknownSocketType =-1 }
 
enum  NetworkLayerProtocol { IPv4Protocol = AF_INET, IPv6Protocol = AF_INET6, UnknownNetworkLayerProtocol = -1 }
 
enum  ReadResult { DATA_ARRIVED = 0, DATA_TIMED_OUT = ETIMEDOUT, DATA_EXCEPTION = 255 }
 

Public Member Functions

virtual ~CSocket ()
 
string GetLocalAddress ()
 
unsigned short GetLocalPort ()
 
void BindLocalPort (unsigned short localPort)
 
void BindLocalAddressAndPort (const string &localAddress, unsigned short localPort=0)
 
unsigned long int GetReadBufferSize ()
 
void SetReadBufferSize (unsigned int nSize)
 
void SetNonBlocking (bool bBlocking)
 
void ConnectToHost (const string &foreignAddress, unsigned short foreignPort)
 
void Send (const void *buffer, int bufferLen)
 
int Recv (void *buffer, int bufferLen)
 
string GetPeerAddress ()
 
unsigned short GetPeerPort ()
 
CSocketoperator<< (const string &sStr)
 
CSocketoperator>> (string &sStr)
 
virtual int OnDataRead (unsigned long timeToWait=ULONG_MAX)
 
void SetBindToDevice (const string &sInterface)
 

Protected Member Functions

 CSocket (SocketType type, NetworkLayerProtocol protocol)
 
 CSocket (int sockDesc)
 

Static Protected Member Functions

static void FillAddr (const string &localAddress, unsigned short localPort, sockaddr_in &localAddr)
 

Protected Attributes

int m_sockDesc
 

Private Member Functions

 CSocket (const CSocket &sock)
 
void operator= (const CSocket &sock)
 

Detailed Description

Base class representing basic communication endpoint.

Definition at line 77 of file UDPSocket.h.

Member Enumeration Documentation

◆ NetworkLayerProtocol

Enum to represent type network layer protocol used for socket

Enumerator
IPv4Protocol 
IPv6Protocol 
UnknownNetworkLayerProtocol 

Definition at line 95 of file UDPSocket.h.

◆ ReadResult

Enum to represent Wait Result when reading data from a socket

Enumerator
DATA_ARRIVED 
DATA_TIMED_OUT 
DATA_EXCEPTION 

Definition at line 105 of file UDPSocket.h.

◆ SocketType

Enum to represent type of socket(UDP or TCP)

Enumerator
TcpSocket 
UdpSocket 
UnknownSocketType 

Definition at line 85 of file UDPSocket.h.

86  {
87  TcpSocket = SOCK_STREAM,
88  UdpSocket = SOCK_DGRAM,
90  };

Constructor & Destructor Documentation

◆ ~CSocket()

CSocket::~CSocket ( )
virtual

Definition at line 47 of file UDPSocket.cpp.

48 {
49  ::close(m_sockDesc);
50  m_sockDesc = -1;
51 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ CSocket() [1/3]

CSocket::CSocket ( SocketType  type,
NetworkLayerProtocol  protocol 
)
protected

Definition at line 53 of file UDPSocket.cpp.

References m_sockDesc.

53  :m_sockDesc(-1)
54 {
55  m_sockDesc = socket(protocol, type, 0);
56  if (m_sockDesc < 0)
57  {
58  throw CSocketException("Socket creation failed (socket())", true);
59  }
60 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ CSocket() [2/3]

CSocket::CSocket ( int  sockDesc)
protected

Definition at line 62 of file UDPSocket.cpp.

References m_sockDesc.

63 {
64  m_sockDesc = sockDesc;
65 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ CSocket() [3/3]

CSocket::CSocket ( const CSocket sock)
private

Definition at line 67 of file UDPSocket.cpp.

References m_sockDesc.

68 {
69  m_sockDesc = sock.m_sockDesc;
70 }
int m_sockDesc
Definition: UDPSocket.h:235

Member Function Documentation

◆ BindLocalAddressAndPort()

void CSocket::BindLocalAddressAndPort ( const string &  localAddress,
unsigned short  localPort = 0 
)

Set the local port to the specified port and the local address to the specified address. If you omit the port, a random port will be selected.

Parameters
localAddresslocal address
localPortlocal port
Exceptions
CSocketExceptionthrown if setting local port or address fails

Definition at line 113 of file UDPSocket.cpp.

References FillAddr(), and m_sockDesc.

Referenced by UDPSocket::UDPSocket().

114 {
115  // Get the address of the requested host
116  sockaddr_in localAddr;
117  FillAddr(localAddress, localPort, localAddr);
118 
119  if (bind(m_sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
120  throw CSocketException("Set of local address and port failed (bind())", true);
121  }
122 }
static void FillAddr(const string &localAddress, unsigned short localPort, sockaddr_in &localAddr)
Definition: UDPSocket.cpp:124
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ BindLocalPort()

void CSocket::BindLocalPort ( unsigned short  localPort)

Set the local port to the specified port and the local address to any interface

Parameters
localPortlocal port
Exceptions
CSocketExceptionthrown if setting local port fails

Definition at line 99 of file UDPSocket.cpp.

References m_sockDesc.

Referenced by UDPSocket::UDPSocket().

100 {
101  // Bind the socket to its port
102  sockaddr_in localAddr;
103  memset(&localAddr, 0, sizeof(localAddr));
104  localAddr.sin_family = AF_INET;
105  localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
106  localAddr.sin_port = htons(localPort);
107 
108  if (bind(m_sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
109  throw CSocketException("Set of local port failed (bind())", true);
110  }
111 }
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the caller graph for this function:

◆ ConnectToHost()

void CSocket::ConnectToHost ( const string &  foreignAddress,
unsigned short  foreignPort 
)

Establish a socket connection with the given foreign address and port

Parameters
foreignAddressforeign address (IP address or name)
foreignPortforeign port
Exceptions
SocketExceptionthrown if unable to establish connection

Definition at line 176 of file UDPSocket.cpp.

References FillAddr(), and m_sockDesc.

177 {
178  // Get the address of the requested host
179  sockaddr_in destAddr;
180  FillAddr(foreignAddress, foreignPort, destAddr);
181 
182  // Try to connect to the given port
183  if (::connect(m_sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
184  throw CSocketException("Connect failed (connect())", true);
185  }
186 }
static void FillAddr(const string &localAddress, unsigned short localPort, sockaddr_in &localAddr)
Definition: UDPSocket.cpp:124
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the call graph for this function:

◆ FillAddr()

void CSocket::FillAddr ( const string &  localAddress,
unsigned short  localPort,
sockaddr_in &  localAddr 
)
staticprotected

Definition at line 124 of file UDPSocket.cpp.

Referenced by BindLocalAddressAndPort(), ConnectToHost(), and UDPSocket::SendDataGram().

125 {
126  memset(&localAddr, 0, sizeof(localAddr)); // Zero out address structure
127  localAddr.sin_family = AF_INET; // Internet address
128 
129  hostent *host; // Resolve name
130  if ((host = gethostbyname(localAddress.c_str())) == NULL) {
131  // strerror() will not work for gethostbyname() and hstrerror()
132  // is supposedly obsolete
133  throw CSocketException("Failed to resolve name (gethostbyname())");
134  }
135  localAddr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
136 
137  localAddr.sin_port = htons(localPort); // Assign port in network byte order
138 }
+ Here is the caller graph for this function:

◆ GetLocalAddress()

std::string CSocket::GetLocalAddress ( )

Get the local address

Returns
local address of socket
Exceptions
CSocketExceptionthrown if fetch fails

Definition at line 77 of file UDPSocket.cpp.

References m_sockDesc.

78 {
79  sockaddr_in addr;
80  unsigned int addr_len = sizeof(addr);
81 
82  if (getsockname(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
83  throw CSocketException("Fetch of local address failed (getsockname())", true);
84  }
85  return inet_ntoa(addr.sin_addr);
86 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ GetLocalPort()

unsigned short CSocket::GetLocalPort ( )

Get the local port

Returns
local port of socket
Exceptions
CSocketExceptionthrown if fetch fails

Definition at line 88 of file UDPSocket.cpp.

References m_sockDesc.

89 {
90  sockaddr_in addr;
91  unsigned int addr_len = sizeof(addr);
92 
93  if (getsockname(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
94  throw CSocketException("Fetch of local port failed (getsockname())", true);
95  }
96  return ntohs(addr.sin_port);
97 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ GetPeerAddress()

std::string CSocket::GetPeerAddress ( )

Get the foreign address. Call connect() before calling recv()

Returns
foreign address
Exceptions
SocketExceptionthrown if unable to fetch foreign address

Definition at line 206 of file UDPSocket.cpp.

References m_sockDesc.

207 {
208  sockaddr_in addr;
209  unsigned int addr_len = sizeof(addr);
210 
211  if (getpeername(m_sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) {
212  throw CSocketException("Fetch of foreign address failed (getpeername())", true);
213  }
214  return inet_ntoa(addr.sin_addr);
215 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ GetPeerPort()

unsigned short CSocket::GetPeerPort ( )

Get the foreign port. Call connect() before calling recv()

Returns
foreign port
Exceptions
SocketExceptionthrown if unable to fetch foreign port

Definition at line 217 of file UDPSocket.cpp.

References m_sockDesc.

218 {
219  sockaddr_in addr;
220  unsigned int addr_len = sizeof(addr);
221 
222  if (getpeername(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
223  throw CSocketException("Fetch of foreign port failed (getpeername())", true);
224  }
225  return ntohs(addr.sin_port);
226 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ GetReadBufferSize()

unsigned long int CSocket::GetReadBufferSize ( )

Returns the size of the internal read buffer. This limits the amount of data that the client can receive before you call

Definition at line 140 of file UDPSocket.cpp.

References m_sockDesc.

Referenced by operator>>().

141 {
142  unsigned long int nSize;
143  socklen_t n = sizeof(nSize);
144  getsockopt(m_sockDesc,SOL_SOCKET,SO_RCVBUF,(void *)&nSize, (&n));
145  // now the variable nSize will have the socket size
146  return nSize;
147 }
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the caller graph for this function:

◆ OnDataRead()

int CSocket::OnDataRead ( unsigned long  timeToWait = ULONG_MAX)
virtual

Blocking function to check whether data arrived in socket for reading.

Parameters
timeToWaitwaits for 'timeToWait' seconds.

Definition at line 243 of file UDPSocket.cpp.

References DATA_ARRIVED, DATA_EXCEPTION, DATA_TIMED_OUT, and m_sockDesc.

244 {
245  /* master file descriptor list */
246  fd_set master;
247 
248  /* temp file descriptor list for select() */
249  fd_set read_fds;
250 
251  /* maximum file descriptor number */
252  int fdmax;
253 
254  /* clear the master and temp sets */
255  FD_ZERO(&master);
256  FD_ZERO(&read_fds);
257 
258  /* add the listener to the master set */
259  FD_SET(m_sockDesc, &master);
260  /* keep track of the biggest file descriptor */
261  fdmax = m_sockDesc; /* so far, it's this one*/
262 
263  /* copy it */
264  read_fds = master;
265  int nRet;
266 
267  if (timeToWait == ULONG_MAX)
268  {
269  nRet = select(fdmax+1, &read_fds, NULL, NULL, NULL);
270  if (nRet == -1)
271  nRet = DATA_EXCEPTION;
272  else if (nRet > 0)
273  nRet = DATA_ARRIVED;
274  }
275  else
276  {
277  struct timeval timeout;
278  timeout.tv_sec = timeToWait;
279  timeout.tv_usec = 0;
280  nRet = select(fdmax+1, &read_fds, NULL, NULL, &timeout);
281  if (nRet == -1)
282  nRet = DATA_EXCEPTION;
283  else if (nRet > 0)
284  nRet = DATA_ARRIVED;
285  else if(nRet == 0)
286  nRet = DATA_TIMED_OUT;
287  }
288 
289  return nRet;
290 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ operator<<()

CSocket & CSocket::operator<< ( const string &  sStr)

Writing sStr to socket

Definition at line 228 of file UDPSocket.cpp.

References Send().

229 {
230  Send(sStr.c_str(), sStr.length());
231  return *this;
232 }
void Send(const void *buffer, int bufferLen)
Definition: UDPSocket.cpp:188
+ Here is the call graph for this function:

◆ operator=()

void CSocket::operator= ( const CSocket sock)
private

Definition at line 72 of file UDPSocket.cpp.

References m_sockDesc.

73 {
74  m_sockDesc = sock.m_sockDesc;
75 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ operator>>()

CSocket & CSocket::operator>> ( string &  sStr)

Reading data to sStr from socket

Definition at line 234 of file UDPSocket.cpp.

References GetReadBufferSize(), and Recv().

235 {
236  char *buff = new char[GetReadBufferSize()];
237  Recv(buff, GetReadBufferSize());
238  sStr.append(buff);
239  delete [] buff;
240  return *this;
241 }
unsigned long int GetReadBufferSize()
Definition: UDPSocket.cpp:140
int Recv(void *buffer, int bufferLen)
Definition: UDPSocket.cpp:195
+ Here is the call graph for this function:

◆ Recv()

int CSocket::Recv ( void *  buffer,
int  bufferLen 
)

Read into the given buffer up to bufferLen bytes data from this socket. Call connect() before calling recv()

Parameters
bufferbuffer to receive the data
bufferLenmaximum number of bytes to read into buffer
Returns
number of bytes read, 0 for EOF, and -1 for error
Exceptions
SocketExceptionthrown if unable to receive data

Definition at line 195 of file UDPSocket.cpp.

References m_sockDesc.

Referenced by operator>>().

196 {
197  int nBytes;
198  if ((nBytes = ::recv(m_sockDesc, (void *) buffer, bufferLen, 0)) < 0) {
199  throw CSocketException("Received failed (recv())", true);
200  }
201  char* sData = static_cast<char *>(buffer);
202  sData[nBytes] = '\0';
203  return nBytes;
204 }
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the caller graph for this function:

◆ Send()

void CSocket::Send ( const void *  buffer,
int  bufferLen 
)

Write the given buffer to this socket. Call connect() before calling send()

Parameters
bufferbuffer to be written
bufferLennumber of bytes from buffer to be written
Exceptions
SocketExceptionthrown if unable to send data

Definition at line 188 of file UDPSocket.cpp.

References m_sockDesc.

Referenced by operator<<().

189 {
190  if (::send(m_sockDesc, (void *) buffer, bufferLen, 0) < 0) {
191  throw CSocketException("Send failed (send())", true);
192  }
193 }
int m_sockDesc
Definition: UDPSocket.h:235
+ Here is the caller graph for this function:

◆ SetBindToDevice()

void CSocket::SetBindToDevice ( const string &  sInterface)

To Bind socket to a symbolic device name like eth0

Parameters
sInterfaceNIC device name

Definition at line 292 of file UDPSocket.cpp.

293 {
294  struct ifreq ifr;
295  memset(&ifr, 0, sizeof(ifr));
296  snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", sInterface.c_str());
297 }

◆ SetNonBlocking()

void CSocket::SetNonBlocking ( bool  bBlocking)

Sets the socket to Blocking/Non blocking state.

Parameters
Boolflag for Non blocking status.

Definition at line 157 of file UDPSocket.cpp.

References m_sockDesc.

158 {
159  int opts;
160 
161  opts = fcntl ( m_sockDesc, F_GETFL );
162 
163  if ( opts < 0 )
164  {
165  return;
166  }
167 
168  if ( bBlocking )
169  opts = ( opts | O_NONBLOCK );
170  else
171  opts = ( opts & ~O_NONBLOCK );
172 
173  fcntl ( m_sockDesc, F_SETFL,opts );
174 }
int m_sockDesc
Definition: UDPSocket.h:235

◆ SetReadBufferSize()

void CSocket::SetReadBufferSize ( unsigned int  nSize)

Sets the read buffer size of the socket.

Parameters
Sizeof the buffer.

Definition at line 149 of file UDPSocket.cpp.

References m_sockDesc.

150 {
151  if (setsockopt(m_sockDesc, SOL_SOCKET, SO_RCVBUF, &nSize, sizeof(nSize)) == -1)
152  {
153  throw CSocketException("Error in setting socket buffer size ", true);
154  }
155 }
int m_sockDesc
Definition: UDPSocket.h:235

Member Data Documentation

◆ m_sockDesc

int CSocket::m_sockDesc
protected

The documentation for this class was generated from the following files: