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/
36 #include "../util/timeutils.h"
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <arpa/inet.h>
50 inline void SocketClose(socket_t socket
)
52 if (socket
!= INVALID_SOCKET_VALUE
)
56 inline void SocketSetBlocking(socket_t socket
, bool bSetTo
)
58 if (socket
!= INVALID_SOCKET_VALUE
)
61 fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) & ~O_NONBLOCK
);
63 fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) | O_NONBLOCK
);
67 inline ssize_t
SocketWrite(socket_t socket
, int *iError
, void* data
, size_t len
)
71 if (socket
== INVALID_SOCKET_VALUE
)
77 ssize_t
iBytesWritten(0);
78 struct timeval
*tv(NULL
);
80 while (iBytesWritten
< (ssize_t
)len
)
83 FD_SET(socket
, &port
);
84 int returnv
= select(socket
+ 1, NULL
, &port
, NULL
, tv
);
90 else if (returnv
== 0)
96 returnv
= write(socket
, (char*)data
+ iBytesWritten
, len
- iBytesWritten
);
102 iBytesWritten
+= returnv
;
105 return iBytesWritten
;
108 inline ssize_t
SocketRead(socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
111 struct timeval timeout
, *tv
;
112 int64_t iNow(0), iTarget(0);
113 ssize_t
iBytesRead(0);
116 if (socket
== INVALID_SOCKET_VALUE
)
125 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
128 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
136 timeout
.tv_sec
= ((long int)iTarget
- (long int)iNow
) / (long int)1000.;
137 timeout
.tv_usec
= ((long int)iTarget
- (long int)iNow
) % (long int)1000.;
142 FD_SET(socket
, &port
);
143 int32_t returnv
= select(socket
+ 1, &port
, NULL
, NULL
, tv
);
150 else if (returnv
== 0)
152 break; //nothing to read
155 returnv
= read(socket
, (char*)data
+ iBytesRead
, len
- iBytesRead
);
162 iBytesRead
+= returnv
;
174 inline void TcpSocketClose(tcp_socket_t socket
)
179 inline void TcpSocketShutdown(tcp_socket_t socket
)
181 if (socket
!= INVALID_SOCKET_VALUE
)
182 shutdown(socket
, SHUT_RDWR
);
185 inline ssize_t
TcpSocketWrite(tcp_socket_t socket
, int *iError
, void* data
, size_t len
)
187 if (socket
== INVALID_SOCKET_VALUE
)
193 ssize_t iReturn
= send(socket
, data
, len
, 0);
194 if (iReturn
< (ssize_t
)len
)
199 inline ssize_t
TcpSocketRead(tcp_socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
201 int64_t iNow(0), iTarget(0);
202 ssize_t
iBytesRead(0);
205 if (socket
== INVALID_SOCKET_VALUE
)
214 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
222 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
226 int iPollResult
= poll(&fds
, 1, iTarget
- iNow
);
227 if (iPollResult
== 0)
234 ssize_t iReadResult
= (iTimeoutMs
> 0) ?
235 recv(socket
, (char*)data
+ iBytesRead
, len
- iBytesRead
, MSG_DONTWAIT
) :
236 recv(socket
, data
, len
, MSG_WAITALL
);
239 if (errno
== EAGAIN
&& iTimeoutMs
> 0)
244 else if (iReadResult
== 0 || (iReadResult
!= (ssize_t
)len
&& iTimeoutMs
== 0))
246 *iError
= ECONNRESET
;
250 iBytesRead
+= iReadResult
;
256 if (iBytesRead
< (ssize_t
)len
)
261 inline bool TcpResolveAddress(const char *strHost
, uint16_t iPort
, int *iError
, struct addrinfo
**info
)
263 struct addrinfo hints
;
265 memset(&hints
, 0, sizeof(hints
));
266 hints
.ai_family
= AF_UNSPEC
;
267 hints
.ai_socktype
= SOCK_STREAM
;
268 hints
.ai_protocol
= IPPROTO_TCP
;
269 sprintf(service
, "%d", iPort
);
271 *iError
= getaddrinfo(strHost
, service
, &hints
, info
);
275 inline int TcpGetSocketError(tcp_socket_t socket
)
278 socklen_t optLen
= sizeof(socket_t
);
279 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (void *)&iReturn
, &optLen
);
283 inline bool TcpSetNoDelay(tcp_socket_t socket
)
286 setsockopt(socket
, SOL_TCP
, TCP_NODELAY
, &iSetTo
, sizeof(iSetTo
));
290 inline bool TcpConnectSocket(tcp_socket_t socket
, struct addrinfo
* addr
, int *iError
, uint64_t iTimeout
= 0)
293 int iConnectResult
= connect(socket
, addr
->ai_addr
, addr
->ai_addrlen
);
294 if (iConnectResult
== -1)
296 if (errno
== EINPROGRESS
)
300 pfd
.events
= POLLOUT
;
303 int iPollResult
= poll(&pfd
, 1, iTimeout
);
304 if (iPollResult
== 0)
306 else if (iPollResult
== -1)
309 socklen_t errlen
= sizeof(int);
310 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (void *)iError
, &errlen
);