CPPMyth
Library to interoperate with MythTV server
Loading...
Searching...
No Matches
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 "local_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_TIMEOUT_SEC 10
34#define SOCKET_TIMEOUT_USEC 0
35#define SOCKET_READ_ATTEMPT 3
36#define SOCKET_BUFFER_SIZE 1472
37#define SOCKET_LISTEN_QUEUE_SIZE 50
38
39namespace 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_TIMEOUT_SEC;
55 m_timeout.tv_usec = SOCKET_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
116 virtual size_t BlockingRead(void* buf, size_t n);
117
121 virtual void Disconnect();
122
126 virtual bool IsValid() const;
127
134 int Listen(timeval *timeout);
135
139 net_socket_t GetHandle() const { return m_socket; }
140
144 std::string GetRemoteAddrInfo();
145
149 std::string GetHostAddrInfo();
150
154 static const char* GetMyHostName();
155
156 protected:
157 net_socket_t m_socket;
158 int m_rcvbuf;
159 int m_errno;
160 int m_attempt;
161
162 private:
163 char* m_buffer;
164 char* m_bufptr;
165 size_t m_buflen;
166 size_t m_rcvlen;
167
168 // prevent copy
169 TcpSocket(const TcpSocket&);
170 TcpSocket& operator=(const TcpSocket&);
171 };
172
173 class TcpServerSocket
174 {
175 public:
176 TcpServerSocket();
177 virtual ~TcpServerSocket();
178
182 int GetErrNo() const { return m_errno; }
183
188 bool Create(SOCKET_AF_t af);
189
193 bool IsValid() const;
194
200 bool Bind(unsigned port);
201
207 bool ListenConnection(int queueSize = SOCKET_LISTEN_QUEUE_SIZE);
208
209 enum AcceptStatus
210 {
211 ACCEPT_ERROR = -1,
212 ACCEPT_TIMEOUT = 0,
213 ACCEPT_SUCCESS = 1,
214 ACCEPT_FAILURE = 2,
215 };
222 AcceptStatus AcceptConnection(TcpSocket& socket, int timeout);
223
227 std::string GetRemoteAddrInfo();
228
232 void Close();
233
237 net_socket_t GetHandle() const { return m_socket; }
238
239 private:
240 SocketAddress* m_addr;
241 net_socket_t m_socket;
242 int m_errno;
243 unsigned m_requestQueueSize;
244
245 // prevent copy
247 TcpServerSocket& operator=(const TcpServerSocket&);
248 };
249
250 class UdpSocket : public NetSocket
251 {
252 public:
253 UdpSocket();
254 UdpSocket(size_t bufferSize);
255 virtual ~UdpSocket();
256
260 int GetErrNo() const { return m_errno; }
261
268 bool SendData(const char* buf, size_t size);
269
276 size_t ReceiveData(void* buf, size_t n);
277
281 bool IsValid() const;
282
290 bool Open(SOCKET_AF_t af, const char* target, unsigned port);
291
298 bool Open(SOCKET_AF_t af, bool broadcast = false);
299
306 bool SetAddress(const char* target, unsigned port);
307
314 bool SetMulticastTTL(int multicastTTL);
315
319 std::string GetRemoteAddrInfo() const;
320
324 net_socket_t GetHandle() const { return m_socket; }
325
326 private:
327 SocketAddress* m_addr;
328 SocketAddress* m_from;
329 net_socket_t m_socket;
330 int m_errno;
331 char* m_buffer;
332 char* m_bufptr;
333 size_t m_buflen;
334 size_t m_rcvlen;
335
336 // prevent copy
337 UdpSocket(const UdpSocket&);
338 UdpSocket& operator=(const UdpSocket&);
339 };
340
341 class UdpServerSocket : public NetSocket
342 {
343 public:
344 UdpServerSocket();
345 UdpServerSocket(size_t bufferSize);
346 ~UdpServerSocket();
347
351 int GetErrNo() const { return m_errno; }
352
357 bool Create(SOCKET_AF_t af);
358
362 bool IsValid() const;
363
369 bool Bind(unsigned port, bool reuse = false);
370
377 bool Bind(const char* addr, unsigned port, bool reuse = false);
378
385 bool SetMulticastTTL(int multicastTTL);
386
393 bool SetMulticastMembership(const char *group, bool join);
394
399 size_t AwaitIncoming();
400
405 std::string GetRemoteAddrInfo() const;
406
411 bool SendData(const char*, size_t) { return false; }
412
419 size_t ReceiveData(void* buf, size_t n)
420 {
421 size_t r = 0;
422 if (IsValid())
423 {
424 if ((r = ReadData(buf, n)) > 0)
425 return r;
426 if (AwaitIncoming() > 0)
427 return ReadData(buf, n);
428 }
429 return r;
430 }
431
438 size_t ReadData(void* buf, size_t n);
439
443 net_socket_t GetHandle() const { return m_socket; }
444
445 private:
446 SocketAddress* m_addr;
447 SocketAddress* m_from;
448 net_socket_t m_socket;
449 int m_errno;
450 char* m_buffer;
451 char* m_bufptr;
452 size_t m_buflen;
453 size_t m_rcvlen;
454
455 // prevent copy
457 UdpServerSocket& operator=(const UdpServerSocket&);
458 };
459
460}
461
462#endif /* SOCKET_H */
net_socket_t GetHandle() const
Definition socket.h:237
int GetErrNo() const
Definition socket.h:182
int GetErrNo() const
Definition socket.h:76
net_socket_t GetHandle() const
Definition socket.h:139
void SetReadAttempt(int n)
Definition socket.h:83
int GetErrNo() const
Definition socket.h:351
bool SendData(const char *, size_t)
Definition socket.h:411
net_socket_t GetHandle() const
Definition socket.h:443
size_t ReadData(void *buf, size_t n)
Definition socket.cpp:1441
size_t ReceiveData(void *buf, size_t n)
Definition socket.h:419
int GetErrNo() const
Definition socket.h:260
net_socket_t GetHandle() const
Definition socket.h:324