1 // Copyright 2012 The Chromium Authors
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 <stddef.h>
10 #include <stdio.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #include "base/containers/span.h"
15 #include "base/notimplemented.h"
16
17 namespace base {
18
19 // static
CreatePair(SyncSocket * socket_a,SyncSocket * socket_b)20 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) {
21 return false;
22 }
23
Close()24 void SyncSocket::Close() {
25 handle_.reset();
26 }
27
Send(span<const uint8_t> data)28 size_t SyncSocket::Send(span<const uint8_t> data) {
29 const ssize_t bytes_written = write(handle(), data.data(), data.size());
30 return bytes_written > 0 ? static_cast<size_t>(bytes_written) : 0;
31 }
32
Receive(span<uint8_t> buffer)33 size_t SyncSocket::Receive(span<uint8_t> buffer) {
34 const ssize_t bytes_read = read(handle(), buffer.data(), buffer.size());
35 return bytes_read > 0 ? static_cast<size_t>(bytes_read) : 0;
36 }
37
ReceiveWithTimeout(span<uint8_t> buffer,TimeDelta timeout)38 size_t SyncSocket::ReceiveWithTimeout(span<uint8_t> buffer, TimeDelta timeout) {
39 NOTIMPLEMENTED();
40 return 0;
41 }
42
Peek()43 size_t SyncSocket::Peek() {
44 NOTIMPLEMENTED();
45 return 0;
46 }
47
IsValid() const48 bool SyncSocket::IsValid() const {
49 return handle_.is_valid();
50 }
51
handle() const52 SyncSocket::Handle SyncSocket::handle() const {
53 return handle_.get();
54 }
55
Release()56 SyncSocket::Handle SyncSocket::Release() {
57 return handle_.release();
58 }
59
Send(span<const uint8_t> data)60 size_t CancelableSyncSocket::Send(span<const uint8_t> data) {
61 return SyncSocket::Send(data);
62 }
63
Shutdown()64 bool CancelableSyncSocket::Shutdown() {
65 Close();
66 return true;
67 }
68
69 // static
CreatePair(CancelableSyncSocket * socket_a,CancelableSyncSocket * socket_b)70 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a,
71 CancelableSyncSocket* socket_b) {
72 return SyncSocket::CreatePair(socket_a, socket_b);
73 }
74
75 } // namespace base
76