0b11c590420e566f00e8c886a76d79a646715d8f
3 * This file is part of the libCEC(R) library.
5 * libCEC(R) is Copyright (C) 2011 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
;
67 inline void SocketClose(socket_t socket
)
69 if (socket
!= SOCKET_ERROR
&& socket
!= INVALID_SOCKET
)
73 inline void SocketSetBlocking(socket_t socket
, bool bSetTo
)
75 u_long nVal
= bSetTo
? 1 : 0;
76 ioctlsocket(socket
, FIONBIO
, &nVal
);
79 inline int64_t SocketWrite(socket_t socket
, int *iError
, uint8_t* data
, uint32_t len
)
82 if (socket
!= SOCKET_ERROR
&& socket
!= INVALID_SOCKET
)
84 iReturn
= send(socket
, (char*)data
, len
, 0);
86 *iError
= GetSocketError();
91 inline int SocketReadFixed(socket_t socket
, char *buf
, int iLength
, int iFlags
)
96 if ((iFlags
& MSG_WAITALL
) == 0)
97 return recv(socket
, buf
, iLength
, iFlags
);
99 iFlags
&= ~MSG_WAITALL
;
100 while(iLength
> 0 && nRes
> 0)
102 nRes
= recv(socket
, buf
, iLength
, iFlags
);
112 inline int32_t SocketRead(socket_t socket
, void *buf
, uint32_t nLen
)
114 int x
= SocketReadFixed(socket
, (char *)buf
, nLen
, MSG_WAITALL
);
117 return GetSocketError();
124 inline int32_t SocketRead(socket_t socket
, int *iError
, uint8_t* data
, uint32_t iLength
, uint64_t iTimeoutMs
)
126 int x
, tot
= 0, nErr
;
133 while(tot
!= (int)iLength
)
135 tv
.tv_sec
= (long)(iTimeoutMs
/ 1000);
136 tv
.tv_usec
= (long)(1000 * (iTimeoutMs
% 1000));
139 FD_SET(socket
, &fd_read
);
141 x
= select(socket
+ 1, &fd_read
, NULL
, NULL
, &tv
);
146 SocketSetBlocking(socket
, false);
148 x
= SocketReadFixed(socket
, (char *)data
+ tot
, iLength
- tot
, 0);
149 nErr
= GetSocketError();
151 SocketSetBlocking(socket
, true);