bump version numbers to 1.7.0 and updated changelog
[deb_libcec.git] / src / lib / platform / sockets / tcp.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34 #include "socket.h"
35
36 using namespace std;
37
38 namespace PLATFORM
39 {
40 class CTcpSocket : public CCommonSocket<tcp_socket_t>
41 {
42 public:
43 CTcpSocket(const CStdString &strHostname, uint16_t iPort) :
44 CCommonSocket<tcp_socket_t>(INVALID_SOCKET_VALUE, strHostname),
45 m_iPort(iPort) {}
46
47 virtual ~CTcpSocket(void) {}
48
49 virtual bool Open(uint64_t iTimeoutMs = 0)
50 {
51 bool bReturn(false);
52 struct addrinfo *address(NULL), *addr(NULL);
53 if (!TcpResolveAddress(m_strName.c_str(), m_iPort, &m_iError, &address))
54 {
55 m_strError = strerror(m_iError);
56 return bReturn;
57 }
58
59 for(addr = address; !bReturn && addr; addr = addr->ai_next)
60 {
61 m_socket = TcpCreateSocket(addr, &m_iError, iTimeoutMs);
62 if (m_socket != INVALID_SOCKET_VALUE)
63 bReturn = true;
64 else
65 m_strError = strerror(m_iError);
66 }
67
68 freeaddrinfo(address);
69 return bReturn;
70 }
71
72 virtual void Close(void)
73 {
74 TcpSocketClose(m_socket);
75 m_socket = INVALID_SOCKET_VALUE;
76 }
77
78 virtual void Shutdown(void)
79 {
80 TcpSocketShutdown(m_socket);
81 m_socket = INVALID_SOCKET_VALUE;
82 }
83
84 virtual ssize_t Write(void* data, size_t len)
85 {
86 return TcpSocketWrite(m_socket, &m_iError, data, len);
87 }
88
89 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0)
90 {
91 return TcpSocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
92 }
93
94 virtual bool IsOpen(void)
95 {
96 return m_socket != INVALID_SOCKET_VALUE;
97 }
98
99 protected:
100 virtual tcp_socket_t TcpCreateSocket(struct addrinfo* addr, int* iError, uint64_t iTimeout)
101 {
102 tcp_socket_t fdSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
103 if (fdSock == INVALID_SOCKET_VALUE)
104 {
105 *iError = errno;
106 return (tcp_socket_t)INVALID_SOCKET_VALUE;
107 }
108
109 if (!TcpConnectSocket(fdSock, addr, iError, iTimeout))
110 {
111 TcpSocketClose(fdSock);
112 return (tcp_socket_t)INVALID_SOCKET_VALUE;
113 }
114
115 TcpSetNoDelay(fdSock);
116
117 return fdSock;
118 }
119
120 uint16_t m_iPort;
121 };
122
123 class CTcpClientSocket : public CCommonSocket<tcp_socket_t>
124 {
125 public:
126 CTcpClientSocket(tcp_socket_t socket) :
127 CCommonSocket<tcp_socket_t>(socket, "tcpclient") {}
128
129 virtual ~CTcpClientSocket(void) {}
130
131 virtual bool Open(uint64_t iTimeoutMs = 0)
132 {
133 (void) iTimeoutMs;
134 return true;
135 }
136
137 virtual void Close(void)
138 {
139 TcpSocketClose(m_socket);
140 m_socket = INVALID_SOCKET_VALUE;
141 }
142
143 virtual void Shutdown(void)
144 {
145 TcpSocketShutdown(m_socket);
146 m_socket = INVALID_SOCKET_VALUE;
147 }
148
149 virtual ssize_t Write(void* data, size_t len)
150 {
151 return TcpSocketWrite(m_socket, &m_iError, data, len);
152 }
153
154 virtual ssize_t Read(void* data, size_t len, uint64_t iTimeoutMs = 0)
155 {
156 return TcpSocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
157 }
158
159 virtual bool IsOpen(void)
160 {
161 return m_socket != INVALID_SOCKET_VALUE;
162 }
163 };
164
165 class CTcpConnection : public CProtectedSocket<CTcpSocket>
166 {
167 public:
168 CTcpConnection(const CStdString &strHostname, uint16_t iPort) :
169 CProtectedSocket<CTcpSocket> (new CTcpSocket(strHostname, iPort)) {}
170 virtual ~CTcpConnection(void) {}
171 };
172 };