OLD | NEW |
---|---|
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
8 #if defined(HOST_OS_FUCHSIA) | 8 #if defined(HOST_OS_FUCHSIA) |
9 | 9 |
10 #include "bin/socket_base.h" | 10 #include "bin/socket_base.h" |
11 | 11 |
12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
13 #include <fcntl.h> // NOLINT | 13 #include <fcntl.h> // NOLINT |
14 #include <ifaddrs.h> // NOLINT | 14 #include <ifaddrs.h> // NOLINT |
15 #include <net/if.h> // NOLINT | 15 #include <net/if.h> // NOLINT |
16 #include <netinet/tcp.h> // NOLINT | 16 #include <netinet/tcp.h> // NOLINT |
17 #include <stdio.h> // NOLINT | 17 #include <stdio.h> // NOLINT |
18 #include <stdlib.h> // NOLINT | 18 #include <stdlib.h> // NOLINT |
19 #include <string.h> // NOLINT | 19 #include <string.h> // NOLINT |
20 #include <sys/ioctl.h> // NOLINT | 20 #include <sys/ioctl.h> // NOLINT |
21 #include <sys/stat.h> // NOLINT | 21 #include <sys/stat.h> // NOLINT |
22 #include <unistd.h> // NOLINT | 22 #include <unistd.h> // NOLINT |
23 | 23 |
24 #include "bin/eventhandler.h" | |
24 #include "bin/fdutils.h" | 25 #include "bin/fdutils.h" |
25 #include "bin/file.h" | 26 #include "bin/file.h" |
26 #include "bin/socket_base_fuchsia.h" | 27 #include "bin/socket_base_fuchsia.h" |
27 #include "platform/signal_blocker.h" | 28 #include "platform/signal_blocker.h" |
28 | 29 |
29 // #define SOCKET_LOG_INFO 1 | 30 // #define SOCKET_LOG_INFO 1 |
30 // #define SOCKET_LOG_ERROR 1 | 31 // #define SOCKET_LOG_ERROR 1 |
31 | 32 |
32 // define SOCKET_LOG_ERROR to get log messages only for errors. | 33 // define SOCKET_LOG_ERROR to get log messages only for errors. |
33 // define SOCKET_LOG_INFO to get log messages for both information and errors. | 34 // define SOCKET_LOG_INFO to get log messages for both information and errors. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 81 } |
81 | 82 |
82 | 83 |
83 bool SocketBase::IsBindError(intptr_t error_number) { | 84 bool SocketBase::IsBindError(intptr_t error_number) { |
84 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 85 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
85 error_number == EINVAL; | 86 error_number == EINVAL; |
86 } | 87 } |
87 | 88 |
88 | 89 |
89 intptr_t SocketBase::Available(intptr_t fd) { | 90 intptr_t SocketBase::Available(intptr_t fd) { |
90 intptr_t available = FDUtils::AvailableBytes(fd); | 91 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
siva
2017/06/21 00:48:10
ASSERT(handle->fd() >= 0);
zra
2017/06/21 06:43:39
Done.
| |
91 LOG_INFO("SocketBase::Available(%ld) = %ld\n", fd, available); | 92 intptr_t available = FDUtils::AvailableBytes(handle->fd()); |
93 LOG_INFO("SocketBase::Available(%ld) = %ld\n", handle->fd(), available); | |
92 return available; | 94 return available; |
93 } | 95 } |
94 | 96 |
95 | 97 |
96 intptr_t SocketBase::Read(intptr_t fd, | 98 intptr_t SocketBase::Read(intptr_t fd, |
97 void* buffer, | 99 void* buffer, |
98 intptr_t num_bytes, | 100 intptr_t num_bytes, |
99 SocketOpKind sync) { | 101 SocketOpKind sync) { |
100 ASSERT(fd >= 0); | 102 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
101 LOG_INFO("SocketBase::Read: calling read(%ld, %p, %ld)\n", fd, buffer, | 103 ASSERT(handle->fd() >= 0); |
102 num_bytes); | 104 LOG_INFO("SocketBase::Read: calling read(%ld, %p, %ld)\n", handle->fd(), |
103 ssize_t read_bytes = NO_RETRY_EXPECTED(read(fd, buffer, num_bytes)); | 105 buffer, num_bytes); |
106 intptr_t read_bytes = handle->Read(buffer, num_bytes); | |
104 ASSERT(EAGAIN == EWOULDBLOCK); | 107 ASSERT(EAGAIN == EWOULDBLOCK); |
105 if ((sync == kAsync) && (read_bytes == -1) && (errno == EWOULDBLOCK)) { | 108 if ((sync == kAsync) && (read_bytes == -1) && (errno == EWOULDBLOCK)) { |
106 // If the read would block we need to retry and therefore return 0 | 109 // If the read would block we need to retry and therefore return 0 |
107 // as the number of bytes written. | 110 // as the number of bytes written. |
108 read_bytes = 0; | 111 read_bytes = 0; |
109 } else if (read_bytes == -1) { | 112 } else if (read_bytes == -1) { |
110 LOG_ERR("SocketBase::Read: read(%ld, %p, %ld) failed\n", fd, buffer, | 113 LOG_ERR("SocketBase::Read: read(%ld, %p, %ld) failed\n", handle->fd(), |
111 num_bytes); | 114 buffer, num_bytes); |
112 } else { | 115 } else { |
113 LOG_INFO("SocketBase::Read: read(%ld, %p, %ld) succeeded\n", fd, buffer, | 116 LOG_INFO("SocketBase::Read: read(%ld, %p, %ld) succeeded\n", handle->fd(), |
114 num_bytes); | 117 buffer, num_bytes); |
115 } | 118 } |
116 return read_bytes; | 119 return read_bytes; |
117 } | 120 } |
118 | 121 |
119 | 122 |
120 intptr_t SocketBase::RecvFrom(intptr_t fd, | 123 intptr_t SocketBase::RecvFrom(intptr_t fd, |
121 void* buffer, | 124 void* buffer, |
122 intptr_t num_bytes, | 125 intptr_t num_bytes, |
123 RawAddr* addr, | 126 RawAddr* addr, |
124 SocketOpKind sync) { | 127 SocketOpKind sync) { |
125 LOG_ERR("SocketBase::RecvFrom is unimplemented\n"); | 128 LOG_ERR("SocketBase::RecvFrom is unimplemented\n"); |
126 UNIMPLEMENTED(); | 129 UNIMPLEMENTED(); |
127 return -1; | 130 return -1; |
128 } | 131 } |
129 | 132 |
130 | 133 |
131 intptr_t SocketBase::Write(intptr_t fd, | 134 intptr_t SocketBase::Write(intptr_t fd, |
132 const void* buffer, | 135 const void* buffer, |
133 intptr_t num_bytes, | 136 intptr_t num_bytes, |
134 SocketOpKind sync) { | 137 SocketOpKind sync) { |
135 ASSERT(fd >= 0); | 138 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
136 LOG_INFO("SocketBase::Write: calling write(%ld, %p, %ld)\n", fd, buffer, | 139 ASSERT(handle->fd() >= 0); |
137 num_bytes); | 140 LOG_INFO("SocketBase::Write: calling write(%ld, %p, %ld)\n", handle->fd(), |
138 ssize_t written_bytes = NO_RETRY_EXPECTED(write(fd, buffer, num_bytes)); | 141 buffer, num_bytes); |
142 intptr_t written_bytes = handle->Write(buffer, num_bytes); | |
139 ASSERT(EAGAIN == EWOULDBLOCK); | 143 ASSERT(EAGAIN == EWOULDBLOCK); |
140 if ((sync == kAsync) && (written_bytes == -1) && (errno == EWOULDBLOCK)) { | 144 if ((sync == kAsync) && (written_bytes == -1) && (errno == EWOULDBLOCK)) { |
141 // If the would block we need to retry and therefore return 0 as | 145 // If the would block we need to retry and therefore return 0 as |
142 // the number of bytes written. | 146 // the number of bytes written. |
143 written_bytes = 0; | 147 written_bytes = 0; |
144 } else if (written_bytes == -1) { | 148 } else if (written_bytes == -1) { |
145 LOG_ERR("SocketBase::Write: write(%ld, %p, %ld) failed\n", fd, buffer, | 149 LOG_ERR("SocketBase::Write: write(%ld, %p, %ld) failed\n", handle->fd(), |
146 num_bytes); | 150 buffer, num_bytes); |
147 } else { | 151 } else { |
148 LOG_INFO("SocketBase::Write: write(%ld, %p, %ld) succeeded\n", fd, buffer, | 152 LOG_INFO("SocketBase::Write: write(%ld, %p, %ld) succeeded\n", handle->fd(), |
149 num_bytes); | 153 buffer, num_bytes); |
150 } | 154 } |
151 return written_bytes; | 155 return written_bytes; |
152 } | 156 } |
153 | 157 |
154 | 158 |
155 intptr_t SocketBase::SendTo(intptr_t fd, | 159 intptr_t SocketBase::SendTo(intptr_t fd, |
156 const void* buffer, | 160 const void* buffer, |
157 intptr_t num_bytes, | 161 intptr_t num_bytes, |
158 const RawAddr& addr, | 162 const RawAddr& addr, |
159 SocketOpKind sync) { | 163 SocketOpKind sync) { |
160 LOG_ERR("SocketBase::SendTo is unimplemented\n"); | 164 LOG_ERR("SocketBase::SendTo is unimplemented\n"); |
161 UNIMPLEMENTED(); | 165 UNIMPLEMENTED(); |
162 return -1; | 166 return -1; |
163 } | 167 } |
164 | 168 |
165 | 169 |
166 intptr_t SocketBase::GetPort(intptr_t fd) { | 170 intptr_t SocketBase::GetPort(intptr_t fd) { |
167 ASSERT(fd >= 0); | 171 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
172 ASSERT(handle->fd() >= 0); | |
168 RawAddr raw; | 173 RawAddr raw; |
169 socklen_t size = sizeof(raw); | 174 socklen_t size = sizeof(raw); |
170 LOG_INFO("SocketBase::GetPort: calling getsockname(%ld)\n", fd); | 175 LOG_INFO("SocketBase::GetPort: calling getsockname(%ld)\n", handle->fd()); |
171 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 176 if (NO_RETRY_EXPECTED(getsockname(handle->fd(), &raw.addr, &size))) { |
172 return 0; | 177 return 0; |
173 } | 178 } |
174 return SocketAddress::GetAddrPort(raw); | 179 return SocketAddress::GetAddrPort(raw); |
175 } | 180 } |
176 | 181 |
177 | 182 |
178 SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) { | 183 SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) { |
179 ASSERT(fd >= 0); | 184 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
185 ASSERT(handle->fd() >= 0); | |
180 RawAddr raw; | 186 RawAddr raw; |
181 socklen_t size = sizeof(raw); | 187 socklen_t size = sizeof(raw); |
182 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 188 if (NO_RETRY_EXPECTED(getpeername(handle->fd(), &raw.addr, &size))) { |
183 return NULL; | 189 return NULL; |
184 } | 190 } |
185 *port = SocketAddress::GetAddrPort(raw); | 191 *port = SocketAddress::GetAddrPort(raw); |
186 return new SocketAddress(&raw.addr); | 192 return new SocketAddress(&raw.addr); |
187 } | 193 } |
188 | 194 |
189 | 195 |
190 void SocketBase::GetError(intptr_t fd, OSError* os_error) { | 196 void SocketBase::GetError(intptr_t fd, OSError* os_error) { |
191 LOG_ERR("SocketBase::GetError is unimplemented\n"); | 197 LOG_ERR("SocketBase::GetError is unimplemented\n"); |
192 UNIMPLEMENTED(); | 198 UNIMPLEMENTED(); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 | 288 |
283 AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces( | 289 AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces( |
284 int type, | 290 int type, |
285 OSError** os_error) { | 291 OSError** os_error) { |
286 UNIMPLEMENTED(); | 292 UNIMPLEMENTED(); |
287 return NULL; | 293 return NULL; |
288 } | 294 } |
289 | 295 |
290 | 296 |
291 void SocketBase::Close(intptr_t fd) { | 297 void SocketBase::Close(intptr_t fd) { |
292 ASSERT(fd >= 0); | 298 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
293 NO_RETRY_EXPECTED(close(fd)); | 299 ASSERT(handle->fd() >= 0); |
300 NO_RETRY_EXPECTED(close(handle->fd())); | |
294 } | 301 } |
295 | 302 |
296 | 303 |
297 bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) { | 304 bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) { |
298 LOG_ERR("SocketBase::GetNoDelay is unimplemented\n"); | 305 LOG_ERR("SocketBase::GetNoDelay is unimplemented\n"); |
299 UNIMPLEMENTED(); | 306 UNIMPLEMENTED(); |
300 return false; | 307 return false; |
301 } | 308 } |
302 | 309 |
303 | 310 |
304 bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) { | 311 bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) { |
312 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); | |
305 int on = enabled ? 1 : 0; | 313 int on = enabled ? 1 : 0; |
306 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 314 return NO_RETRY_EXPECTED(setsockopt(handle->fd(), IPPROTO_TCP, TCP_NODELAY, |
307 reinterpret_cast<char*>(&on), | 315 reinterpret_cast<char*>(&on), |
308 sizeof(on))) == 0; | 316 sizeof(on))) == 0; |
309 } | 317 } |
310 | 318 |
311 | 319 |
312 bool SocketBase::GetMulticastLoop(intptr_t fd, | 320 bool SocketBase::GetMulticastLoop(intptr_t fd, |
313 intptr_t protocol, | 321 intptr_t protocol, |
314 bool* enabled) { | 322 bool* enabled) { |
315 LOG_ERR("SocketBase::GetMulticastLoop is unimplemented\n"); | 323 LOG_ERR("SocketBase::GetMulticastLoop is unimplemented\n"); |
316 UNIMPLEMENTED(); | 324 UNIMPLEMENTED(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 UNIMPLEMENTED(); | 381 UNIMPLEMENTED(); |
374 return false; | 382 return false; |
375 } | 383 } |
376 | 384 |
377 } // namespace bin | 385 } // namespace bin |
378 } // namespace dart | 386 } // namespace dart |
379 | 387 |
380 #endif // defined(HOST_OS_FUCHSIA) | 388 #endif // defined(HOST_OS_FUCHSIA) |
381 | 389 |
382 #endif // !defined(DART_IO_DISABLED) | 390 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |