posix: fixed lib/platform/sockets/tcp.h compilation and some bugs. added CTcpSocket...
[deb_libcec.git] / src / lib / platform / posix / os-socket.h
CommitLineData
ba65909d
LOK
1#pragma once
2/*
3 * This file is part of the libCEC(R) library.
4 *
9b53a148 5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
ba65909d
LOK
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
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.
14 *
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.
19 *
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.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
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/
32 */
33
34
35#include "../os.h"
36#include "../util/timeutils.h"
37#include <stdio.h>
38#include <fcntl.h>
39
40namespace PLATFORM
41{
42 inline void SocketClose(socket_t socket)
43 {
44 if (socket != INVALID_SOCKET)
45 close(socket);
46 }
47
48 inline void SocketSetBlocking(socket_t socket, bool bSetTo)
49 {
50// return 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);
54 }
55
56 inline int64_t SocketWrite(socket_t socket, int *iError, uint8_t* data, uint32_t len)
57 {
58 fd_set port;
59
60 if (socket == -1)
61 {
62 *iError = EINVAL;
63 return -1;
64 }
65
66 int64_t iBytesWritten = 0;
cb2397e9 67 struct timeval *tv(NULL);
ba65909d
LOK
68
69 while (iBytesWritten < len)
70 {
71 FD_ZERO(&port);
72 FD_SET(socket, &port);
73 int returnv = select(socket + 1, NULL, &port, NULL, tv);
74 if (returnv < 0)
75 {
76 *iError = errno;
77 return -1;
78 }
79 else if (returnv == 0)
80 {
81 *iError = ETIMEDOUT;
82 return -1;
83 }
84
85 returnv = write(socket, data + iBytesWritten, len - iBytesWritten);
86 if (returnv == -1)
87 {
88 *iError = errno;
89 return -1;
90 }
91 iBytesWritten += returnv;
92 }
93
94 return iBytesWritten;
95 }
96
97 inline int32_t SocketRead(socket_t socket, int *iError, uint8_t* data, uint32_t len, uint64_t iTimeoutMs /*= 0*/)
98 {
99 fd_set port;
100 struct timeval timeout, *tv;
101 int64_t iNow(0), iTarget(0);
102 int32_t iBytesRead = 0;
103 *iError = 0;
104
105 if (socket == -1)
106 {
107 *iError = EINVAL;
108 return -1;
109 }
110
111 if (iTimeoutMs > 0)
112 {
113 iNow = GetTimeMs();
114 iTarget = iNow + (int64_t) iTimeoutMs;
115 }
116
117 while (iBytesRead < (int32_t) len && (iTimeoutMs == 0 || iTarget > iNow))
118 {
119 if (iTimeoutMs == 0)
120 {
121 tv = NULL;
122 }
123 else
124 {
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.;
127 tv = &timeout;
128 }
129
130 FD_ZERO(&port);
131 FD_SET(socket, &port);
132 int32_t returnv = select(socket + 1, &port, NULL, NULL, tv);
133
134 if (returnv == -1)
135 {
136 *iError = errno;
137 return -1;
138 }
139 else if (returnv == 0)
140 {
141 break; //nothing to read
142 }
143
144 returnv = read(socket, data + iBytesRead, len - iBytesRead);
145 if (returnv == -1)
146 {
147 *iError = errno;
148 return -1;
149 }
150
151 iBytesRead += returnv;
152
153 if (iTimeoutMs > 0)
154 iNow = GetTimeMs();
155 }
156
157 return iBytesRead;
158 }
159}