Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 2 | // 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] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 9 | // data. Because the receiving is blocking, they can be used to perform |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 10 | // rudimentary cross-process synchronization with low latency. |
| 11 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | #include <stddef.h> |
| 13 | |
| 14 | #include "base/base_export.h" |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 15 | #include "base/containers/span.h" |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 16 | #include "base/files/platform_file.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 17 | #include "base/synchronization/waitable_event.h" |
| 18 | #include "base/time/time.h" |
| 19 | #include "build/build_config.h" |
| 20 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 21 | #if BUILDFLAG(IS_WIN) |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 22 | #include <windows.h> |
| 23 | #endif |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | namespace base { |
| 27 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 28 | class BASE_EXPORT SyncSocket { |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 29 | public: |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 30 | using Handle = PlatformFile; |
| 31 | using ScopedHandle = ScopedPlatformFile; |
[email protected] | 5895ee1 | 2011-12-22 19:33:27 | [diff] [blame] | 32 | static const Handle kInvalidHandle; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 33 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 34 | SyncSocket(); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 35 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 36 | // Creates a SyncSocket from a Handle. |
| 37 | explicit SyncSocket(Handle handle); |
| 38 | explicit SyncSocket(ScopedHandle handle); |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 39 | SyncSocket(const SyncSocket&) = delete; |
| 40 | SyncSocket& operator=(const SyncSocket&) = delete; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 41 | virtual ~SyncSocket(); |
| 42 | |
| 43 | // Initializes and connects a pair of sockets. |
| 44 | // |socket_a| and |socket_b| must not hold a valid handle. Upon successful |
| 45 | // return, the sockets will both be valid and connected. |
| 46 | static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 47 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 48 | // Closes the SyncSocket. |
| 49 | virtual void Close(); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 50 | |
| 51 | // Sends the message to the remote peer of the SyncSocket. |
| 52 | // Note it is not safe to send messages from the same socket handle by |
| 53 | // multiple threads simultaneously. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 54 | // `data` must be non-empty. |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 55 | // Returns the number of bytes sent, or 0 upon failure. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 56 | virtual size_t Send(span<const uint8_t> data); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 57 | |
| 58 | // Receives a message from an SyncSocket. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 59 | // The data will be received in `buffer`, which must be non-empty. |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 60 | // Returns the number of bytes received, or 0 upon failure. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 61 | virtual size_t Receive(span<uint8_t> buffer); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 62 | |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 63 | // Same as Receive() but only blocks for data until `timeout` has elapsed or |
| 64 | // `buffer` is exhausted. Currently only timeouts less than one second are |
| 65 | // allowed. Returns the number of bytes read. |
| 66 | virtual size_t ReceiveWithTimeout(span<uint8_t> buffer, TimeDelta timeout); |
[email protected] | 62558f1 | 2013-10-19 22:13:19 | [diff] [blame] | 67 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 68 | // Returns the number of bytes available. If non-zero, Receive() will not |
grunell | 8d9071d | 2015-09-10 15:24:42 | [diff] [blame] | 69 | // not block when called. |
| 70 | virtual size_t Peek(); |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 71 | |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 72 | // Returns true if the Handle is valid, and false if it is not. |
| 73 | bool IsValid() const; |
| 74 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 75 | // Extracts the contained handle. Used for transferring between |
| 76 | // processes. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 77 | Handle handle() const; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 78 | |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 79 | // Extracts and takes ownership of the contained handle. |
| 80 | Handle Release(); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 81 | ScopedHandle Take(); |
maxmorin | d4bcb11 | 2017-04-13 11:43:13 | [diff] [blame] | 82 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 83 | protected: |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 84 | ScopedHandle handle_; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 85 | }; |
| 86 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 87 | // Derives from SyncSocket and adds support for shutting down the socket from |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 88 | // another thread while a blocking Receive or Send is being done from the |
| 89 | // thread that owns the socket. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 90 | class BASE_EXPORT CancelableSyncSocket : public SyncSocket { |
| 91 | public: |
| 92 | CancelableSyncSocket(); |
| 93 | explicit CancelableSyncSocket(Handle handle); |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 94 | explicit CancelableSyncSocket(ScopedHandle handle); |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 95 | CancelableSyncSocket(const CancelableSyncSocket&) = delete; |
| 96 | CancelableSyncSocket& operator=(const CancelableSyncSocket&) = delete; |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 97 | ~CancelableSyncSocket() override = default; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 98 | |
| 99 | // Initializes a pair of cancelable sockets. See documentation for |
| 100 | // SyncSocket::CreatePair for more details. |
| 101 | static bool CreatePair(CancelableSyncSocket* socket_a, |
| 102 | CancelableSyncSocket* socket_b); |
| 103 | |
| 104 | // A way to shut down a socket even if another thread is currently performing |
| 105 | // a blocking Receive or Send. |
| 106 | bool Shutdown(); |
| 107 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 108 | #if BUILDFLAG(IS_WIN) |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 109 | // Since the Linux and Mac implementations actually use a socket, shutting |
| 110 | // them down from another thread is pretty simple - we can just call |
| 111 | // shutdown(). However, the Windows implementation relies on named pipes |
| 112 | // and there isn't a way to cancel a blocking synchronous Read that is |
| 113 | // supported on <Vista. So, for Windows only, we override these |
| 114 | // SyncSocket methods in order to support shutting down the 'socket'. |
Robert Sesek | 6ab73b02 | 2020-02-13 16:42:39 | [diff] [blame] | 115 | void Close() override; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 116 | size_t Receive(span<uint8_t> buffer) override; |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 117 | size_t ReceiveWithTimeout(span<uint8_t> buffer, TimeDelta timeout) override; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 118 | #endif |
| 119 | |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 120 | // Send() is overridden to catch cases where the remote end is not responding |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 121 | // and we fill the local socket buffer. When `data` is full, this |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 122 | // implementation of Send() will not block indefinitely as |
| 123 | // SyncSocket::Send will, but instead return 0, as no bytes could be sent. |
| 124 | // Note that the socket will not be closed in this case. |
Austin Sullivan | edf168fd | 2024-01-17 21:37:20 | [diff] [blame] | 125 | size_t Send(span<const uint8_t> data) override; |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 126 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 127 | private: |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 128 | #if BUILDFLAG(IS_WIN) |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 129 | WaitableEvent shutdown_event_; |
| 130 | WaitableEvent file_operation_; |
| 131 | #endif |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 132 | }; |
| 133 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 134 | } // namespace base |
| 135 | |
| 136 | #endif // BASE_SYNC_SOCKET_H_ |