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/
36 #include "../util/timeutils.h"
42 inline void SocketClose(socket_t socket
)
44 if (socket
!= INVALID_SOCKET
)
48 inline void SocketSetBlocking(socket_t socket
, bool bSetTo
)
51 // fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK) == 0 :
52 // fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK) == 0;
53 fcntl(socket
, F_SETFL
, bSetTo
? FNDELAY
: 0);
56 inline int64_t SocketWrite(socket_t socket
, int *iError
, uint8_t* data
, uint32_t len
)
66 int64_t iBytesWritten
= 0;
69 while (iBytesWritten
< len
)
72 FD_SET(socket
, &port
);
73 int returnv
= select(socket
+ 1, NULL
, &port
, NULL
, tv
);
79 else if (returnv
== 0)
85 returnv
= write(socket
, data
+ iBytesWritten
, len
- iBytesWritten
);
91 iBytesWritten
+= returnv
;
97 inline int32_t SocketRead(socket_t socket
, int *iError
, uint8_t* data
, uint32_t len
, uint64_t iTimeoutMs
/*= 0*/)
100 struct timeval timeout
, *tv
;
101 int64_t iNow(0), iTarget(0);
102 int32_t iBytesRead
= 0;
114 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
117 while (iBytesRead
< (int32_t) len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
125 timeout
.tv_sec
= ((long int)iTarget
- (long int)iNow
) / (long int)1000.;
126 timeout
.tv_usec
= ((long int)iTarget
- (long int)iNow
) % (long int)1000.;
131 FD_SET(socket
, &port
);
132 int32_t returnv
= select(socket
+ 1, &port
, NULL
, NULL
, tv
);
139 else if (returnv
== 0)
141 break; //nothing to read
144 returnv
= read(socket
, data
+ iBytesRead
, len
- iBytesRead
);
151 iBytesRead
+= returnv
;