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> 9 #include <stdio.h> 10 #include <sys/types.h> 11 12 #include "base/logging.h" 13 14 namespace base { 15 16 const SyncSocket::Handle SyncSocket::kInvalidHandle = -1; 17 18 SyncSocket::SyncSocket() : handle_(kInvalidHandle) { 19 } 20 21 SyncSocket::~SyncSocket() { 22 Close(); 23 } 24 25 // static 26 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { 27 return false; 28 } 29 30 bool SyncSocket::Close() { 31 if (handle_ != kInvalidHandle) { 32 if (close(handle_) < 0) 33 DPLOG(ERROR) << "close"; 34 handle_ = kInvalidHandle; 35 } 36 return true; 37 } 38 39 size_t SyncSocket::Send(const void* buffer, size_t length) { 40 const ssize_t bytes_written = write(handle_, buffer, length); 41 return bytes_written > 0 ? bytes_written : 0; 42 } 43 44 size_t SyncSocket::Receive(void* buffer, size_t length) { 45 const ssize_t bytes_read = read(handle_, buffer, length); 46 return bytes_read > 0 ? bytes_read : 0; 47 } 48 49 size_t SyncSocket::ReceiveWithTimeout(void* buffer, size_t length, TimeDelta) { 50 NOTIMPLEMENTED(); 51 return 0; 52 } 53 54 size_t SyncSocket::Peek() { 55 NOTIMPLEMENTED(); 56 return 0; 57 } 58 59 CancelableSyncSocket::CancelableSyncSocket() { 60 } 61 62 CancelableSyncSocket::CancelableSyncSocket(Handle handle) 63 : SyncSocket(handle) { 64 } 65 66 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { 67 return SyncSocket::Send(buffer, length); 68 } 69 70 bool CancelableSyncSocket::Shutdown() { 71 return SyncSocket::Close(); 72 } 73 74 // static 75 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, 76 CancelableSyncSocket* socket_b) { 77 return SyncSocket::CreatePair(socket_a, socket_b); 78 } 79 80 } // namespace base 81