CPPMyth
Library to interoperate with MythTV server
socket.h
1 /*
2  * Copyright (C) 2014-2015 Jean-Luc Barriere
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 3, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING. If not, write to
16  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 
22 #ifndef SOCKET_H
23 #define SOCKET_H
24 
25 #include <cppmyth_config.h>
26 #include "os/os.h"
27 
28 #include <cstddef> // for size_t
29 #include <string>
30 
31 #define SOCKET_HOSTNAME_MAXSIZE 256
32 #define SOCKET_RCVBUF_MINSIZE 16384
33 #define SOCKET_READ_TIMEOUT_SEC 10
34 #define SOCKET_READ_TIMEOUT_USEC 0
35 #define SOCKET_READ_ATTEMPT 3
36 #define SOCKET_BUFFER_SIZE 1472
37 #define SOCKET_CONNECTION_REQUESTS 5
38 
39 namespace NSROOT
40 {
41 
42  typedef enum {
43  SOCKET_AF_INET4,
44  SOCKET_AF_INET6,
45  } SOCKET_AF_t;
46 
47  struct SocketAddress;
48 
49  class NetSocket
50  {
51  public:
52  NetSocket()
53  {
54  m_timeout.tv_sec = SOCKET_READ_TIMEOUT_SEC;
55  m_timeout.tv_usec = SOCKET_READ_TIMEOUT_USEC;
56  }
57  virtual ~NetSocket() { }
58  virtual bool SendData(const char* buf, size_t size) = 0;
59  virtual size_t ReceiveData(void* buf, size_t n) = 0;
60  void SetTimeout(timeval timeout) { m_timeout = timeout; }
61 
62  protected:
63  struct timeval m_timeout;
64  };
65 
66  class TcpSocket : public NetSocket
67  {
68  friend class TcpServerSocket;
69  public:
70  TcpSocket();
71  virtual ~TcpSocket();
72 
76  int GetErrNo() const { return m_errno; }
77 
83  void SetReadAttempt(int n) { m_attempt = n; }
84 
92  virtual bool Connect(const char *server, unsigned port, int rcvbuf);
93 
100  virtual bool SendData(const char* buf, size_t size);
101 
108  virtual size_t ReceiveData(void* buf, size_t n);
109 
113  virtual void Disconnect();
114 
118  virtual bool IsValid() const;
119 
126  int Listen(timeval *timeout);
127 
131  net_socket_t GetHandle() const { return m_socket; }
132 
136  std::string GetHostAddrInfo();
137 
141  static const char* GetMyHostName();
142 
143  protected:
144  net_socket_t m_socket;
145  int m_rcvbuf;
146  int m_errno;
147  int m_attempt;
148 
149  private:
150  char* m_buffer;
151  char* m_bufptr;
152  size_t m_buflen;
153  size_t m_rcvlen;
154 
155  // prevent copy
156  TcpSocket(const TcpSocket&);
157  TcpSocket& operator=(const TcpSocket&);
158  };
159 
161  {
162  public:
163  TcpServerSocket();
164  ~TcpServerSocket();
165 
169  int GetErrNo() const { return m_errno; }
170 
175  bool Create(SOCKET_AF_t af);
176 
180  bool IsValid() const;
181 
187  bool Bind(unsigned port);
188 
194  bool ListenConnection(int queueSize = SOCKET_CONNECTION_REQUESTS);
195 
201  bool AcceptConnection(TcpSocket& socket);
202 
206  void Close();
207 
211  net_socket_t GetHandle() const { return m_socket; }
212 
213  private:
214  SocketAddress* m_addr;
215  net_socket_t m_socket;
216  int m_errno;
217  unsigned m_requestQueueSize;
218 
219  // prevent copy
221  TcpServerSocket& operator=(const TcpServerSocket&);
222  };
223 
224  class UdpSocket : public NetSocket
225  {
226  public:
227  UdpSocket();
228  UdpSocket(size_t bufferSize);
229  virtual ~UdpSocket();
230 
234  int GetErrNo() const { return m_errno; }
235 
242  bool SendData(const char* buf, size_t size);
243 
250  size_t ReceiveData(void* buf, size_t n);
251 
255  bool IsValid() const;
256 
264  bool Open(SOCKET_AF_t af, const char* target, unsigned port);
265 
272  bool Open(SOCKET_AF_t af, bool broadcast = false);
273 
280  bool SetAddress(const char* target, unsigned port);
281 
288  bool SetMulticastTTL(int multicastTTL);
289 
293  std::string GetRemoteAddrInfo() const;
294 
298  net_socket_t GetHandle() const { return m_socket; }
299 
300  private:
301  SocketAddress* m_addr;
302  SocketAddress* m_from;
303  net_socket_t m_socket;
304  int m_errno;
305  char* m_buffer;
306  char* m_bufptr;
307  size_t m_buflen;
308  size_t m_rcvlen;
309 
310  // prevent copy
311  UdpSocket(const UdpSocket&);
312  UdpSocket& operator=(const UdpSocket&);
313  };
314 
316  {
317  public:
318  UdpServerSocket();
319  UdpServerSocket(size_t bufferSize);
320  ~UdpServerSocket();
321 
325  int GetErrNo() const { return m_errno; }
326 
331  bool Create(SOCKET_AF_t af);
332 
336  bool IsValid() const;
337 
343  bool Bind(unsigned port);
344 
351  bool SetMulticastTTL(int multicastTTL);
352 
359  bool SetMulticastMembership(const char *group, bool join);
360 
365  size_t AwaitIncoming(timeval timeout);
366  size_t AwaitIncoming();
367 
372  std::string GetRemoteAddrInfo() const;
373 
380  size_t ReadData(void* buf, size_t n);
381 
385  net_socket_t GetHandle() const { return m_socket; }
386 
387  private:
388  SocketAddress* m_addr;
389  SocketAddress* m_from;
390  net_socket_t m_socket;
391  int m_errno;
392  char* m_buffer;
393  char* m_bufptr;
394  size_t m_buflen;
395  size_t m_rcvlen;
396  struct timeval m_timeout;
397 
398  // prevent copy
400  UdpServerSocket& operator=(const UdpServerSocket&);
401  };
402 
404  {
405  public:
406  UdpMessageReader(UdpServerSocket& boundSocket)
407  : NetSocket()
408  , m_bound(boundSocket) { }
409 
410  bool SendData(const char* buf, size_t size) { (void)buf; (void)size; return false; };
411 
412  size_t ReceiveData(void* buf, size_t n)
413  {
414  size_t r = 0;
415  if (m_bound.IsValid())
416  {
417  if ((r = m_bound.ReadData(buf, n)) > 0)
418  return r;
419  if (m_bound.AwaitIncoming(m_timeout) > 0)
420  return m_bound.ReadData(buf, n);
421  }
422  return r;
423  }
424 
425  private:
426  UdpServerSocket& m_bound;
427  };
428 
429 }
430 
431 #endif /* SOCKET_H */
net_socket_t GetHandle() const
Definition: socket.h:211
int GetErrNo() const
Definition: socket.h:234
int GetErrNo() const
Definition: socket.h:169
void SetReadAttempt(int n)
Definition: socket.h:83
int GetErrNo() const
Definition: socket.h:76
net_socket_t GetHandle() const
Definition: socket.h:298
net_socket_t GetHandle() const
Definition: socket.h:385
net_socket_t GetHandle() const
Definition: socket.h:131
int GetErrNo() const
Definition: socket.h:325