xref: /aosp_15_r20/external/libchrome/base/sync_socket.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_SYNC_SOCKET_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_SYNC_SOCKET_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker // A socket abstraction used for sending and receiving plain
9*635a8641SAndroid Build Coastguard Worker // data.  Because the receiving is blocking, they can be used to perform
10*635a8641SAndroid Build Coastguard Worker // rudimentary cross-process synchronization with low latency.
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/waitable_event.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
20*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
23*635a8641SAndroid Build Coastguard Worker #include <windows.h>
24*635a8641SAndroid Build Coastguard Worker #endif
25*635a8641SAndroid Build Coastguard Worker #include <sys/types.h>
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
28*635a8641SAndroid Build Coastguard Worker #include "base/file_descriptor_posix.h"
29*635a8641SAndroid Build Coastguard Worker #endif
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker namespace base {
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT SyncSocket {
34*635a8641SAndroid Build Coastguard Worker  public:
35*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
36*635a8641SAndroid Build Coastguard Worker   typedef HANDLE Handle;
37*635a8641SAndroid Build Coastguard Worker   typedef Handle TransitDescriptor;
38*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
39*635a8641SAndroid Build Coastguard Worker   typedef int Handle;
40*635a8641SAndroid Build Coastguard Worker   typedef FileDescriptor TransitDescriptor;
41*635a8641SAndroid Build Coastguard Worker #endif
42*635a8641SAndroid Build Coastguard Worker   static const Handle kInvalidHandle;
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker   SyncSocket();
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker   // Creates a SyncSocket from a Handle.  Used in transport.
SyncSocket(Handle handle)47*635a8641SAndroid Build Coastguard Worker   explicit SyncSocket(Handle handle) : handle_(handle)  {}
48*635a8641SAndroid Build Coastguard Worker   virtual ~SyncSocket();
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker   // Initializes and connects a pair of sockets.
51*635a8641SAndroid Build Coastguard Worker   // |socket_a| and |socket_b| must not hold a valid handle.  Upon successful
52*635a8641SAndroid Build Coastguard Worker   // return, the sockets will both be valid and connected.
53*635a8641SAndroid Build Coastguard Worker   static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker   // Returns |Handle| wrapped in a |TransitDescriptor|.
56*635a8641SAndroid Build Coastguard Worker   static Handle UnwrapHandle(const TransitDescriptor& descriptor);
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker   // Prepares a |TransitDescriptor| which wraps |Handle| used for transit.
59*635a8641SAndroid Build Coastguard Worker   // This is used to prepare the underlying shared resource before passing back
60*635a8641SAndroid Build Coastguard Worker   // the handle to be used by the peer process.
61*635a8641SAndroid Build Coastguard Worker   bool PrepareTransitDescriptor(ProcessHandle peer_process_handle,
62*635a8641SAndroid Build Coastguard Worker                                 TransitDescriptor* descriptor);
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker   // Closes the SyncSocket.  Returns true on success, false on failure.
65*635a8641SAndroid Build Coastguard Worker   virtual bool Close();
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker   // Sends the message to the remote peer of the SyncSocket.
68*635a8641SAndroid Build Coastguard Worker   // Note it is not safe to send messages from the same socket handle by
69*635a8641SAndroid Build Coastguard Worker   // multiple threads simultaneously.
70*635a8641SAndroid Build Coastguard Worker   // buffer is a pointer to the data to send.
71*635a8641SAndroid Build Coastguard Worker   // length is the length of the data to send (must be non-zero).
72*635a8641SAndroid Build Coastguard Worker   // Returns the number of bytes sent, or 0 upon failure.
73*635a8641SAndroid Build Coastguard Worker   virtual size_t Send(const void* buffer, size_t length);
74*635a8641SAndroid Build Coastguard Worker 
75*635a8641SAndroid Build Coastguard Worker   // Receives a message from an SyncSocket.
76*635a8641SAndroid Build Coastguard Worker   // buffer is a pointer to the buffer to receive data.
77*635a8641SAndroid Build Coastguard Worker   // length is the number of bytes of data to receive (must be non-zero).
78*635a8641SAndroid Build Coastguard Worker   // Returns the number of bytes received, or 0 upon failure.
79*635a8641SAndroid Build Coastguard Worker   virtual size_t Receive(void* buffer, size_t length);
80*635a8641SAndroid Build Coastguard Worker 
81*635a8641SAndroid Build Coastguard Worker   // Same as Receive() but only blocks for data until |timeout| has elapsed or
82*635a8641SAndroid Build Coastguard Worker   // |buffer| |length| is exhausted.  Currently only timeouts less than one
83*635a8641SAndroid Build Coastguard Worker   // second are allowed.  Return the amount of data read.
84*635a8641SAndroid Build Coastguard Worker   virtual size_t ReceiveWithTimeout(void* buffer,
85*635a8641SAndroid Build Coastguard Worker                                     size_t length,
86*635a8641SAndroid Build Coastguard Worker                                     TimeDelta timeout);
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker   // Returns the number of bytes available. If non-zero, Receive() will not
89*635a8641SAndroid Build Coastguard Worker   // not block when called.
90*635a8641SAndroid Build Coastguard Worker   virtual size_t Peek();
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker   // Extracts the contained handle.  Used for transferring between
93*635a8641SAndroid Build Coastguard Worker   // processes.
handle()94*635a8641SAndroid Build Coastguard Worker   Handle handle() const { return handle_; }
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker   // Extracts and takes ownership of the contained handle.
97*635a8641SAndroid Build Coastguard Worker   Handle Release();
98*635a8641SAndroid Build Coastguard Worker 
99*635a8641SAndroid Build Coastguard Worker  protected:
100*635a8641SAndroid Build Coastguard Worker   Handle handle_;
101*635a8641SAndroid Build Coastguard Worker 
102*635a8641SAndroid Build Coastguard Worker  private:
103*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(SyncSocket);
104*635a8641SAndroid Build Coastguard Worker };
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker // Derives from SyncSocket and adds support for shutting down the socket from
107*635a8641SAndroid Build Coastguard Worker // another thread while a blocking Receive or Send is being done from the
108*635a8641SAndroid Build Coastguard Worker // thread that owns the socket.
109*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
110*635a8641SAndroid Build Coastguard Worker  public:
111*635a8641SAndroid Build Coastguard Worker   CancelableSyncSocket();
112*635a8641SAndroid Build Coastguard Worker   explicit CancelableSyncSocket(Handle handle);
113*635a8641SAndroid Build Coastguard Worker   ~CancelableSyncSocket() override = default;
114*635a8641SAndroid Build Coastguard Worker 
115*635a8641SAndroid Build Coastguard Worker   // Initializes a pair of cancelable sockets.  See documentation for
116*635a8641SAndroid Build Coastguard Worker   // SyncSocket::CreatePair for more details.
117*635a8641SAndroid Build Coastguard Worker   static bool CreatePair(CancelableSyncSocket* socket_a,
118*635a8641SAndroid Build Coastguard Worker                          CancelableSyncSocket* socket_b);
119*635a8641SAndroid Build Coastguard Worker 
120*635a8641SAndroid Build Coastguard Worker   // A way to shut down a socket even if another thread is currently performing
121*635a8641SAndroid Build Coastguard Worker   // a blocking Receive or Send.
122*635a8641SAndroid Build Coastguard Worker   bool Shutdown();
123*635a8641SAndroid Build Coastguard Worker 
124*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
125*635a8641SAndroid Build Coastguard Worker   // Since the Linux and Mac implementations actually use a socket, shutting
126*635a8641SAndroid Build Coastguard Worker   // them down from another thread is pretty simple - we can just call
127*635a8641SAndroid Build Coastguard Worker   // shutdown().  However, the Windows implementation relies on named pipes
128*635a8641SAndroid Build Coastguard Worker   // and there isn't a way to cancel a blocking synchronous Read that is
129*635a8641SAndroid Build Coastguard Worker   // supported on <Vista. So, for Windows only, we override these
130*635a8641SAndroid Build Coastguard Worker   // SyncSocket methods in order to support shutting down the 'socket'.
131*635a8641SAndroid Build Coastguard Worker   bool Close() override;
132*635a8641SAndroid Build Coastguard Worker   size_t Receive(void* buffer, size_t length) override;
133*635a8641SAndroid Build Coastguard Worker   size_t ReceiveWithTimeout(void* buffer,
134*635a8641SAndroid Build Coastguard Worker                             size_t length,
135*635a8641SAndroid Build Coastguard Worker                             TimeDelta timeout) override;
136*635a8641SAndroid Build Coastguard Worker #endif
137*635a8641SAndroid Build Coastguard Worker 
138*635a8641SAndroid Build Coastguard Worker   // Send() is overridden to catch cases where the remote end is not responding
139*635a8641SAndroid Build Coastguard Worker   // and we fill the local socket buffer. When the buffer is full, this
140*635a8641SAndroid Build Coastguard Worker   // implementation of Send() will not block indefinitely as
141*635a8641SAndroid Build Coastguard Worker   // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
142*635a8641SAndroid Build Coastguard Worker   // Note that the socket will not be closed in this case.
143*635a8641SAndroid Build Coastguard Worker   size_t Send(const void* buffer, size_t length) override;
144*635a8641SAndroid Build Coastguard Worker 
145*635a8641SAndroid Build Coastguard Worker  private:
146*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
147*635a8641SAndroid Build Coastguard Worker   WaitableEvent shutdown_event_;
148*635a8641SAndroid Build Coastguard Worker   WaitableEvent file_operation_;
149*635a8641SAndroid Build Coastguard Worker #endif
150*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
151*635a8641SAndroid Build Coastguard Worker };
152*635a8641SAndroid Build Coastguard Worker 
153*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) && !defined(COMPONENT_BUILD)
154*635a8641SAndroid Build Coastguard Worker // TODO(cpu): remove this once chrome is split in two dlls.
155*635a8641SAndroid Build Coastguard Worker __declspec(selectany)
156*635a8641SAndroid Build Coastguard Worker     const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
157*635a8641SAndroid Build Coastguard Worker #endif
158*635a8641SAndroid Build Coastguard Worker 
159*635a8641SAndroid Build Coastguard Worker }  // namespace base
160*635a8641SAndroid Build Coastguard Worker 
161*635a8641SAndroid Build Coastguard Worker #endif  // BASE_SYNC_SOCKET_H_
162