OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
7 | 7 |
8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
9 #include <stdio.h> // NOLINT | 9 #include <stdio.h> // NOLINT |
10 #include <stdlib.h> // NOLINT | 10 #include <stdlib.h> // NOLINT |
(...skipping 16 matching lines...) Expand all Loading... |
27 struct hostent* server; | 27 struct hostent* server; |
28 struct sockaddr_in server_address; | 28 struct sockaddr_in server_address; |
29 | 29 |
30 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 30 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
31 if (fd < 0) { | 31 if (fd < 0) { |
32 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 32 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
33 return -1; | 33 return -1; |
34 } | 34 } |
35 | 35 |
36 FDUtils::SetCloseOnExec(fd); | 36 FDUtils::SetCloseOnExec(fd); |
37 FDUtils::SetNonBlocking(fd); | 37 Socket::SetNonBlocking(fd); |
38 | 38 |
39 server = gethostbyname(host); | 39 server = gethostbyname(host); |
40 if (server == NULL) { | 40 if (server == NULL) { |
41 TEMP_FAILURE_RETRY(close(fd)); | 41 TEMP_FAILURE_RETRY(close(fd)); |
42 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 42 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
43 return -1; | 43 return -1; |
44 } | 44 } |
45 | 45 |
46 server_address.sin_family = AF_INET; | 46 server_address.sin_family = AF_INET; |
47 server_address.sin_port = htons(port); | 47 server_address.sin_port = htons(port); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 TEMP_FAILURE_RETRY(close(fd)); | 211 TEMP_FAILURE_RETRY(close(fd)); |
212 Log::PrintErr("Error Bind: %s\n", strerror(errno)); | 212 Log::PrintErr("Error Bind: %s\n", strerror(errno)); |
213 return -1; | 213 return -1; |
214 } | 214 } |
215 | 215 |
216 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { | 216 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { |
217 Log::PrintErr("Error Listen: %s\n", strerror(errno)); | 217 Log::PrintErr("Error Listen: %s\n", strerror(errno)); |
218 return -1; | 218 return -1; |
219 } | 219 } |
220 | 220 |
221 FDUtils::SetNonBlocking(fd); | 221 Socket::SetNonBlocking(fd); |
222 return fd; | 222 return fd; |
223 } | 223 } |
224 | 224 |
225 | 225 |
226 static bool IsTemporaryAcceptError(int error) { | 226 static bool IsTemporaryAcceptError(int error) { |
227 // On Android a number of protocol errors should be treated as EAGAIN. | 227 // On Android a number of protocol errors should be treated as EAGAIN. |
228 // These are the ones for TCP/IP. | 228 // These are the ones for TCP/IP. |
229 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || | 229 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || |
230 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || | 230 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || |
231 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || | 231 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || |
232 (error == ENETUNREACH); | 232 (error == ENETUNREACH); |
233 } | 233 } |
234 | 234 |
235 | 235 |
236 intptr_t ServerSocket::Accept(intptr_t fd) { | 236 intptr_t ServerSocket::Accept(intptr_t fd) { |
237 intptr_t socket; | 237 intptr_t socket; |
238 struct sockaddr clientaddr; | 238 struct sockaddr clientaddr; |
239 socklen_t addrlen = sizeof(clientaddr); | 239 socklen_t addrlen = sizeof(clientaddr); |
240 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 240 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
241 if (socket == -1) { | 241 if (socket == -1) { |
242 if (IsTemporaryAcceptError(errno)) { | 242 if (IsTemporaryAcceptError(errno)) { |
243 // We need to signal to the caller that this is actually not an | 243 // We need to signal to the caller that this is actually not an |
244 // error. We got woken up from the poll on the listening socket, | 244 // error. We got woken up from the poll on the listening socket, |
245 // but there is no connection ready to be accepted. | 245 // but there is no connection ready to be accepted. |
246 ASSERT(kTemporaryFailure != -1); | 246 ASSERT(kTemporaryFailure != -1); |
247 socket = kTemporaryFailure; | 247 socket = kTemporaryFailure; |
248 } | 248 } |
249 } else { | 249 } else { |
250 FDUtils::SetNonBlocking(socket); | 250 Socket::SetNonBlocking(socket); |
251 } | 251 } |
252 return socket; | 252 return socket; |
253 } | 253 } |
254 | 254 |
255 | 255 |
256 void Socket::Close(intptr_t fd) { | 256 void Socket::Close(intptr_t fd) { |
257 ASSERT(fd >= 0); | 257 ASSERT(fd >= 0); |
258 int err = TEMP_FAILURE_RETRY(close(fd)); | 258 int err = TEMP_FAILURE_RETRY(close(fd)); |
259 if (err != 0) { | 259 if (err != 0) { |
260 const int kBufferSize = 1024; | 260 const int kBufferSize = 1024; |
261 char error_message[kBufferSize]; | 261 char error_message[kBufferSize]; |
262 strerror_r(errno, error_message, kBufferSize); | 262 strerror_r(errno, error_message, kBufferSize); |
263 Log::PrintErr("%s\n", error_message); | 263 Log::PrintErr("%s\n", error_message); |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
| 267 |
| 268 bool Socket::SetNonBlocking(intptr_t fd) { |
| 269 return FDUtils::SetNonBlocking(fd); |
| 270 } |
| 271 |
| 272 |
| 273 bool Socket::SetBlocking(intptr_t fd) { |
| 274 return FDUtils::SetBlocking(fd); |
| 275 } |
| 276 |
267 #endif // defined(TARGET_OS_ANDROID) | 277 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |