| 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 | #include "platform/globals.h" |
| 6 | #if defined(HOST_OS_LINUX) |
| 7 | |
| 8 | #include "bin/sync_socket.h" |
| 9 | |
| 10 | #include <errno.h> // NOLINT |
| 11 | |
| 12 | #include "bin/fdutils.h" |
| 13 | #include "bin/socket_base.h" |
| 14 | #include "platform/signal_blocker.h" |
| 15 | |
| 16 | namespace dart { |
| 17 | namespace bin { |
| 18 | |
| 19 | bool SynchronousSocket::Initialize() { |
| 20 | // Nothing to do on Linux. |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | static intptr_t Create(const RawAddr& addr) { |
| 25 | intptr_t fd; |
| 26 | intptr_t type = SOCK_STREAM | SOCK_CLOEXEC; |
| 27 | fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, type, 0)); |
| 28 | if (fd < 0) { |
| 29 | return -1; |
| 30 | } |
| 31 | return fd; |
| 32 | } |
| 33 | |
| 34 | static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
| 35 | intptr_t result = TEMP_FAILURE_RETRY( |
| 36 | connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
| 37 | if (result == 0) { |
| 38 | return fd; |
| 39 | } |
| 40 | ASSERT(errno != EINPROGRESS); |
| 41 | FDUtils::SaveErrorAndClose(fd); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | intptr_t SynchronousSocket::CreateConnect(const RawAddr& addr) { |
| 46 | intptr_t fd = Create(addr); |
| 47 | if (fd < 0) { |
| 48 | return fd; |
| 49 | } |
| 50 | return Connect(fd, addr); |
| 51 | } |
| 52 | |
| 53 | intptr_t SynchronousSocket::Available(intptr_t fd) { |
| 54 | return SocketBase::Available(fd); |
| 55 | } |
| 56 | |
| 57 | intptr_t SynchronousSocket::GetPort(intptr_t fd) { |
| 58 | return SocketBase::GetPort(fd); |
| 59 | } |
| 60 | |
| 61 | SocketAddress* SynchronousSocket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| 62 | return SocketBase::GetRemotePeer(fd, port); |
| 63 | } |
| 64 | |
| 65 | intptr_t SynchronousSocket::Read(intptr_t fd, |
| 66 | void* buffer, |
| 67 | intptr_t num_bytes) { |
| 68 | return SocketBase::Read(fd, buffer, num_bytes, SocketBase::kSync); |
| 69 | } |
| 70 | |
| 71 | intptr_t SynchronousSocket::Write(intptr_t fd, |
| 72 | const void* buffer, |
| 73 | intptr_t num_bytes) { |
| 74 | return SocketBase::Write(fd, buffer, num_bytes, SocketBase::kSync); |
| 75 | } |
| 76 | |
| 77 | void SynchronousSocket::ShutdownRead(intptr_t fd) { |
| 78 | VOID_NO_RETRY_EXPECTED(shutdown(fd, SHUT_RD)); |
| 79 | } |
| 80 | |
| 81 | void SynchronousSocket::ShutdownWrite(intptr_t fd) { |
| 82 | VOID_NO_RETRY_EXPECTED(shutdown(fd, SHUT_WR)); |
| 83 | } |
| 84 | |
| 85 | void SynchronousSocket::Close(intptr_t fd) { |
| 86 | return SocketBase::Close(fd); |
| 87 | } |
| 88 | |
| 89 | } // namespace bin |
| 90 | } // namespace dart |
| 91 | |
| 92 | #endif // defined(HOST_OS_LINUX) |
| 93 | |