blob: fcf4155047e20cface5ed42086828b9db729aea7 [file] [log] [blame]
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
sehr@google.com4f325aa2009-11-25 01:14:53 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_SYNC_SOCKET_H_
6#define BASE_SYNC_SOCKET_H_
7
8// A socket abstraction used for sending and receiving plain
xians@chromium.orgf7d9af82012-04-19 19:23:03 +09009// data. Because the receiving is blocking, they can be used to perform
sehr@google.com4f325aa2009-11-25 01:14:53 +090010// rudimentary cross-process synchronization with low latency.
11
avia6a6a682015-12-27 07:15:14 +090012#include <stddef.h>
13
14#include "base/base_export.h"
15#include "base/compiler_specific.h"
16#include "base/macros.h"
17#include "base/process/process_handle.h"
18#include "base/synchronization/waitable_event.h"
19#include "base/time/time.h"
20#include "build/build_config.h"
21
sehr@google.com4f325aa2009-11-25 01:14:53 +090022#if defined(OS_WIN)
23#include <windows.h>
24#endif
25#include <sys/types.h>
26
burnik81ab7622014-09-08 15:58:22 +090027#if defined(OS_POSIX)
28#include "base/file_descriptor_posix.h"
29#endif
30
sehr@google.com4f325aa2009-11-25 01:14:53 +090031namespace base {
32
darin@chromium.orge585bed2011-08-06 00:34:00 +090033class BASE_EXPORT SyncSocket {
sehr@google.com4f325aa2009-11-25 01:14:53 +090034 public:
35#if defined(OS_WIN)
36 typedef HANDLE Handle;
burnik81ab7622014-09-08 15:58:22 +090037 typedef Handle TransitDescriptor;
sehr@google.com4f325aa2009-11-25 01:14:53 +090038#else
39 typedef int Handle;
burnik81ab7622014-09-08 15:58:22 +090040 typedef FileDescriptor TransitDescriptor;
sehr@google.com4f325aa2009-11-25 01:14:53 +090041#endif
groby@chromium.orge870b642011-12-23 04:33:27 +090042 static const Handle kInvalidHandle;
sehr@google.com4f325aa2009-11-25 01:14:53 +090043
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090044 SyncSocket();
sehr@google.com4f325aa2009-11-25 01:14:53 +090045
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090046 // Creates a SyncSocket from a Handle. Used in transport.
47 explicit SyncSocket(Handle handle) : handle_(handle) {}
48 virtual ~SyncSocket();
49
50 // Initializes and connects a pair of sockets.
51 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful
52 // return, the sockets will both be valid and connected.
53 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
sehr@google.com4f325aa2009-11-25 01:14:53 +090054
burnik81ab7622014-09-08 15:58:22 +090055 // Returns |Handle| wrapped in a |TransitDescriptor|.
56 static Handle UnwrapHandle(const TransitDescriptor& descriptor);
57
58 // Prepares a |TransitDescriptor| which wraps |Handle| used for transit.
59 // This is used to prepare the underlying shared resource before passing back
60 // the handle to be used by the peer process.
61 bool PrepareTransitDescriptor(ProcessHandle peer_process_handle,
62 TransitDescriptor* descriptor);
63
sehr@google.com4f325aa2009-11-25 01:14:53 +090064 // Closes the SyncSocket. Returns true on success, false on failure.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090065 virtual bool Close();
sehr@google.com4f325aa2009-11-25 01:14:53 +090066
67 // Sends the message to the remote peer of the SyncSocket.
68 // Note it is not safe to send messages from the same socket handle by
69 // multiple threads simultaneously.
70 // buffer is a pointer to the data to send.
71 // length is the length of the data to send (must be non-zero).
72 // Returns the number of bytes sent, or 0 upon failure.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090073 virtual size_t Send(const void* buffer, size_t length);
sehr@google.com4f325aa2009-11-25 01:14:53 +090074
75 // Receives a message from an SyncSocket.
76 // buffer is a pointer to the buffer to receive data.
77 // length is the number of bytes of data to receive (must be non-zero).
78 // Returns the number of bytes received, or 0 upon failure.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090079 virtual size_t Receive(void* buffer, size_t length);
sehr@google.com4f325aa2009-11-25 01:14:53 +090080
dalecurtis@chromium.org96b19df2013-10-20 07:13:19 +090081 // Same as Receive() but only blocks for data until |timeout| has elapsed or
82 // |buffer| |length| is exhausted. Currently only timeouts less than one
83 // second are allowed. Return the amount of data read.
84 virtual size_t ReceiveWithTimeout(void* buffer,
85 size_t length,
86 TimeDelta timeout);
87
cpu@chromium.org750392c2009-12-05 07:53:22 +090088 // Returns the number of bytes available. If non-zero, Receive() will not
grunellc128ba52015-09-11 00:24:42 +090089 // not block when called.
90 virtual size_t Peek();
cpu@chromium.org750392c2009-12-05 07:53:22 +090091
sehr@google.com4f325aa2009-11-25 01:14:53 +090092 // Extracts the contained handle. Used for transferring between
93 // processes.
94 Handle handle() const { return handle_; }
95
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090096 protected:
sehr@google.com4f325aa2009-11-25 01:14:53 +090097 Handle handle_;
98
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090099 private:
sehr@google.com4f325aa2009-11-25 01:14:53 +0900100 DISALLOW_COPY_AND_ASSIGN(SyncSocket);
101};
102
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900103// Derives from SyncSocket and adds support for shutting down the socket from
xians@chromium.orgf7d9af82012-04-19 19:23:03 +0900104// another thread while a blocking Receive or Send is being done from the
105// thread that owns the socket.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900106class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
107 public:
108 CancelableSyncSocket();
109 explicit CancelableSyncSocket(Handle handle);
dcheng7dc8df52014-10-21 19:54:51 +0900110 ~CancelableSyncSocket() override {}
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900111
112 // Initializes a pair of cancelable sockets. See documentation for
113 // SyncSocket::CreatePair for more details.
114 static bool CreatePair(CancelableSyncSocket* socket_a,
115 CancelableSyncSocket* socket_b);
116
117 // A way to shut down a socket even if another thread is currently performing
118 // a blocking Receive or Send.
119 bool Shutdown();
120
121#if defined(OS_WIN)
122 // Since the Linux and Mac implementations actually use a socket, shutting
123 // them down from another thread is pretty simple - we can just call
124 // shutdown(). However, the Windows implementation relies on named pipes
125 // and there isn't a way to cancel a blocking synchronous Read that is
126 // supported on <Vista. So, for Windows only, we override these
127 // SyncSocket methods in order to support shutting down the 'socket'.
dcheng21f84402015-04-17 16:35:46 +0900128 bool Close() override;
129 size_t Receive(void* buffer, size_t length) override;
130 size_t ReceiveWithTimeout(void* buffer,
131 size_t length,
132 TimeDelta timeout) override;
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900133#endif
134
xians@chromium.orgf7d9af82012-04-19 19:23:03 +0900135 // Send() is overridden to catch cases where the remote end is not responding
136 // and we fill the local socket buffer. When the buffer is full, this
137 // implementation of Send() will not block indefinitely as
138 // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
139 // Note that the socket will not be closed in this case.
dcheng7dc8df52014-10-21 19:54:51 +0900140 size_t Send(const void* buffer, size_t length) override;
xians@chromium.orgf7d9af82012-04-19 19:23:03 +0900141
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900142 private:
143#if defined(OS_WIN)
144 WaitableEvent shutdown_event_;
145 WaitableEvent file_operation_;
146#endif
147 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
148};
149
cpu@chromium.org434a48c2013-05-23 13:34:11 +0900150#if defined(OS_WIN) && !defined(COMPONENT_BUILD)
151// TODO(cpu): remove this once chrome is split in two dlls.
152__declspec(selectany)
153 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE;
154#endif
155
sehr@google.com4f325aa2009-11-25 01:14:53 +0900156} // namespace base
157
158#endif // BASE_SYNC_SOCKET_H_