bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sync_socket.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <limits.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include <stddef.h> |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <sys/types.h> |
| 12 | |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 15 | namespace base { |
| 16 | |
| 17 | const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; |
| 18 | |
| 19 | SyncSocket::SyncSocket() : handle_(kInvalidHandle) { |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | SyncSocket::~SyncSocket() { |
dalecurtis@chromium.org | c124bdb | 2013-10-21 20:09:52 | [diff] [blame] | 23 | Close(); |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // static |
| 27 | bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 28 | return false; |
| 29 | } |
| 30 | |
burnik | 3d67005 | 2014-09-08 06:58:22 | [diff] [blame] | 31 | // static |
| 32 | SyncSocket::Handle SyncSocket::UnwrapHandle( |
| 33 | const SyncSocket::TransitDescriptor& descriptor) { |
| 34 | // TODO(xians): Still unclear how NaCl uses SyncSocket. |
| 35 | // See http://crbug.com/409656 |
| 36 | NOTIMPLEMENTED(); |
| 37 | return SyncSocket::kInvalidHandle; |
| 38 | } |
| 39 | |
| 40 | bool SyncSocket::PrepareTransitDescriptor( |
| 41 | ProcessHandle peer_process_handle, |
| 42 | SyncSocket::TransitDescriptor* descriptor) { |
| 43 | // TODO(xians): Still unclear how NaCl uses SyncSocket. |
| 44 | // See http://crbug.com/409656 |
| 45 | NOTIMPLEMENTED(); |
| 46 | return false; |
| 47 | } |
| 48 | |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 49 | bool SyncSocket::Close() { |
bbudge@chromium.org | 5cfa522 | 2012-07-14 14:06:06 | [diff] [blame] | 50 | if (handle_ != kInvalidHandle) { |
| 51 | if (close(handle_) < 0) |
| 52 | DPLOG(ERROR) << "close"; |
dalecurtis@chromium.org | c124bdb | 2013-10-21 20:09:52 | [diff] [blame] | 53 | handle_ = kInvalidHandle; |
bbudge@chromium.org | 5cfa522 | 2012-07-14 14:06:06 | [diff] [blame] | 54 | } |
| 55 | return true; |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | size_t SyncSocket::Send(const void* buffer, size_t length) { |
dalecurtis@chromium.org | c124bdb | 2013-10-21 20:09:52 | [diff] [blame] | 59 | const ssize_t bytes_written = write(handle_, buffer, length); |
| 60 | return bytes_written > 0 ? bytes_written : 0; |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | size_t SyncSocket::Receive(void* buffer, size_t length) { |
dalecurtis@chromium.org | c124bdb | 2013-10-21 20:09:52 | [diff] [blame] | 64 | const ssize_t bytes_read = read(handle_, buffer, length); |
| 65 | return bytes_read > 0 ? bytes_read : 0; |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 66 | } |
| 67 | |
dalecurtis@chromium.org | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 68 | size_t SyncSocket::ReceiveWithTimeout(void* buffer, size_t length, TimeDelta) { |
| 69 | NOTIMPLEMENTED(); |
| 70 | return 0; |
| 71 | } |
| 72 | |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 73 | size_t SyncSocket::Peek() { |
dalecurtis@chromium.org | c124bdb | 2013-10-21 20:09:52 | [diff] [blame] | 74 | NOTIMPLEMENTED(); |
| 75 | return 0; |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 76 | } |
| 77 | |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 78 | SyncSocket::Handle SyncSocket::Release() { |
| 79 | Handle r = handle_; |
| 80 | handle_ = kInvalidHandle; |
| 81 | return r; |
| 82 | } |
| 83 | |
bbudge@chromium.org | 5cfa522 | 2012-07-14 14:06:06 | [diff] [blame] | 84 | CancelableSyncSocket::CancelableSyncSocket() { |
| 85 | } |
| 86 | |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 87 | CancelableSyncSocket::CancelableSyncSocket(Handle handle) |
| 88 | : SyncSocket(handle) { |
| 89 | } |
| 90 | |
bbudge@chromium.org | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 91 | size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { |
dalecurtis@chromium.org | 38b57545 | 2013-10-23 03:03:18 | [diff] [blame] | 92 | return SyncSocket::Send(buffer, length); |
bbudge@chromium.org | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 93 | } |
| 94 | |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 95 | bool CancelableSyncSocket::Shutdown() { |
dalecurtis@chromium.org | 38b57545 | 2013-10-23 03:03:18 | [diff] [blame] | 96 | return SyncSocket::Close(); |
bbudge@chromium.org | d0bac29 | 2012-02-29 16:10:45 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // static |
| 100 | bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, |
| 101 | CancelableSyncSocket* socket_b) { |
| 102 | return SyncSocket::CreatePair(socket_a, socket_b); |
| 103 | } |
| 104 | |
| 105 | } // namespace base |