OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 #if !defined(DART_IO_DISABLED) |
| 6 |
| 7 #include "platform/globals.h" |
| 8 #if defined(HOST_OS_LINUX) |
| 9 |
| 10 #include "bin/sync_socket.h" |
| 11 |
| 12 #include <errno.h> // NOLINT |
| 13 |
| 14 #include "bin/fdutils.h" |
| 15 #include "platform/signal_blocker.h" |
| 16 |
| 17 namespace dart { |
| 18 namespace bin { |
| 19 |
| 20 SynchronousSocket::SynchronousSocket(intptr_t fd) : fd_(fd) {} |
| 21 |
| 22 |
| 23 void SynchronousSocket::SetClosedFd() { |
| 24 fd_ = kClosedFd; |
| 25 } |
| 26 |
| 27 |
| 28 bool SynchronousSocket::Initialize() { |
| 29 // Nothing to do on Linux. |
| 30 return true; |
| 31 } |
| 32 |
| 33 |
| 34 static intptr_t Create(const RawAddr& addr) { |
| 35 intptr_t fd; |
| 36 intptr_t type = SOCK_STREAM | SOCK_CLOEXEC; |
| 37 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, type, 0)); |
| 38 if (fd < 0) { |
| 39 return -1; |
| 40 } |
| 41 return fd; |
| 42 } |
| 43 |
| 44 |
| 45 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
| 46 intptr_t result = TEMP_FAILURE_RETRY( |
| 47 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 48 if (result == 0) { |
| 49 return fd; |
| 50 } |
| 51 ASSERT(errno != EINPROGRESS); |
| 52 FDUtils::FDUtils::SaveErrorAndClose(fd); |
| 53 return -1; |
| 54 } |
| 55 |
| 56 |
| 57 intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) { |
| 58 intptr_t fd = Create(addr); |
| 59 if (fd < 0) { |
| 60 return fd; |
| 61 } |
| 62 return Connect(fd, addr); |
| 63 } |
| 64 |
| 65 |
| 66 void SynchronousSocket::ShutdownRead(intptr_t fd) { |
| 67 VOID_NO_RETRY_EXPECTED(shutdown(fd, SHUT_RD)); |
| 68 } |
| 69 |
| 70 |
| 71 void SynchronousSocket::ShutdownWrite(intptr_t fd) { |
| 72 VOID_NO_RETRY_EXPECTED(shutdown(fd, SHUT_WR)); |
| 73 } |
| 74 |
| 75 } // namespace bin |
| 76 } // namespace dart |
| 77 |
| 78 #endif // defined(HOST_OS_LINUX) |
| 79 |
| 80 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |