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
SyncSocket()18 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {
19 }
20
~SyncSocket()21 SyncSocket::~SyncSocket() {
22 Close();
23 }
24
25 // static
CreatePair(SyncSocket * socket_a,SyncSocket * socket_b)26 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
27 return false;
28 }
29
30 // static
UnwrapHandle(const SyncSocket::TransitDescriptor & descriptor)31 SyncSocket::Handle SyncSocket::UnwrapHandle(
32 const SyncSocket::TransitDescriptor& descriptor) {
33 // TODO(xians): Still unclear how NaCl uses SyncSocket.
34 // See http://crbug.com/409656
35 NOTIMPLEMENTED();
36 return SyncSocket::kInvalidHandle;
37 }
38
PrepareTransitDescriptor(ProcessHandle peer_process_handle,SyncSocket::TransitDescriptor * descriptor)39 bool SyncSocket::PrepareTransitDescriptor(
40 ProcessHandle peer_process_handle,
41 SyncSocket::TransitDescriptor* descriptor) {
42 // TODO(xians): Still unclear how NaCl uses SyncSocket.
43 // See http://crbug.com/409656
44 NOTIMPLEMENTED();
45 return false;
46 }
47
Close()48 bool SyncSocket::Close() {
49 if (handle_ != kInvalidHandle) {
50 if (close(handle_) < 0)
51 DPLOG(ERROR) << "close";
52 handle_ = kInvalidHandle;
53 }
54 return true;
55 }
56
Send(const void * buffer,size_t length)57 size_t SyncSocket::Send(const void* buffer, size_t length) {
58 const ssize_t bytes_written = write(handle_, buffer, length);
59 return bytes_written > 0 ? bytes_written : 0;
60 }
61
Receive(void * buffer,size_t length)62 size_t SyncSocket::Receive(void* buffer, size_t length) {
63 const ssize_t bytes_read = read(handle_, buffer, length);
64 return bytes_read > 0 ? bytes_read : 0;
65 }
66
ReceiveWithTimeout(void * buffer,size_t length,TimeDelta)67 size_t SyncSocket::ReceiveWithTimeout(void* buffer, size_t length, TimeDelta) {
68 NOTIMPLEMENTED();
69 return 0;
70 }
71
Peek()72 size_t SyncSocket::Peek() {
73 NOTIMPLEMENTED();
74 return 0;
75 }
76
CancelableSyncSocket()77 CancelableSyncSocket::CancelableSyncSocket() {
78 }
79
CancelableSyncSocket(Handle handle)80 CancelableSyncSocket::CancelableSyncSocket(Handle handle)
81 : SyncSocket(handle) {
82 }
83
Send(const void * buffer,size_t length)84 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) {
85 return SyncSocket::Send(buffer, length);
86 }
87
Shutdown()88 bool CancelableSyncSocket::Shutdown() {
89 return SyncSocket::Close();
90 }
91
92 // static
CreatePair(CancelableSyncSocket * socket_a,CancelableSyncSocket * socket_b)93 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
94 CancelableSyncSocket* socket_b) {
95 return SyncSocket::CreatePair(socket_a, socket_b);
96 }
97
98 } // namespace base
99