2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
34 #include "lib/platform/sockets/tcp.h"
35 #include "lib/platform/sockets/serversocket.h"
38 using namespace PLATFORM
;
40 bool CTcpServerSocket::Open(uint64_t UNUSED(iTimeoutMs
))
43 struct addrinfo
*address(NULL
), *addr(NULL
);
44 if (!TcpResolveAddress("localhost", m_iPort
, &m_iError
, &address
))
46 m_strError
= strerror(m_iError
);
50 for(addr
= address
; !bReturn
&& addr
; addr
= addr
->ai_next
)
52 m_socket
= TcpCreateSocket(addr
, &m_iError
);
53 if (m_socket
!= INVALID_SOCKET_VALUE
)
60 m_strError
= strerror(m_iError
);
66 m_iError
= bind(m_socket
, addr
->ai_addr
, addr
->ai_addrlen
);
69 m_strError
= strerror(m_iError
);
74 freeaddrinfo(address
);
78 m_iError
= listen(m_socket
, 16);
81 m_strError
= strerror(m_iError
);
89 void CTcpServerSocket::Close(void)
92 TcpSocketClose(m_socket
);
93 m_socket
= INVALID_SOCKET_VALUE
;
96 void CTcpServerSocket::Shutdown(void)
101 bool CTcpServerSocket::IsOpen(void)
103 return m_socket
!= INVALID_SOCKET_VALUE
;
106 std::string
CTcpServerSocket::GetError(void)
108 std::string strError
;
109 strError
= m_strError
.empty() && m_iError
!= 0 ? strerror(m_iError
) : m_strError
;
113 int CTcpServerSocket::GetErrorNumber(void)
118 std::string
CTcpServerSocket::GetName(void)
120 std::string
strName("localhost");
124 ISocket
* CTcpServerSocket::Accept(void)
126 struct sockaddr clientAddr
;
127 unsigned int iClientLen(sizeof(clientAddr
));
128 tcp_socket_t client
= accept(m_socket
, &clientAddr
, &iClientLen
);
130 if (client
!= INVALID_SOCKET_VALUE
)
132 CTcpClientSocket
*socket
= new CTcpClientSocket(client
);
133 return (ISocket
*)socket
;
136 m_strError
= strerror(m_iError
);
140 tcp_socket_t
CTcpServerSocket::TcpCreateSocket(struct addrinfo
* addr
, int* iError
)
142 tcp_socket_t fdSock
= socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
143 if (fdSock
== INVALID_SOCKET_VALUE
)
146 return (tcp_socket_t
)INVALID_SOCKET_VALUE
;
149 TcpSetNoDelay(fdSock
);