| // Copyright 2012 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef BASE_SYNC_SOCKET_H_ |
| #define BASE_SYNC_SOCKET_H_ |
| |
| // A socket abstraction used for sending and receiving plain |
| // data. Because the receiving is blocking, they can be used to perform |
| // rudimentary cross-process synchronization with low latency. |
| |
| #include<stddef.h> |
| |
| #include"base/base_export.h" |
| #include"base/containers/span.h" |
| #include"base/files/platform_file.h" |
| #include"base/synchronization/waitable_event.h" |
| #include"base/time/time.h" |
| #include"build/build_config.h" |
| |
| #if BUILDFLAG(IS_WIN) |
| #include<windows.h> |
| #endif |
| #include<sys/types.h> |
| |
| namespacebase{ |
| |
| class BASE_EXPORTSyncSocket{ |
| public: |
| usingHandle=PlatformFile; |
| usingScopedHandle=ScopedPlatformFile; |
| staticconstHandle kInvalidHandle; |
| |
| SyncSocket(); |
| |
| // Creates a SyncSocket from a Handle. |
| explicitSyncSocket(Handle handle); |
| explicitSyncSocket(ScopedHandle handle); |
| SyncSocket(constSyncSocket&)=delete; |
| SyncSocket&operator=(constSyncSocket&)=delete; |
| virtual~SyncSocket(); |
| |
| // Initializes and connects a pair of sockets. |
| // |socket_a| and |socket_b| must not hold a valid handle. Upon successful |
| // return, the sockets will both be valid and connected. |
| staticboolCreatePair(SyncSocket* socket_a,SyncSocket* socket_b); |
| |
| // Closes the SyncSocket. |
| virtualvoidClose(); |
| |
| // Sends the message to the remote peer of the SyncSocket. |
| // Note it is not safe to send messages from the same socket handle by |
| // multiple threads simultaneously. |
| // `data` must be non-empty. |
| // Returns the number of bytes sent, or 0 upon failure. |
| virtualsize_tSend(span<constuint8_t> data); |
| |
| // Receives a message from an SyncSocket. |
| // The data will be received in `buffer`, which must be non-empty. |
| // Returns the number of bytes received, or 0 upon failure. |
| virtualsize_tReceive(span<uint8_t> buffer); |
| |
| // Same as Receive() but only blocks for data until `timeout` has elapsed or |
| // `buffer` is exhausted. Currently only timeouts less than one second are |
| // allowed. Returns the number of bytes read. |
| virtualsize_tReceiveWithTimeout(span<uint8_t> buffer,TimeDelta timeout); |
| |
| // Returns the number of bytes available. If non-zero, Receive() will not |
| // not block when called. |
| virtualsize_tPeek(); |
| |
| // Returns true if the Handle is valid, and false if it is not. |
| boolIsValid()const; |
| |
| // Extracts the contained handle. Used for transferring between |
| // processes. |
| Handle handle()const; |
| |
| // Extracts and takes ownership of the contained handle. |
| HandleRelease(); |
| ScopedHandleTake(); |
| |
| protected: |
| ScopedHandle handle_; |
| }; |
| |
| // Derives from SyncSocket and adds support for shutting down the socket from |
| // another thread while a blocking Receive or Send is being done from the |
| // thread that owns the socket. |
| class BASE_EXPORTCancelableSyncSocket:publicSyncSocket{ |
| public: |
| CancelableSyncSocket(); |
| explicitCancelableSyncSocket(Handle handle); |
| explicitCancelableSyncSocket(ScopedHandle handle); |
| CancelableSyncSocket(constCancelableSyncSocket&)=delete; |
| CancelableSyncSocket&operator=(constCancelableSyncSocket&)=delete; |
| ~CancelableSyncSocket()override=default; |
| |
| // Initializes a pair of cancelable sockets. See documentation for |
| // SyncSocket::CreatePair for more details. |
| staticboolCreatePair(CancelableSyncSocket* socket_a, |
| CancelableSyncSocket* socket_b); |
| |
| // A way to shut down a socket even if another thread is currently performing |
| // a blocking Receive or Send. |
| boolShutdown(); |
| |
| #if BUILDFLAG(IS_WIN) |
| // Since the Linux and Mac implementations actually use a socket, shutting |
| // them down from another thread is pretty simple - we can just call |
| // shutdown(). However, the Windows implementation relies on named pipes |
| // and there isn't a way to cancel a blocking synchronous Read that is |
| // supported on <Vista. So, for Windows only, we override these |
| // SyncSocket methods in order to support shutting down the 'socket'. |
| voidClose()override; |
| size_tReceive(span<uint8_t> buffer)override; |
| size_tReceiveWithTimeout(span<uint8_t> buffer,TimeDelta timeout)override; |
| #endif |
| |
| // Send() is overridden to catch cases where the remote end is not responding |
| // and we fill the local socket buffer. When `data` is full, this |
| // implementation of Send() will not block indefinitely as |
| // SyncSocket::Send will, but instead return 0, as no bytes could be sent. |
| // Note that the socket will not be closed in this case. |
| size_tSend(span<constuint8_t> data)override; |
| |
| private: |
| #if BUILDFLAG(IS_WIN) |
| WaitableEvent shutdown_event_; |
| WaitableEvent file_operation_; |
| #endif |
| }; |
| |
| }// namespace base |
| |
| #endif// BASE_SYNC_SOCKET_H_ |