3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
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/
35 #include "../util/timeutils.h"
37 #pragma comment(lib, "Ws2_32.lib")
42 #define SHUT_RDWR SD_BOTH
51 #define MSG_WAITALL 0x8
54 inline int GetSocketError(void)
56 int error
= WSAGetLastError();
59 case WSAEINPROGRESS
: return EINPROGRESS
;
60 case WSAECONNRESET
: return ECONNRESET
;
61 case WSAETIMEDOUT
: return ETIMEDOUT
;
62 case WSAEWOULDBLOCK
: return EAGAIN
;
63 default : return error
;
69 inline void SerialSocketClose(serial_socket_t socket
)
71 if (socket
!= INVALID_HANDLE_VALUE
)
75 inline ssize_t
SerialSocketWrite(serial_socket_t socket
, int *iError
, void* data
, size_t len
)
77 DWORD
iBytesWritten(0);
78 if (socket
!= INVALID_HANDLE_VALUE
)
80 if (!WriteFile(socket
, data
, len
, &iBytesWritten
, NULL
))
82 *iError
= GetLastError();
85 return (ssize_t
)iBytesWritten
;
91 inline ssize_t
SerialSocketRead(serial_socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
94 if (socket
!= INVALID_HANDLE_VALUE
)
96 if(!ReadFile(socket
, data
, len
, &iBytesRead
, NULL
) != 0)
98 *iError
= GetLastError();
101 return (ssize_t
)iBytesRead
;
109 inline void TcpSocketSetBlocking(tcp_socket_t socket
, bool bSetTo
)
111 u_long iSetTo
= bSetTo
? 0 : 1;
112 ioctlsocket(socket
, FIONBIO
, &iSetTo
);
115 inline void TcpSocketClose(tcp_socket_t socket
)
120 inline void TcpSocketShutdown(tcp_socket_t socket
)
122 if (socket
!= INVALID_SOCKET
&&
123 socket
!= SOCKET_ERROR
)
124 shutdown(socket
, SHUT_RDWR
);
127 inline ssize_t
TcpSocketWrite(tcp_socket_t socket
, int *iError
, void* data
, size_t len
)
129 if (socket
== INVALID_SOCKET
||
130 socket
== SOCKET_ERROR
)
136 ssize_t iReturn
= send(socket
, (char*)data
, len
, 0);
137 if (iReturn
< (ssize_t
)len
)
142 inline ssize_t
TcpSocketRead(tcp_socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
144 int64_t iNow(0), iTarget(0);
145 ssize_t
iBytesRead(0);
148 if (socket
== INVALID_SOCKET
||
149 socket
== SOCKET_ERROR
)
158 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
163 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
167 tv
.tv_sec
= (long)(iTimeoutMs
/ 1000);
168 tv
.tv_usec
= 1000 * (long)(iTimeoutMs
% 1000);
171 FD_SET(socket
, &fd_read
);
173 if (select(socket
+ 1, &fd_read
, NULL
, NULL
, &tv
) == 0)
175 TcpSocketSetBlocking(socket
, false);
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();
184 if (errno
== EAGAIN
&& iTimeoutMs
> 0)
189 else if (iReadResult
== 0 || (iReadResult
!= (ssize_t
)len
&& iTimeoutMs
== 0))
191 *iError
= ECONNRESET
;
195 iBytesRead
+= iReadResult
;
199 TcpSocketSetBlocking(socket
, true);
206 inline bool TcpResolveAddress(const char *strHost
, uint16_t iPort
, int *iError
, struct addrinfo
**info
)
208 struct addrinfo hints
;
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
);
216 *iError
= getaddrinfo(strHost
, service
, &hints
, info
);
220 inline int TcpGetSocketError(tcp_socket_t socket
)
223 socklen_t optLen
= sizeof(tcp_socket_t
);
224 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (char *)&iReturn
, &optLen
);
228 inline bool TcpSetNoDelay(tcp_socket_t socket
)
231 setsockopt(socket
, IPPROTO_TCP
, TCP_NODELAY
, (const char*)&iSetTo
, sizeof(iSetTo
));
235 inline bool TcpConnectSocket(tcp_socket_t socket
, struct addrinfo
* addr
, int *iError
, uint64_t iTimeout
= 0)
237 TcpSocketSetBlocking(socket
, false);
240 int iConnectResult
= connect(socket
, addr
->ai_addr
, addr
->ai_addrlen
);
241 if (iConnectResult
== -1)
243 if (GetSocketError() == EINPROGRESS
||
244 GetSocketError() == EAGAIN
)
246 fd_set fd_write
, fd_except
;
248 tv
.tv_sec
= (long)(iTimeout
/ 1000);
249 tv
.tv_usec
= 1000 * (long)(iTimeout
% 1000);
253 FD_SET(socket
, &fd_write
);
254 FD_SET(socket
, &fd_except
);
256 int iPollResult
= select(sizeof(socket
)*8, NULL
, &fd_write
, &fd_except
, &tv
);
257 if (iPollResult
== 0)
259 else if (iPollResult
== -1)
260 *iError
= GetSocketError();
263 socklen_t errlen
= sizeof(int);
264 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (char *)iError
, &errlen
);
273 TcpSocketSetBlocking(socket
, true);