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>
46 /* Needed on Mac OS/X */
48 #define SOL_TCP IPPROTO_TCP
55 inline void SocketClose(socket_t socket
)
57 if (socket
!= INVALID_SOCKET_VALUE
)
61 inline void SocketSetBlocking(socket_t socket
, bool bSetTo
)
63 if (socket
!= INVALID_SOCKET_VALUE
)
66 fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) & ~O_NONBLOCK
);
68 fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) | O_NONBLOCK
);
72 inline ssize_t
SocketWrite(socket_t socket
, int *iError
, void* data
, size_t len
)
76 if (socket
== INVALID_SOCKET_VALUE
)
82 ssize_t
iBytesWritten(0);
83 struct timeval
*tv(NULL
);
85 while (iBytesWritten
< (ssize_t
)len
)
88 FD_SET(socket
, &port
);
89 int returnv
= select(socket
+ 1, NULL
, &port
, NULL
, tv
);
95 else if (returnv
== 0)
101 returnv
= write(socket
, (char*)data
+ iBytesWritten
, len
- iBytesWritten
);
107 iBytesWritten
+= returnv
;
110 return iBytesWritten
;
113 inline ssize_t
SocketRead(socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
116 struct timeval timeout
, *tv
;
117 int64_t iNow(0), iTarget(0);
118 ssize_t
iBytesRead(0);
121 if (socket
== INVALID_SOCKET_VALUE
)
130 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
133 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
141 timeout
.tv_sec
= ((long int)iTarget
- (long int)iNow
) / (long int)1000.;
142 timeout
.tv_usec
= ((long int)iTarget
- (long int)iNow
) % (long int)1000.;
147 FD_SET(socket
, &port
);
148 int32_t returnv
= select(socket
+ 1, &port
, NULL
, NULL
, tv
);
155 else if (returnv
== 0)
157 break; //nothing to read
160 returnv
= read(socket
, (char*)data
+ iBytesRead
, len
- iBytesRead
);
167 iBytesRead
+= returnv
;
179 inline void TcpSocketClose(tcp_socket_t socket
)
184 inline void TcpSocketShutdown(tcp_socket_t socket
)
186 if (socket
!= INVALID_SOCKET_VALUE
)
187 shutdown(socket
, SHUT_RDWR
);
190 inline ssize_t
TcpSocketWrite(tcp_socket_t socket
, int *iError
, void* data
, size_t len
)
192 if (socket
== INVALID_SOCKET_VALUE
)
198 ssize_t iReturn
= send(socket
, data
, len
, 0);
199 if (iReturn
< (ssize_t
)len
)
204 inline ssize_t
TcpSocketRead(tcp_socket_t socket
, int *iError
, void* data
, size_t len
, uint64_t iTimeoutMs
/*= 0*/)
206 int64_t iNow(0), iTarget(0);
207 ssize_t
iBytesRead(0);
210 if (socket
== INVALID_SOCKET_VALUE
)
219 iTarget
= iNow
+ (int64_t) iTimeoutMs
;
227 while (iBytesRead
>= 0 && iBytesRead
< (ssize_t
)len
&& (iTimeoutMs
== 0 || iTarget
> iNow
))
231 int iPollResult
= poll(&fds
, 1, iTarget
- iNow
);
232 if (iPollResult
== 0)
239 ssize_t iReadResult
= (iTimeoutMs
> 0) ?
240 recv(socket
, (char*)data
+ iBytesRead
, len
- iBytesRead
, MSG_DONTWAIT
) :
241 recv(socket
, data
, len
, MSG_WAITALL
);
244 if (errno
== EAGAIN
&& iTimeoutMs
> 0)
249 else if (iReadResult
== 0 || (iReadResult
!= (ssize_t
)len
&& iTimeoutMs
== 0))
251 *iError
= ECONNRESET
;
255 iBytesRead
+= iReadResult
;
261 if (iBytesRead
< (ssize_t
)len
)
266 inline bool TcpResolveAddress(const char *strHost
, uint16_t iPort
, int *iError
, struct addrinfo
**info
)
268 struct addrinfo hints
;
270 memset(&hints
, 0, sizeof(hints
));
271 hints
.ai_family
= AF_UNSPEC
;
272 hints
.ai_socktype
= SOCK_STREAM
;
273 hints
.ai_protocol
= IPPROTO_TCP
;
274 sprintf(service
, "%d", iPort
);
276 *iError
= getaddrinfo(strHost
, service
, &hints
, info
);
280 inline int TcpGetSocketError(tcp_socket_t socket
)
283 socklen_t optLen
= sizeof(socket_t
);
284 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (void *)&iReturn
, &optLen
);
288 inline bool TcpSetNoDelay(tcp_socket_t socket
)
291 setsockopt(socket
, SOL_TCP
, TCP_NODELAY
, &iSetTo
, sizeof(iSetTo
));
295 inline bool TcpConnectSocket(tcp_socket_t socket
, struct addrinfo
* addr
, int *iError
, uint64_t iTimeout
= 0)
298 int iConnectResult
= connect(socket
, addr
->ai_addr
, addr
->ai_addrlen
);
299 if (iConnectResult
== -1)
301 if (errno
== EINPROGRESS
)
305 pfd
.events
= POLLOUT
;
308 int iPollResult
= poll(&pfd
, 1, iTimeout
);
309 if (iPollResult
== 0)
311 else if (iPollResult
== -1)
314 socklen_t errlen
= sizeof(int);
315 getsockopt(socket
, SOL_SOCKET
, SO_ERROR
, (void *)iError
, &errlen
);