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.
5 #ifndef BASE_SYNC_SOCKET_H_
6 #define BASE_SYNC_SOCKET_H_
8 // A socket abstraction used for sending and receiving plain
9 // data. Because the receiving is blocking, they can be used to perform
10 // rudimentary cross-process synchronization with low latency.
14 #include "base/base_export.h"
15 #include "base/compiler_specific.h"
16 #include "base/files/platform_file.h"
17 #include "base/macros.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "base/time/time.h"
20 #include "build/build_config.h"
25 #include <sys/types.h>
27 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
28 #include "base/file_descriptor_posix.h"
33 class BASE_EXPORT SyncSocket {
35 using Handle = PlatformFile;
36 using ScopedHandle = ScopedPlatformFile;
37 static const Handle kInvalidHandle;
41 // Creates a SyncSocket from a Handle.
42 explicit SyncSocket(Handle handle);
43 explicit SyncSocket(ScopedHandle handle);
44 virtual ~SyncSocket();
46 // Initializes and connects a pair of sockets.
47 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful
48 // return, the sockets will both be valid and connected.
49 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
51 // Closes the SyncSocket.
54 // Sends the message to the remote peer of the SyncSocket.
55 // Note it is not safe to send messages from the same socket handle by
56 // multiple threads simultaneously.
57 // buffer is a pointer to the data to send.
58 // length is the length of the data to send (must be non-zero).
59 // Returns the number of bytes sent, or 0 upon failure.
60 virtual size_t Send(const void* buffer, size_t length);
62 // Receives a message from an SyncSocket.
63 // buffer is a pointer to the buffer to receive data.
64 // length is the number of bytes of data to receive (must be non-zero).
65 // Returns the number of bytes received, or 0 upon failure.
66 virtual size_t Receive(void* buffer, size_t length);
68 // Same as Receive() but only blocks for data until |timeout| has elapsed or
69 // |buffer| |length| is exhausted. Currently only timeouts less than one
70 // second are allowed. Return the amount of data read.
71 virtual size_t ReceiveWithTimeout(void* buffer,
75 // Returns the number of bytes available. If non-zero, Receive() will not
76 // not block when called.
77 virtual size_t Peek();
79 // Returns true if the Handle is valid, and false if it is not.
82 // Extracts the contained handle. Used for transferring between
84 Handle handle() const;
86 // Extracts and takes ownership of the contained handle.
94 DISALLOW_COPY_AND_ASSIGN(SyncSocket);
97 // Derives from SyncSocket and adds support for shutting down the socket from
98 // another thread while a blocking Receive or Send is being done from the
99 // thread that owns the socket.
100 class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
102 CancelableSyncSocket();
103 explicit CancelableSyncSocket(Handle handle);
104 explicit CancelableSyncSocket(ScopedHandle handle);
105 ~CancelableSyncSocket() override = default;
107 // Initializes a pair of cancelable sockets. See documentation for
108 // SyncSocket::CreatePair for more details.
109 static bool CreatePair(CancelableSyncSocket* socket_a,
110 CancelableSyncSocket* socket_b);
112 // A way to shut down a socket even if another thread is currently performing
113 // a blocking Receive or Send.
117 // Since the Linux and Mac implementations actually use a socket, shutting
118 // them down from another thread is pretty simple - we can just call
119 // shutdown(). However, the Windows implementation relies on named pipes
120 // and there isn't a way to cancel a blocking synchronous Read that is
121 // supported on <Vista. So, for Windows only, we override these
122 // SyncSocket methods in order to support shutting down the 'socket'.
123 void Close() override;
124 size_t Receive(void* buffer, size_t length) override;
125 size_t ReceiveWithTimeout(void* buffer,
127 TimeDelta timeout) override;
130 // Send() is overridden to catch cases where the remote end is not responding
131 // and we fill the local socket buffer. When the buffer is full, this
132 // implementation of Send() will not block indefinitely as
133 // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
134 // Note that the socket will not be closed in this case.
135 size_t Send(const void* buffer, size_t length) override;
139 WaitableEvent shutdown_event_;
140 WaitableEvent file_operation_;
142 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
147 #endif // BASE_SYNC_SOCKET_H_