86750c50b9bc4e51d5abe56d8ee8a09f253a4341
[deb_libcec.git] / src / lib / platform / windows / os-socket.h
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 #include <ws2spi.h>
38 #include <ws2ipdef.h>
39 #include <ws2tcpip.h>
40
41 #define SHUT_RDWR SD_BOTH
42
43 #ifndef ETIMEDOUT
44 #define ETIMEDOUT 138
45 #endif
46
47 namespace PLATFORM
48 {
49 #ifndef MSG_WAITALL
50 #define MSG_WAITALL 0x8
51 #endif
52
53 inline int GetSocketError(void)
54 {
55 int error = WSAGetLastError();
56 switch(error)
57 {
58 case WSAEINPROGRESS: return EINPROGRESS;
59 case WSAECONNRESET : return ECONNRESET;
60 case WSAETIMEDOUT : return ETIMEDOUT;
61 case WSAEWOULDBLOCK: return EAGAIN;
62 default : return error;
63 }
64 }
65
66 // Serial port
67 //@{
68 inline void SerialSocketClose(serial_socket_t socket)
69 {
70 if (socket != INVALID_HANDLE_VALUE)
71 CloseHandle(socket);
72 }
73
74 inline ssize_t SerialSocketWrite(serial_socket_t socket, int *iError, void* data, size_t len)
75 {
76 if (len != (DWORD)len)
77 {
78 *iError = EINVAL;
79 return -1;
80 }
81
82 DWORD iBytesWritten(0);
83 if (socket != INVALID_HANDLE_VALUE)
84 {
85 if (!WriteFile(socket, data, (DWORD)len, &iBytesWritten, NULL))
86 {
87 *iError = GetLastError();
88 return -1;
89 }
90 return (ssize_t)iBytesWritten;
91 }
92
93 return -1;
94 }
95
96 inline ssize_t SerialSocketRead(serial_socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/)
97 {
98 *iError = 0;
99
100 if (len != (DWORD)len)
101 {
102 *iError = EINVAL;
103 return -1;
104 }
105
106 DWORD iBytesRead(0);
107 if (socket != INVALID_HANDLE_VALUE)
108 {
109 if(!ReadFile(socket, data, (DWORD)len, &iBytesRead, NULL) != 0)
110 {
111 *iError = GetLastError();
112 return -1;
113 }
114 return (ssize_t)iBytesRead;
115 }
116 return -1;
117 }
118 //@}
119
120 // TCP
121 //@{
122 inline void TcpSocketSetBlocking(tcp_socket_t socket, bool bSetTo)
123 {
124 u_long iSetTo = bSetTo ? 0 : 1;
125 ioctlsocket(socket, FIONBIO, &iSetTo);
126 }
127
128 inline void TcpSocketClose(tcp_socket_t socket)
129 {
130 closesocket(socket);
131 }
132
133 inline void TcpSocketShutdown(tcp_socket_t socket)
134 {
135 if (socket != INVALID_SOCKET &&
136 socket != SOCKET_ERROR)
137 shutdown(socket, SHUT_RDWR);
138 }
139
140 inline ssize_t TcpSocketWrite(tcp_socket_t socket, int *iError, void* data, size_t len)
141 {
142 if (socket == INVALID_SOCKET ||
143 socket == SOCKET_ERROR ||
144 len != (int)len)
145 {
146 *iError = EINVAL;
147 return -1;
148 }
149
150 ssize_t iReturn = send(socket, (char*)data, (int)len, 0);
151 if (iReturn < (ssize_t)len)
152 *iError = GetSocketError();
153 return iReturn;
154 }
155
156 inline ssize_t TcpSocketRead(tcp_socket_t socket, int *iError, void* data, size_t len, uint64_t iTimeoutMs /*= 0*/)
157 {
158 int64_t iNow(0), iTarget(0);
159 ssize_t iBytesRead(0);
160 *iError = 0;
161
162 if (socket == INVALID_SOCKET ||
163 socket == SOCKET_ERROR ||
164 len != (int)len)
165 {
166 *iError = EINVAL;
167 return -1;
168 }
169
170 if (iTimeoutMs > 0)
171 {
172 iNow = GetTimeMs();
173 iTarget = iNow + (int64_t) iTimeoutMs;
174 }
175
176 fd_set fd_read;
177 struct timeval tv;
178 while (iBytesRead >= 0 && iBytesRead < (ssize_t)len && (iTimeoutMs == 0 || iTarget > iNow))
179 {
180 if (iTimeoutMs > 0)
181 {
182 tv.tv_sec = (long)(iTimeoutMs / 1000);
183 tv.tv_usec = 1000 * (long)(iTimeoutMs % 1000);
184
185 FD_ZERO(&fd_read);
186 FD_SET(socket, &fd_read);
187
188 if (select((int)socket + 1, &fd_read, NULL, NULL, &tv) == 0)
189 {
190 *iError = ETIMEDOUT;
191 return ETIMEDOUT;
192 }
193 TcpSocketSetBlocking(socket, false);
194 }
195
196 ssize_t iReadResult = (iTimeoutMs > 0) ?
197 recv(socket, (char*)data + iBytesRead, (int)(len - iBytesRead), 0) :
198 recv(socket, (char*)data, (int)len, MSG_WAITALL);
199 *iError = GetSocketError();
200
201 if (iTimeoutMs > 0)
202 {
203 TcpSocketSetBlocking(socket, true);
204 iNow = GetTimeMs();
205 }
206
207 if (iReadResult < 0)
208 {
209 if (*iError == EAGAIN && iTimeoutMs > 0)
210 continue;
211 return -1;
212 }
213 else if (iReadResult == 0 || (iReadResult != (ssize_t)len && iTimeoutMs == 0))
214 {
215 *iError = ECONNRESET;
216 return -1;
217 }
218
219 iBytesRead += iReadResult;
220 }
221
222 if (iBytesRead < (ssize_t)len && *iError == 0)
223 *iError = ETIMEDOUT;
224
225 return iBytesRead;
226 }
227
228 inline bool TcpResolveAddress(const char *strHost, uint16_t iPort, int *iError, struct addrinfo **info)
229 {
230 struct addrinfo hints;
231 char service[33];
232 memset(&hints, 0, sizeof(hints));
233 hints.ai_family = AF_UNSPEC;
234 hints.ai_socktype = SOCK_STREAM;
235 hints.ai_protocol = IPPROTO_TCP;
236 sprintf(service, "%d", iPort);
237
238 *iError = getaddrinfo(strHost, service, &hints, info);
239 return !(*iError);
240 }
241
242 inline int TcpGetSocketError(tcp_socket_t socket)
243 {
244 int iReturn(0);
245 socklen_t optLen = sizeof(tcp_socket_t);
246 getsockopt(socket, SOL_SOCKET, SO_ERROR, (char *)&iReturn, &optLen);
247 return iReturn;
248 }
249
250 inline bool TcpSetNoDelay(tcp_socket_t socket)
251 {
252 int iSetTo(1);
253 setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&iSetTo, sizeof(iSetTo));
254 return true;
255 }
256
257 inline bool TcpConnectSocket(tcp_socket_t socket, struct addrinfo* addr, int *iError, uint64_t iTimeout = 0)
258 {
259 TcpSocketSetBlocking(socket, false);
260
261 *iError = 0;
262 int iConnectResult = connect(socket, addr->ai_addr, (int)addr->ai_addrlen);
263 if (iConnectResult == -1)
264 {
265 if (GetSocketError() == EINPROGRESS ||
266 GetSocketError() == EAGAIN)
267 {
268 fd_set fd_write, fd_except;
269 struct timeval tv;
270 tv.tv_sec = (long)(iTimeout / 1000);
271 tv.tv_usec = 1000 * (long)(iTimeout % 1000);
272
273 FD_ZERO(&fd_write);
274 FD_ZERO(&fd_except);
275 FD_SET(socket, &fd_write);
276 FD_SET(socket, &fd_except);
277
278 int iPollResult = select(sizeof(socket)*8, NULL, &fd_write, &fd_except, &tv);
279 if (iPollResult == 0)
280 *iError = ETIMEDOUT;
281 else if (iPollResult == -1)
282 *iError = GetSocketError();
283 else
284 {
285 socklen_t errlen = sizeof(int);
286 getsockopt(socket, SOL_SOCKET, SO_ERROR, (char *)iError, &errlen);
287 }
288 }
289 else
290 {
291 *iError = GetSocketError();
292 }
293 }
294
295 TcpSocketSetBlocking(socket, true);
296
297 return *iError == 0;
298 }
299 }