3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
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/
40 class CTcpSocket
: public CCommonSocket
<tcp_socket_t
>
43 CTcpSocket(const std::string
&strHostname
, uint16_t iPort
) :
44 CCommonSocket
<tcp_socket_t
>(INVALID_SOCKET_VALUE
, strHostname
),
47 virtual ~CTcpSocket(void) {}
49 virtual bool Open(uint64_t iTimeoutMs
= 0)
52 struct addrinfo
*address(NULL
), *addr(NULL
);
53 if (!TcpResolveAddress(m_strName
.c_str(), m_iPort
, &m_iError
, &address
))
55 m_strError
= strerror(m_iError
);
59 for(addr
= address
; !bReturn
&& addr
; addr
= addr
->ai_next
)
61 m_socket
= TcpCreateSocket(addr
, &m_iError
, iTimeoutMs
);
62 if (m_socket
!= INVALID_SOCKET_VALUE
)
65 m_strError
= strerror(m_iError
);
68 freeaddrinfo(address
);
72 virtual void Close(void)
74 TcpSocketClose(m_socket
);
75 m_socket
= INVALID_SOCKET_VALUE
;
78 virtual void Shutdown(void)
80 TcpSocketShutdown(m_socket
);
81 m_socket
= INVALID_SOCKET_VALUE
;
84 virtual ssize_t
Write(void* data
, size_t len
)
86 return TcpSocketWrite(m_socket
, &m_iError
, data
, len
);
89 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0)
91 return TcpSocketRead(m_socket
, &m_iError
, data
, len
, iTimeoutMs
);
94 virtual bool IsOpen(void)
96 return m_socket
!= INVALID_SOCKET_VALUE
;
100 virtual tcp_socket_t
TcpCreateSocket(struct addrinfo
* addr
, int* iError
, uint64_t iTimeout
)
102 tcp_socket_t fdSock
= socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
103 if (fdSock
== INVALID_SOCKET_VALUE
)
106 return (tcp_socket_t
)INVALID_SOCKET_VALUE
;
109 if (!TcpConnectSocket(fdSock
, addr
, iError
, iTimeout
))
111 TcpSocketClose(fdSock
);
112 return (tcp_socket_t
)INVALID_SOCKET_VALUE
;
115 TcpSetNoDelay(fdSock
);
123 class CTcpClientSocket
: public CCommonSocket
<tcp_socket_t
>
126 CTcpClientSocket(tcp_socket_t socket
) :
127 CCommonSocket
<tcp_socket_t
>(socket
, "tcpclient") {}
129 virtual ~CTcpClientSocket(void) {}
131 virtual bool Open(uint64_t iTimeoutMs
= 0)
137 virtual void Close(void)
139 TcpSocketClose(m_socket
);
140 m_socket
= INVALID_SOCKET_VALUE
;
143 virtual void Shutdown(void)
145 TcpSocketShutdown(m_socket
);
146 m_socket
= INVALID_SOCKET_VALUE
;
149 virtual ssize_t
Write(void* data
, size_t len
)
151 return TcpSocketWrite(m_socket
, &m_iError
, data
, len
);
154 virtual ssize_t
Read(void* data
, size_t len
, uint64_t iTimeoutMs
= 0)
156 return TcpSocketRead(m_socket
, &m_iError
, data
, len
, iTimeoutMs
);
159 virtual bool IsOpen(void)
161 return m_socket
!= INVALID_SOCKET_VALUE
;
165 class CTcpConnection
: public CProtectedSocket
<CTcpSocket
>
168 CTcpConnection(const std::string
&strHostname
, uint16_t iPort
) :
169 CProtectedSocket
<CTcpSocket
> (new CTcpSocket(strHostname
, iPort
)) {}
170 virtual ~CTcpConnection(void) {}