| 1 | /* |
| 2 | * This file is part of the libCEC(R) library. |
| 3 | * |
| 4 | * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved. |
| 5 | * libCEC(R) is an original work, containing original code. |
| 6 | * |
| 7 | * libCEC(R) is a trademark of Pulse-Eight Limited. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 22 | * |
| 23 | * |
| 24 | * Alternatively, you can license this library under a commercial license, |
| 25 | * please contact Pulse-Eight Licensing for more information. |
| 26 | * |
| 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/ |
| 31 | */ |
| 32 | |
| 33 | #include "env.h" |
| 34 | #include "lib/platform/sockets/tcp.h" |
| 35 | #include "lib/platform/sockets/serversocket.h" |
| 36 | |
| 37 | using namespace std; |
| 38 | using namespace PLATFORM; |
| 39 | |
| 40 | bool CTcpServerSocket::Open(uint64_t UNUSED(iTimeoutMs)) |
| 41 | { |
| 42 | bool bReturn(false); |
| 43 | struct addrinfo *address(NULL), *addr(NULL); |
| 44 | if (!TcpResolveAddress("localhost", m_iPort, &m_iError, &address)) |
| 45 | { |
| 46 | m_strError = strerror(m_iError); |
| 47 | return bReturn; |
| 48 | } |
| 49 | |
| 50 | for(addr = address; !bReturn && addr; addr = addr->ai_next) |
| 51 | { |
| 52 | m_socket = TcpCreateSocket(addr, &m_iError); |
| 53 | if (m_socket != INVALID_SOCKET_VALUE) |
| 54 | { |
| 55 | bReturn = true; |
| 56 | break; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | m_strError = strerror(m_iError); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (bReturn) |
| 65 | { |
| 66 | m_iError = bind(m_socket, addr->ai_addr, addr->ai_addrlen); |
| 67 | if (m_iError) |
| 68 | { |
| 69 | m_strError = strerror(m_iError); |
| 70 | bReturn = false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | freeaddrinfo(address); |
| 75 | |
| 76 | if (bReturn) |
| 77 | { |
| 78 | m_iError = listen(m_socket, 16); |
| 79 | if (m_iError) |
| 80 | { |
| 81 | m_strError = strerror(m_iError); |
| 82 | bReturn = false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return bReturn; |
| 87 | } |
| 88 | |
| 89 | void CTcpServerSocket::Close(void) |
| 90 | { |
| 91 | if (IsOpen()) |
| 92 | TcpSocketClose(m_socket); |
| 93 | m_socket = INVALID_SOCKET_VALUE; |
| 94 | } |
| 95 | |
| 96 | void CTcpServerSocket::Shutdown(void) |
| 97 | { |
| 98 | Close(); |
| 99 | } |
| 100 | |
| 101 | bool CTcpServerSocket::IsOpen(void) |
| 102 | { |
| 103 | return m_socket != INVALID_SOCKET_VALUE; |
| 104 | } |
| 105 | |
| 106 | std::string CTcpServerSocket::GetError(void) |
| 107 | { |
| 108 | std::string strError; |
| 109 | strError = m_strError.empty() && m_iError != 0 ? strerror(m_iError) : m_strError; |
| 110 | return strError; |
| 111 | } |
| 112 | |
| 113 | int CTcpServerSocket::GetErrorNumber(void) |
| 114 | { |
| 115 | return m_iError; |
| 116 | } |
| 117 | |
| 118 | std::string CTcpServerSocket::GetName(void) |
| 119 | { |
| 120 | std::string strName("localhost"); |
| 121 | return strName; |
| 122 | } |
| 123 | |
| 124 | ISocket* CTcpServerSocket::Accept(void) |
| 125 | { |
| 126 | struct sockaddr clientAddr; |
| 127 | unsigned int iClientLen(sizeof(clientAddr)); |
| 128 | tcp_socket_t client = accept(m_socket, &clientAddr, &iClientLen); |
| 129 | |
| 130 | if (client != INVALID_SOCKET_VALUE) |
| 131 | { |
| 132 | CTcpClientSocket *socket = new CTcpClientSocket(client); |
| 133 | return (ISocket*)socket; |
| 134 | } |
| 135 | |
| 136 | m_strError = strerror(m_iError); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | tcp_socket_t CTcpServerSocket::TcpCreateSocket(struct addrinfo* addr, int* iError) |
| 141 | { |
| 142 | tcp_socket_t fdSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
| 143 | if (fdSock == INVALID_SOCKET_VALUE) |
| 144 | { |
| 145 | *iError = errno; |
| 146 | return (tcp_socket_t)INVALID_SOCKET_VALUE; |
| 147 | } |
| 148 | |
| 149 | TcpSetNoDelay(fdSock); |
| 150 | |
| 151 | return fdSock; |
| 152 | } |