| 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 "../os.h" |
| 35 | #include "../util/timeutils.h" |
| 36 | |
| 37 | #pragma comment(lib, "Ws2_32.lib") |
| 38 | #include <ws2spi.h> |
| 39 | #include <ws2ipdef.h> |
| 40 | #include <ws2tcpip.h> |
| 41 | |
| 42 | #define SHUT_RDWR SD_BOTH |
| 43 | |
| 44 | #ifndef ETIMEDOUT |
| 45 | #define ETIMEDOUT 138 |
| 46 | #endif |
| 47 | |
| 48 | namespace PLATFORM |
| 49 | { |
| 50 | #ifndef MSG_WAITALL |
| 51 | #define MSG_WAITALL 0x8 |
| 52 | #endif |
| 53 | |
| 54 | inline int GetSocketError(void) |
| 55 | { |
| 56 | int error = WSAGetLastError(); |
| 57 | switch(error) |
| 58 | { |
| 59 | case WSAEINPROGRESS: return EINPROGRESS; |
| 60 | case WSAECONNRESET : return ECONNRESET; |
| 61 | case WSAETIMEDOUT : return ETIMEDOUT; |
| 62 | case WSAEWOULDBLOCK: return EAGAIN; |
| 63 | default : return error; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | inline void SocketClose(socket_t socket) |
| 68 | { |
| 69 | if (socket != SOCKET_ERROR && socket != INVALID_SOCKET) |
| 70 | closesocket(socket); |
| 71 | } |
| 72 | |
| 73 | inline void SocketSetBlocking(socket_t socket, bool bSetTo) |
| 74 | { |
| 75 | u_long nVal = bSetTo ? 1 : 0; |
| 76 | ioctlsocket(socket, FIONBIO, &nVal); |
| 77 | } |
| 78 | |
| 79 | inline int64_t SocketWrite(socket_t socket, int *iError, uint8_t* data, uint32_t len) |
| 80 | { |
| 81 | int64_t iReturn(-1); |
| 82 | if (socket != SOCKET_ERROR && socket != INVALID_SOCKET) |
| 83 | { |
| 84 | iReturn = send(socket, (char*)data, len, 0); |
| 85 | if (iReturn <= 0) |
| 86 | *iError = GetSocketError(); |
| 87 | } |
| 88 | return iReturn; |
| 89 | } |
| 90 | |
| 91 | inline int SocketReadFixed(socket_t socket, char *buf, int iLength, int iFlags) |
| 92 | { |
| 93 | int iReadResult(1), iBytesRead(0); |
| 94 | |
| 95 | if ((iFlags & MSG_WAITALL) == 0) |
| 96 | return recv(socket, buf, iLength, iFlags); |
| 97 | |
| 98 | iFlags &= ~MSG_WAITALL; |
| 99 | while(iBytesRead < iLength && iReadResult > 0) |
| 100 | { |
| 101 | if ((iReadResult = recv(socket, buf + iBytesRead, iLength - iBytesRead, iFlags)) < 0) |
| 102 | return iReadResult; |
| 103 | } |
| 104 | return iLength - iBytesRead; |
| 105 | } |
| 106 | |
| 107 | inline int32_t SocketRead(socket_t socket, void *buf, uint32_t nLen) |
| 108 | { |
| 109 | int iReadResult = SocketReadFixed(socket, (char *)buf, nLen, MSG_WAITALL); |
| 110 | |
| 111 | if (iReadResult == -1) |
| 112 | return GetSocketError(); |
| 113 | if (iReadResult < (int)nLen) |
| 114 | return ECONNRESET; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | inline int32_t SocketRead(socket_t socket, int *iError, uint8_t* data, uint32_t iLength, uint64_t iTimeoutMs) |
| 120 | { |
| 121 | int iReadResult(0); |
| 122 | uint32_t iBytesRead(0); |
| 123 | fd_set fd_read; |
| 124 | struct timeval tv; |
| 125 | |
| 126 | if (iTimeoutMs <= 0) |
| 127 | return EINVAL; |
| 128 | |
| 129 | uint64_t iNow = GetTimeMs(); |
| 130 | uint64_t iTarget = iNow + iTimeoutMs; |
| 131 | |
| 132 | while(iNow < iTarget && iBytesRead < iLength) |
| 133 | { |
| 134 | tv.tv_sec = (long)(iTarget - iNow / 1000); |
| 135 | tv.tv_usec = (long)(1000 * (iTarget - iNow % 1000)); |
| 136 | |
| 137 | FD_ZERO(&fd_read); |
| 138 | FD_SET(socket, &fd_read); |
| 139 | |
| 140 | if ((iReadResult = select(socket + 1, &fd_read, NULL, NULL, &tv)) == 0) |
| 141 | return ETIMEDOUT; |
| 142 | |
| 143 | SocketSetBlocking(socket, false); |
| 144 | |
| 145 | iReadResult = SocketReadFixed(socket, (char *)data + iBytesRead, iLength - iBytesRead, 0); |
| 146 | |
| 147 | SocketSetBlocking(socket, true); |
| 148 | |
| 149 | if (iReadResult == -1) |
| 150 | { |
| 151 | int iError = GetSocketError(); |
| 152 | if (iError == EAGAIN) |
| 153 | continue; |
| 154 | return iError; |
| 155 | } |
| 156 | else if (iReadResult == 0) |
| 157 | return ECONNRESET; |
| 158 | |
| 159 | iBytesRead += iReadResult; |
| 160 | } |
| 161 | |
| 162 | return iBytesRead == iLength ? 0 : ECONNRESET; |
| 163 | } |
| 164 | } |