Commit | Line | Data |
---|---|---|
24048d57 LOK |
1 | #pragma once |
2 | /* | |
3 | * This file is part of the libCEC(R) library. | |
4 | * | |
b492c10e | 5 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
24048d57 LOK |
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 | ||
99666519 LOK |
67 | // Serial port |
68 | //@{ | |
69 | inline void SerialSocketClose(serial_socket_t socket) | |
24048d57 | 70 | { |
99666519 LOK |
71 | if (socket != INVALID_HANDLE_VALUE) |
72 | CloseHandle(socket); | |
24048d57 LOK |
73 | } |
74 | ||
99666519 | 75 | inline ssize_t SerialSocketWrite(serial_socket_t socket, int *iError, void* data, size_t len) |
24048d57 | 76 | { |
99666519 LOK |
77 | DWORD iBytesWritten(0); |
78 | if (socket != INVALID_HANDLE_VALUE) | |
79 | { | |
80 | if (!WriteFile(socket, data, len, &iBytesWritten, NULL)) | |
81 | { | |
82 | *iError = GetLastError(); | |
83 | return -1; | |
84 | } | |
85 | return (ssize_t)iBytesWritten; | |
86 | } | |
87 | ||
88 | return -1; | |
24048d57 LOK |
89 | } |
90 | ||
99666519 | 91 | inline ssize_t SerialSocketRead(serial_socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/) |
24048d57 | 92 | { |
99666519 LOK |
93 | DWORD iBytesRead(0); |
94 | if (socket != INVALID_HANDLE_VALUE) | |
1d9111bd | 95 | { |
99666519 LOK |
96 | if(!ReadFile(socket, data, len, &iBytesRead, NULL) != 0) |
97 | { | |
98 | *iError = GetLastError(); | |
99 | return -1; | |
100 | } | |
101 | return (ssize_t)iBytesRead; | |
1d9111bd | 102 | } |
99666519 | 103 | return -1; |
24048d57 | 104 | } |
99666519 | 105 | //@} |
24048d57 | 106 | |
99666519 LOK |
107 | // TCP |
108 | //@{ | |
109 | inline void TcpSocketSetBlocking(tcp_socket_t socket, bool bSetTo) | |
24048d57 | 110 | { |
99666519 LOK |
111 | u_long iSetTo = bSetTo ? 0 : 1; |
112 | ioctlsocket(socket, FIONBIO, &iSetTo); | |
113 | } | |
1d9111bd | 114 | |
99666519 LOK |
115 | inline void TcpSocketClose(tcp_socket_t socket) |
116 | { | |
117 | closesocket(socket); | |
118 | } | |
1d9111bd | 119 | |
99666519 LOK |
120 | inline void TcpSocketShutdown(tcp_socket_t socket) |
121 | { | |
122 | if (socket != INVALID_SOCKET && | |
123 | socket != SOCKET_ERROR) | |
124 | shutdown(socket, SHUT_RDWR); | |
125 | } | |
126 | ||
127 | inline ssize_t TcpSocketWrite(tcp_socket_t socket, int *iError, void* data, size_t len) | |
128 | { | |
129 | if (socket == INVALID_SOCKET || | |
130 | socket == SOCKET_ERROR) | |
1d9111bd | 131 | { |
99666519 LOK |
132 | *iError = EINVAL; |
133 | return -1; | |
1d9111bd | 134 | } |
99666519 LOK |
135 | |
136 | ssize_t iReturn = send(socket, (char*)data, len, 0); | |
137 | if (iReturn < (ssize_t)len) | |
138 | *iError = errno; | |
139 | return iReturn; | |
1d9111bd LOK |
140 | } |
141 | ||
99666519 | 142 | inline ssize_t TcpSocketRead(tcp_socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/) |
1d9111bd | 143 | { |
99666519 LOK |
144 | int64_t iNow(0), iTarget(0); |
145 | ssize_t iBytesRead(0); | |
146 | *iError = 0; | |
1d9111bd | 147 | |
99666519 LOK |
148 | if (socket == INVALID_SOCKET || |
149 | socket == SOCKET_ERROR) | |
150 | { | |
151 | *iError = EINVAL; | |
152 | return -1; | |
153 | } | |
1d9111bd | 154 | |
99666519 LOK |
155 | if (iTimeoutMs > 0) |
156 | { | |
157 | iNow = GetTimeMs(); | |
158 | iTarget = iNow + (int64_t) iTimeoutMs; | |
159 | } | |
1d9111bd | 160 | |
1d9111bd LOK |
161 | fd_set fd_read; |
162 | struct timeval tv; | |
99666519 LOK |
163 | while (iBytesRead >= 0 && iBytesRead < (ssize_t)len && (iTimeoutMs == 0 || iTarget > iNow)) |
164 | { | |
165 | if (iTimeoutMs > 0) | |
166 | { | |
167 | tv.tv_sec = (long)(iTimeoutMs / 1000); | |
168 | tv.tv_usec = 1000 * (long)(iTimeoutMs % 1000); | |
1d9111bd | 169 | |
99666519 LOK |
170 | FD_ZERO(&fd_read); |
171 | FD_SET(socket, &fd_read); | |
1d9111bd | 172 | |
99666519 LOK |
173 | if (select(socket + 1, &fd_read, NULL, NULL, &tv) == 0) |
174 | return ETIMEDOUT; | |
175 | TcpSocketSetBlocking(socket, false); | |
176 | } | |
71194cf4 | 177 | |
99666519 LOK |
178 | ssize_t iReadResult = (iTimeoutMs > 0) ? |
179 | recv(socket, (char*)data + iBytesRead, len - iBytesRead, MSG_WAITALL) : | |
180 | recv(socket, (char*)data, len, MSG_WAITALL); | |
181 | *iError = GetSocketError(); | |
182 | if (iReadResult < 0) | |
183 | { | |
184 | if (errno == EAGAIN && iTimeoutMs > 0) | |
185 | continue; | |
186 | *iError = errno; | |
187 | return -1; | |
188 | } | |
189 | else if (iReadResult == 0 || (iReadResult != (ssize_t)len && iTimeoutMs == 0)) | |
190 | { | |
191 | *iError = ECONNRESET; | |
192 | return -1; | |
193 | } | |
1d9111bd | 194 | |
99666519 | 195 | iBytesRead += iReadResult; |
1d9111bd | 196 | |
99666519 LOK |
197 | if (iTimeoutMs > 0) |
198 | { | |
199 | TcpSocketSetBlocking(socket, true); | |
200 | iNow = GetTimeMs(); | |
201 | } | |
202 | } | |
203 | return 0; | |
204 | } | |
1d9111bd | 205 | |
99666519 LOK |
206 | inline bool TcpResolveAddress(const char *strHost, uint16_t iPort, int *iError, struct addrinfo **info) |
207 | { | |
208 | struct addrinfo hints; | |
209 | char service[33]; | |
210 | memset(&hints, 0, sizeof(hints)); | |
211 | hints.ai_family = AF_UNSPEC; | |
212 | hints.ai_socktype = SOCK_STREAM; | |
213 | hints.ai_protocol = IPPROTO_TCP; | |
214 | sprintf(service, "%d", iPort); | |
1d9111bd | 215 | |
99666519 LOK |
216 | *iError = getaddrinfo(strHost, service, &hints, info); |
217 | return !(*iError); | |
218 | } | |
1d9111bd | 219 | |
99666519 LOK |
220 | inline int TcpGetSocketError(tcp_socket_t socket) |
221 | { | |
222 | int iReturn(0); | |
223 | socklen_t optLen = sizeof(tcp_socket_t); | |
224 | getsockopt(socket, SOL_SOCKET, SO_ERROR, (char *)&iReturn, &optLen); | |
225 | return iReturn; | |
226 | } | |
1d9111bd | 227 | |
99666519 LOK |
228 | inline bool TcpSetNoDelay(tcp_socket_t socket) |
229 | { | |
230 | int iSetTo(1); | |
231 | setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&iSetTo, sizeof(iSetTo)); | |
232 | return true; | |
233 | } | |
234 | ||
235 | inline bool TcpConnectSocket(tcp_socket_t socket, struct addrinfo* addr, int *iError, uint64_t iTimeout = 0) | |
236 | { | |
237 | TcpSocketSetBlocking(socket, false); | |
238 | ||
239 | *iError = 0; | |
240 | int iConnectResult = connect(socket, addr->ai_addr, addr->ai_addrlen); | |
241 | if (iConnectResult == -1) | |
242 | { | |
243 | if (GetSocketError() == EINPROGRESS || | |
244 | GetSocketError() == EAGAIN) | |
1d9111bd | 245 | { |
99666519 LOK |
246 | fd_set fd_write, fd_except; |
247 | struct timeval tv; | |
248 | tv.tv_sec = (long)(iTimeout / 1000); | |
249 | tv.tv_usec = 1000 * (long)(iTimeout % 1000); | |
1d9111bd | 250 | |
99666519 LOK |
251 | FD_ZERO(&fd_write); |
252 | FD_ZERO(&fd_except); | |
253 | FD_SET(socket, &fd_write); | |
254 | FD_SET(socket, &fd_except); | |
255 | ||
256 | int iPollResult = select(sizeof(socket)*8, NULL, &fd_write, &fd_except, &tv); | |
257 | if (iPollResult == 0) | |
258 | *iError = ETIMEDOUT; | |
259 | else if (iPollResult == -1) | |
260 | *iError = GetSocketError(); | |
261 | else | |
262 | { | |
263 | socklen_t errlen = sizeof(int); | |
264 | getsockopt(socket, SOL_SOCKET, SO_ERROR, (char *)iError, &errlen); | |
265 | } | |
266 | } | |
267 | else | |
268 | { | |
269 | *iError = errno; | |
270 | } | |
1d9111bd | 271 | } |
71194cf4 | 272 | |
99666519 LOK |
273 | TcpSocketSetBlocking(socket, true); |
274 | ||
275 | return *iError == 0; | |
24048d57 LOK |
276 | } |
277 | } |