Commit | Line | Data |
---|---|---|
24048d57 LOK |
1 | #pragma once |
2 | /* | |
3 | * This file is part of the libCEC(R) library. | |
4 | * | |
5 | * libCEC(R) is Copyright (C) 2011 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 | ||
1d9111bd LOK |
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 | ||
24048d57 LOK |
48 | namespace PLATFORM |
49 | { | |
1d9111bd LOK |
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 | ||
24048d57 LOK |
67 | inline void SocketClose(socket_t socket) |
68 | { | |
1d9111bd LOK |
69 | if (socket != SOCKET_ERROR && socket != INVALID_SOCKET) |
70 | closesocket(socket); | |
24048d57 LOK |
71 | } |
72 | ||
73 | inline void SocketSetBlocking(socket_t socket, bool bSetTo) | |
74 | { | |
1d9111bd LOK |
75 | u_long nVal = bSetTo ? 1 : 0; |
76 | ioctlsocket(socket, FIONBIO, &nVal); | |
24048d57 LOK |
77 | } |
78 | ||
79 | inline int64_t SocketWrite(socket_t socket, int *iError, uint8_t* data, uint32_t len) | |
80 | { | |
1d9111bd LOK |
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; | |
24048d57 LOK |
89 | } |
90 | ||
1d9111bd | 91 | inline int SocketReadFixed(socket_t socket, char *buf, int iLength, int iFlags) |
24048d57 | 92 | { |
71194cf4 | 93 | int iReadResult(1), iBytesRead(0); |
1d9111bd LOK |
94 | |
95 | if ((iFlags & MSG_WAITALL) == 0) | |
96 | return recv(socket, buf, iLength, iFlags); | |
97 | ||
98 | iFlags &= ~MSG_WAITALL; | |
71194cf4 | 99 | while(iBytesRead < iLength && iReadResult > 0) |
1d9111bd | 100 | { |
71194cf4 LOK |
101 | if ((iReadResult = recv(socket, buf + iBytesRead, iLength - iBytesRead, iFlags)) < 0) |
102 | return iReadResult; | |
1d9111bd | 103 | } |
71194cf4 | 104 | return iLength - iBytesRead; |
1d9111bd LOK |
105 | } |
106 | ||
107 | inline int32_t SocketRead(socket_t socket, void *buf, uint32_t nLen) | |
108 | { | |
71194cf4 | 109 | int iReadResult = SocketReadFixed(socket, (char *)buf, nLen, MSG_WAITALL); |
1d9111bd | 110 | |
71194cf4 | 111 | if (iReadResult == -1) |
1d9111bd | 112 | return GetSocketError(); |
71194cf4 | 113 | if (iReadResult < (int)nLen) |
1d9111bd LOK |
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 | { | |
71194cf4 LOK |
121 | int iReadResult(0); |
122 | uint32_t iBytesRead(0); | |
1d9111bd LOK |
123 | fd_set fd_read; |
124 | struct timeval tv; | |
125 | ||
126 | if (iTimeoutMs <= 0) | |
127 | return EINVAL; | |
128 | ||
71194cf4 LOK |
129 | uint64_t iNow = GetTimeMs(); |
130 | uint64_t iTarget = iNow + iTimeoutMs; | |
131 | ||
132 | while(iNow < iTarget && iBytesRead < iLength) | |
1d9111bd | 133 | { |
71194cf4 LOK |
134 | tv.tv_sec = (long)(iTarget - iNow / 1000); |
135 | tv.tv_usec = (long)(1000 * (iTarget - iNow % 1000)); | |
1d9111bd LOK |
136 | |
137 | FD_ZERO(&fd_read); | |
138 | FD_SET(socket, &fd_read); | |
139 | ||
71194cf4 | 140 | if ((iReadResult = select(socket + 1, &fd_read, NULL, NULL, &tv)) == 0) |
1d9111bd LOK |
141 | return ETIMEDOUT; |
142 | ||
143 | SocketSetBlocking(socket, false); | |
144 | ||
71194cf4 | 145 | iReadResult = SocketReadFixed(socket, (char *)data + iBytesRead, iLength - iBytesRead, 0); |
1d9111bd LOK |
146 | |
147 | SocketSetBlocking(socket, true); | |
148 | ||
71194cf4 | 149 | if (iReadResult == -1) |
1d9111bd | 150 | { |
71194cf4 LOK |
151 | int iError = GetSocketError(); |
152 | if (iError == EAGAIN) | |
1d9111bd | 153 | continue; |
71194cf4 | 154 | return iError; |
1d9111bd | 155 | } |
71194cf4 | 156 | else if (iReadResult == 0) |
1d9111bd LOK |
157 | return ECONNRESET; |
158 | ||
71194cf4 | 159 | iBytesRead += iReadResult; |
1d9111bd | 160 | } |
71194cf4 LOK |
161 | |
162 | return iBytesRead == iLength ? 0 : ECONNRESET; | |
24048d57 LOK |
163 | } |
164 | } |