blob: 6cc027964bd99717db2c89bceeba317476efb75f [file] [log] [blame]
[email protected]532e9bd2012-01-25 12:04:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0840cc72009-11-24 16:14:532// 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
[email protected]5d272092012-04-19 10:23:039// data. Because the receiving is blocking, they can be used to perform
[email protected]0840cc72009-11-24 16:14:5310// rudimentary cross-process synchronization with low latency.
11
avi9b6f42932015-12-26 22:15:1412#include <stddef.h>
13
14#include "base/base_export.h"
Robert Sesek6ab73b022020-02-13 16:42:3915#include "base/files/platform_file.h"
avi9b6f42932015-12-26 22:15:1416#include "base/synchronization/waitable_event.h"
17#include "base/time/time.h"
18#include "build/build_config.h"
19
Xiaohan Wang38e4ebb2022-01-19 06:57:4320#if BUILDFLAG(IS_WIN)
[email protected]0840cc72009-11-24 16:14:5321#include <windows.h>
22#endif
23#include <sys/types.h>
24
Xiaohan Wang38e4ebb2022-01-19 06:57:4325#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
burnik3d670052014-09-08 06:58:2226#include "base/file_descriptor_posix.h"
27#endif
28
[email protected]0840cc72009-11-24 16:14:5329namespace base {
30
[email protected]0bea7252011-08-05 15:34:0031class BASE_EXPORT SyncSocket {
[email protected]0840cc72009-11-24 16:14:5332 public:
Robert Sesek6ab73b022020-02-13 16:42:3933 using Handle = PlatformFile;
34 using ScopedHandle = ScopedPlatformFile;
[email protected]5895ee12011-12-22 19:33:2735 static const Handle kInvalidHandle;
[email protected]0840cc72009-11-24 16:14:5336
[email protected]532e9bd2012-01-25 12:04:1737 SyncSocket();
[email protected]0840cc72009-11-24 16:14:5338
Robert Sesek6ab73b022020-02-13 16:42:3939 // Creates a SyncSocket from a Handle.
40 explicit SyncSocket(Handle handle);
41 explicit SyncSocket(ScopedHandle handle);
David Bienvenu5f4d4f02020-09-27 16:55:0342 SyncSocket(const SyncSocket&) = delete;
43 SyncSocket& operator=(const SyncSocket&) = delete;
[email protected]532e9bd2012-01-25 12:04:1744 virtual ~SyncSocket();
45
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);
[email protected]0840cc72009-11-24 16:14:5350
Robert Sesek6ab73b022020-02-13 16:42:3951 // Closes the SyncSocket.
52 virtual void Close();
[email protected]0840cc72009-11-24 16:14:5353
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.
[email protected]532e9bd2012-01-25 12:04:1760 virtual size_t Send(const void* buffer, size_t length);
[email protected]0840cc72009-11-24 16:14:5361
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.
[email protected]532e9bd2012-01-25 12:04:1766 virtual size_t Receive(void* buffer, size_t length);
[email protected]0840cc72009-11-24 16:14:5367
[email protected]62558f12013-10-19 22:13:1968 // 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,
72 size_t length,
73 TimeDelta timeout);
74
[email protected]d8b65912009-12-04 22:53:2275 // Returns the number of bytes available. If non-zero, Receive() will not
grunell8d9071d2015-09-10 15:24:4276 // not block when called.
77 virtual size_t Peek();
[email protected]d8b65912009-12-04 22:53:2278
Robert Sesek6ab73b022020-02-13 16:42:3979 // Returns true if the Handle is valid, and false if it is not.
80 bool IsValid() const;
81
[email protected]0840cc72009-11-24 16:14:5382 // Extracts the contained handle. Used for transferring between
83 // processes.
Robert Sesek6ab73b022020-02-13 16:42:3984 Handle handle() const;
[email protected]0840cc72009-11-24 16:14:5385
maxmorind4bcb112017-04-13 11:43:1386 // Extracts and takes ownership of the contained handle.
87 Handle Release();
Robert Sesek6ab73b022020-02-13 16:42:3988 ScopedHandle Take();
maxmorind4bcb112017-04-13 11:43:1389
[email protected]532e9bd2012-01-25 12:04:1790 protected:
Robert Sesek6ab73b022020-02-13 16:42:3991 ScopedHandle handle_;
[email protected]0840cc72009-11-24 16:14:5392};
93
[email protected]532e9bd2012-01-25 12:04:1794// Derives from SyncSocket and adds support for shutting down the socket from
[email protected]5d272092012-04-19 10:23:0395// another thread while a blocking Receive or Send is being done from the
96// thread that owns the socket.
[email protected]532e9bd2012-01-25 12:04:1797class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
98 public:
99 CancelableSyncSocket();
100 explicit CancelableSyncSocket(Handle handle);
Robert Sesek6ab73b022020-02-13 16:42:39101 explicit CancelableSyncSocket(ScopedHandle handle);
David Bienvenu5f4d4f02020-09-27 16:55:03102 CancelableSyncSocket(const CancelableSyncSocket&) = delete;
103 CancelableSyncSocket& operator=(const CancelableSyncSocket&) = delete;
Chris Watkins091d6292017-12-13 04:25:58104 ~CancelableSyncSocket() override = default;
[email protected]532e9bd2012-01-25 12:04:17105
106 // Initializes a pair of cancelable sockets. See documentation for
107 // SyncSocket::CreatePair for more details.
108 static bool CreatePair(CancelableSyncSocket* socket_a,
109 CancelableSyncSocket* socket_b);
110
111 // A way to shut down a socket even if another thread is currently performing
112 // a blocking Receive or Send.
113 bool Shutdown();
114
Xiaohan Wang38e4ebb2022-01-19 06:57:43115#if BUILDFLAG(IS_WIN)
[email protected]532e9bd2012-01-25 12:04:17116 // Since the Linux and Mac implementations actually use a socket, shutting
117 // them down from another thread is pretty simple - we can just call
118 // shutdown(). However, the Windows implementation relies on named pipes
119 // and there isn't a way to cancel a blocking synchronous Read that is
120 // supported on <Vista. So, for Windows only, we override these
121 // SyncSocket methods in order to support shutting down the 'socket'.
Robert Sesek6ab73b022020-02-13 16:42:39122 void Close() override;
dchengfbce1262015-04-17 07:35:46123 size_t Receive(void* buffer, size_t length) override;
124 size_t ReceiveWithTimeout(void* buffer,
125 size_t length,
126 TimeDelta timeout) override;
[email protected]532e9bd2012-01-25 12:04:17127#endif
128
[email protected]5d272092012-04-19 10:23:03129 // Send() is overridden to catch cases where the remote end is not responding
130 // and we fill the local socket buffer. When the buffer is full, this
131 // implementation of Send() will not block indefinitely as
132 // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
133 // Note that the socket will not be closed in this case.
dcheng56488182014-10-21 10:54:51134 size_t Send(const void* buffer, size_t length) override;
[email protected]5d272092012-04-19 10:23:03135
[email protected]532e9bd2012-01-25 12:04:17136 private:
Xiaohan Wang38e4ebb2022-01-19 06:57:43137#if BUILDFLAG(IS_WIN)
[email protected]532e9bd2012-01-25 12:04:17138 WaitableEvent shutdown_event_;
139 WaitableEvent file_operation_;
140#endif
[email protected]532e9bd2012-01-25 12:04:17141};
142
[email protected]0840cc72009-11-24 16:14:53143} // namespace base
144
145#endif // BASE_SYNC_SOCKET_H_