| 6 | #define BASE_SYNC_SOCKET_H_ |
| 7 |
| 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. |
| 11 |
| 12 | #include <stddef.h> |
| 13 |
| 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" |
| 21 |
| 22 | #if defined(OS_WIN) |
| 23 | #include <windows.h> |
| 24 | #endif |
| 25 | #include <sys/types.h> |
| 26 |
| 27 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 28 | #include "base/file_descriptor_posix.h" |
| 29 | #endif |
| 30 |
| 31 | namespace base { |
| 32 |
| 33 | class BASE_EXPORT SyncSocket { |
| 34 | public: |
| 35 | using Handle = PlatformFile; |
| 36 | using ScopedHandle = ScopedPlatformFile; |
| 37 | static const Handle kInvalidHandle; |
| 38 |
| 39 | SyncSocket(); |
| 40 |
| 41 | // Creates a SyncSocket from a Handle. |
| 42 | explicit SyncSocket(Handle handle); |
| 43 | explicit SyncSocket(ScopedHandle handle); |
| 44 | 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); |
| 50 |
| 51 | // Closes the SyncSocket. |
| 52 | virtual void Close(); |
| 53 |
| 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); |
| 61 |
| 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); |
| 67 |
| 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, |
| 72 | size_t length, |
| 73 | TimeDelta timeout); |
| 74 |
| 75 | // Returns the number of bytes available. If non-zero, Receive() will not |
| 76 | // not block when called. |
| 77 | virtual size_t Peek(); |
| 78 |
| 79 | // Returns true if the Handle is valid, and false if it is not. |
| 80 | bool IsValid() const; |
| 81 |
| 82 | // Extracts the contained handle. Used for transferring between |
| 83 | // processes. |
| 84 | Handle handle() const; |
| 85 |
| 86 | // Extracts and takes ownership of the contained handle. |
| 87 | Handle Release(); |
| 88 | ScopedHandle Take(); |
| 89 |
| 90 | protected: |
| 91 | ScopedHandle handle_; |
| 92 |
| 93 | private: |
| 94 | DISALLOW_COPY_AND_ASSIGN(SyncSocket); |
| 95 | }; |
| 96 |
| 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 { |
| 101 | public: |
| 102 | CancelableSyncSocket(); |
| 103 | explicit CancelableSyncSocket(Handle handle); |
| 104 | explicit CancelableSyncSocket(ScopedHandle handle); |
| 105 | ~CancelableSyncSocket() override = default; |
| 106 |
| 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); |
| 111 |
| 112 | // A way to shut down a socket even if another thread is currently performing |
| 113 | // a blocking Receive or Send. |
| 114 | bool Shutdown(); |
| 115 |
| 116 | #if defined(OS_WIN) |
| 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, |
| 126 | size_t length, |
| 127 | TimeDelta timeout) override; |
| 128 | #endif |
| 129 |
| 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; |
| 136 |
| 137 | private: |
| 138 | #if defined(OS_WIN) |
| 139 | WaitableEvent shutdown_event_; |
| 140 | WaitableEvent file_operation_; |
| 141 | #endif |
| 142 | DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
| 143 | }; |
| 144 |
| 145 | } // namespace base |
| 146 |
| 147 | #endif // BASE_SYNC_SOCKET_H_ |
| 148 |