| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
| 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 18 matching lines...) Expand all Loading... |
| 29 struct hostent* server; | 29 struct hostent* server; |
| 30 struct sockaddr_in server_address; | 30 struct sockaddr_in server_address; |
| 31 | 31 |
| 32 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 32 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 33 if (fd < 0) { | 33 if (fd < 0) { |
| 34 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 34 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 35 return -1; | 35 return -1; |
| 36 } | 36 } |
| 37 | 37 |
| 38 FDUtils::SetCloseOnExec(fd); | 38 FDUtils::SetCloseOnExec(fd); |
| 39 FDUtils::SetNonBlocking(fd); | 39 Socket::SetNonBlocking(fd); |
| 40 | 40 |
| 41 server = gethostbyname(host); | 41 server = gethostbyname(host); |
| 42 if (server == NULL) { | 42 if (server == NULL) { |
| 43 VOID_TEMP_FAILURE_RETRY(close(fd)); | 43 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 44 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 44 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 45 return -1; | 45 return -1; |
| 46 } | 46 } |
| 47 | 47 |
| 48 server_address.sin_family = AF_INET; | 48 server_address.sin_family = AF_INET; |
| 49 server_address.sin_port = htons(port); | 49 server_address.sin_port = htons(port); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 sizeof(server_address))) < 0) { | 219 sizeof(server_address))) < 0) { |
| 220 VOID_TEMP_FAILURE_RETRY(close(fd)); | 220 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 221 return -1; | 221 return -1; |
| 222 } | 222 } |
| 223 | 223 |
| 224 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 224 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 225 TEMP_FAILURE_RETRY(close(fd)); | 225 TEMP_FAILURE_RETRY(close(fd)); |
| 226 return -1; | 226 return -1; |
| 227 } | 227 } |
| 228 | 228 |
| 229 FDUtils::SetNonBlocking(fd); | 229 Socket::SetNonBlocking(fd); |
| 230 return fd; | 230 return fd; |
| 231 } | 231 } |
| 232 | 232 |
| 233 | 233 |
| 234 intptr_t ServerSocket::Accept(intptr_t fd) { | 234 intptr_t ServerSocket::Accept(intptr_t fd) { |
| 235 intptr_t socket; | 235 intptr_t socket; |
| 236 struct sockaddr clientaddr; | 236 struct sockaddr clientaddr; |
| 237 socklen_t addrlen = sizeof(clientaddr); | 237 socklen_t addrlen = sizeof(clientaddr); |
| 238 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 238 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
| 239 if (socket == -1) { | 239 if (socket == -1) { |
| 240 if (errno == EAGAIN) { | 240 if (errno == EAGAIN) { |
| 241 // We need to signal to the caller that this is actually not an | 241 // We need to signal to the caller that this is actually not an |
| 242 // error. We got woken up from the poll on the listening socket, | 242 // error. We got woken up from the poll on the listening socket, |
| 243 // but there is no connection ready to be accepted. | 243 // but there is no connection ready to be accepted. |
| 244 ASSERT(kTemporaryFailure != -1); | 244 ASSERT(kTemporaryFailure != -1); |
| 245 socket = kTemporaryFailure; | 245 socket = kTemporaryFailure; |
| 246 } | 246 } |
| 247 } else { | 247 } else { |
| 248 FDUtils::SetNonBlocking(socket); | 248 Socket::SetNonBlocking(socket); |
| 249 } | 249 } |
| 250 return socket; | 250 return socket; |
| 251 } | 251 } |
| 252 | 252 |
| 253 | 253 |
| 254 void Socket::Close(intptr_t fd) { | 254 void Socket::Close(intptr_t fd) { |
| 255 ASSERT(fd >= 0); | 255 ASSERT(fd >= 0); |
| 256 int err = TEMP_FAILURE_RETRY(close(fd)); | 256 int err = TEMP_FAILURE_RETRY(close(fd)); |
| 257 if (err != 0) { | 257 if (err != 0) { |
| 258 const int kBufferSize = 1024; | 258 const int kBufferSize = 1024; |
| 259 char error_message[kBufferSize]; | 259 char error_message[kBufferSize]; |
| 260 strerror_r(errno, error_message, kBufferSize); | 260 strerror_r(errno, error_message, kBufferSize); |
| 261 Log::PrintErr("%s\n", error_message); | 261 Log::PrintErr("%s\n", error_message); |
| 262 } | 262 } |
| 263 } | 263 } |
| 264 | 264 |
| 265 |
| 266 bool Socket::SetNonBlocking(intptr_t fd) { |
| 267 return FDUtils::SetNonBlocking(fd); |
| 268 } |
| 269 |
| 270 |
| 271 bool Socket::SetBlocking(intptr_t fd) { |
| 272 return FDUtils::SetBlocking(fd); |
| 273 } |
| 274 |
| 265 #endif // defined(TARGET_OS_MACOS) | 275 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |