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"
41 #define SHUT_RDWR SD_BOTH
50 #define MSG_WAITALL 0x8
53 inline int GetSocketError(void)
55 int error
= WSAGetLastError();
58 case WSAEINPROGRESS
: return EINPROGRESS
;
59 case WSAECONNRESET
: return ECONNRESET
;
60 case WSAETIMEDOUT
: return ETIMEDOUT
;
61 case WSAEWOULDBLOCK
: return EAGAIN
;
62 default : return error
;
68 inline void SerialSocketClose(serial_socket_t socket
)
70 if (socket
!= INVALID_HANDLE_VALUE
)
74 inline ssize_t
SerialSocketWrite(serial_socket_t socket
, int *iError
, void* data
, size_t len
)
76 if (len
!= (DWORD
)len
)
82 DWORD
iBytesWritten(0);
83 if (socket
!= INVALID_HANDLE_VALUE
)
85 if (!WriteFile(socket
, data
, (DWORD
)len
, &iBytesWritten
, NULL
))
87 *iError
= GetLastError();
90 return (ssize_t
)iBytesWritten
;
96 inline ssize_t
SerialSocketRead(serial_socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
98 if (len
!= (DWORD
)len
)
105 if (socket
!= INVALID_HANDLE_VALUE
)
107 if(!ReadFile(socket
, data
, (DWORD
)len
, &iBytesRead
, NULL
) != 0)
109 *iError
= GetLastError();
112 return (ssize_t
)iBytesRead
;
120 inline void TcpSocketSetBlocking(tcp_socket_t socket
, bool bSetTo
)
122 u_long iSetTo
= bSetTo
? 0 : 1;
123 ioctlsocket(socket
, FIONBIO
, &iSetTo
);
126 inline void TcpSocketClose(tcp_socket_t socket
)
131 inline void TcpSocketShutdown(tcp_socket_t socket
)
133 if (socket
!= INVALID_SOCKET
&&
134 socket
!= SOCKET_ERROR
)
135 shutdown(socket
, SHUT_RDWR
);
138 inline ssize_t
TcpSocketWrite(tcp_socket_t socket
, int *iError
, void* data
, size_t len
)
140 if (socket
== INVALID_SOCKET
||
141 socket
== SOCKET_ERROR
||
148 ssize_t iReturn
= send(socket
, (char*)data
, (int)len
, 0);
149 if (iReturn
< (ssize_t
)len
)
150 *iError
= GetSocketError();
154 inline ssize_t
TcpSocketRead(tcp_socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
156 int64_t iNow(0), iTarget(0);
157 ssize_t
iBytesRead(0);
160 if (socket
== INVALID_SOCKET
||
161 socket
== SOCKET_ERROR
||
171 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
176 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
180 tv
.tv_sec
= (long)(iTimeoutMs
/ 1000);
181 tv
.tv_usec
= 1000 * (long)(iTimeoutMs
% 1000);
184 FD_SET(socket
, &fd_read
);
186 if (select((int)socket
+ 1, &fd_read
, NULL
, NULL
, &tv
) == 0)
191 TcpSocketSetBlocking(socket
, false);
194 ssize_t iReadResult
= (iTimeoutMs
> 0) ?
195 recv(socket
, (char*)data
+ iBytesRead
, (int)(len
- iBytesRead
), 0) :
196 recv(socket
, (char*)data
, (int)len
, MSG_WAITALL
);
197 *iError
= GetSocketError();
201 TcpSocketSetBlocking(socket
, true);
207 if (*iError
== EAGAIN
&& iTimeoutMs
> 0)
211 else if (iReadResult
== 0 || (iReadResult
!= (ssize_t
)len
&& iTimeoutMs
== 0))
213 *iError
= ECONNRESET
;
217 iBytesRead
+= iReadResult
;
220 if (iBytesRead
< (ssize_t
)len
&& *iError
== 0)
226 inline bool TcpResolveAddress(const char *strHost
, uint16_t iPort
, int *iError
, struct addrinfo
**info
)
228 struct addrinfo hints
;
230 memset(&hints
, 0, sizeof(hints
));
231 hints
.ai_family
= AF_UNSPEC
;
232 hints
.ai_socktype
= SOCK_STREAM
;
233 hints
.ai_protocol
= IPPROTO_TCP
;
234 sprintf(service
, "%d", iPort
);
236 *iError
= getaddrinfo(strHost
, service
, &hints
, info
);
240 inline int TcpGetSocketError(tcp_socket_t socket
)
243 socklen_t optLen
= sizeof(tcp_socket_t
);
244 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (char *)&iReturn
, &optLen
);
248 inline bool TcpSetNoDelay(tcp_socket_t socket
)
251 setsockopt(socket
, IPPROTO_TCP
, TCP_NODELAY
, (const char*)&iSetTo
, sizeof(iSetTo
));
255 inline bool TcpConnectSocket(tcp_socket_t socket
, struct addrinfo
* addr
, int *iError
, uint64_t iTimeout
= 0)
257 TcpSocketSetBlocking(socket
, false);
260 int iConnectResult
= connect(socket
, addr
->ai_addr
, (int)addr
->ai_addrlen
);
261 if (iConnectResult
== -1)
263 if (GetSocketError() == EINPROGRESS
||
264 GetSocketError() == EAGAIN
)
266 fd_set fd_write
, fd_except
;
268 tv
.tv_sec
= (long)(iTimeout
/ 1000);
269 tv
.tv_usec
= 1000 * (long)(iTimeout
% 1000);
273 FD_SET(socket
, &fd_write
);
274 FD_SET(socket
, &fd_except
);
276 int iPollResult
= select(sizeof(socket
)*8, NULL
, &fd_write
, &fd_except
, &tv
);
277 if (iPollResult
== 0)
279 else if (iPollResult
== -1)
280 *iError
= GetSocketError();
283 socklen_t errlen
= sizeof(int);
284 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (char *)iError
, &errlen
);
289 *iError
= GetSocketError();
293 TcpSocketSetBlocking(socket
, true);