[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[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 | |
| 12 | #include "base/basictypes.h" |
| 13 | #if defined(OS_WIN) |
| 14 | #include <windows.h> |
| 15 | #endif |
| 16 | #include <sys/types.h> |
| 17 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 18 | #include "base/base_export.h" |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 19 | #include "base/compiler_specific.h" |
| 20 | #include "base/synchronization/waitable_event.h" |
[email protected] | 9493ee95c | 2011-03-28 23:48:44 | [diff] [blame] | 21 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 22 | namespace base { |
| 23 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 24 | class BASE_EXPORT SyncSocket { |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 25 | public: |
| 26 | #if defined(OS_WIN) |
| 27 | typedef HANDLE Handle; |
| 28 | #else |
| 29 | typedef int Handle; |
| 30 | #endif |
[email protected] | 5895ee1 | 2011-12-22 19:33:27 | [diff] [blame] | 31 | static const Handle kInvalidHandle; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 32 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 33 | SyncSocket(); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 34 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 35 | // Creates a SyncSocket from a Handle. Used in transport. |
| 36 | explicit SyncSocket(Handle handle) : handle_(handle) {} |
| 37 | virtual ~SyncSocket(); |
| 38 | |
| 39 | // Initializes and connects a pair of sockets. |
| 40 | // |socket_a| and |socket_b| must not hold a valid handle. Upon successful |
| 41 | // return, the sockets will both be valid and connected. |
| 42 | static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 43 | |
| 44 | // Closes the SyncSocket. Returns true on success, false on failure. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 45 | virtual bool Close(); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 46 | |
| 47 | // Sends the message to the remote peer of the SyncSocket. |
| 48 | // Note it is not safe to send messages from the same socket handle by |
| 49 | // multiple threads simultaneously. |
| 50 | // buffer is a pointer to the data to send. |
| 51 | // length is the length of the data to send (must be non-zero). |
| 52 | // Returns the number of bytes sent, or 0 upon failure. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 53 | virtual size_t Send(const void* buffer, size_t length); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 54 | |
| 55 | // Receives a message from an SyncSocket. |
| 56 | // buffer is a pointer to the buffer to receive data. |
| 57 | // length is the number of bytes of data to receive (must be non-zero). |
| 58 | // Returns the number of bytes received, or 0 upon failure. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 59 | virtual size_t Receive(void* buffer, size_t length); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 60 | |
[email protected] | d8b6591 | 2009-12-04 22:53:22 | [diff] [blame] | 61 | // Returns the number of bytes available. If non-zero, Receive() will not |
| 62 | // not block when called. NOTE: Some implementations cannot reliably |
| 63 | // determine the number of bytes available so avoid using the returned |
| 64 | // size as a promise and simply test against zero. |
| 65 | size_t Peek(); |
| 66 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 67 | // Extracts the contained handle. Used for transferring between |
| 68 | // processes. |
| 69 | Handle handle() const { return handle_; } |
| 70 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 71 | protected: |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 72 | Handle handle_; |
| 73 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 74 | private: |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 75 | DISALLOW_COPY_AND_ASSIGN(SyncSocket); |
| 76 | }; |
| 77 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 78 | // Derives from SyncSocket and adds support for shutting down the socket from |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 79 | // another thread while a blocking Receive or Send is being done from the |
| 80 | // thread that owns the socket. |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 81 | class BASE_EXPORT CancelableSyncSocket : public SyncSocket { |
| 82 | public: |
| 83 | CancelableSyncSocket(); |
| 84 | explicit CancelableSyncSocket(Handle handle); |
| 85 | virtual ~CancelableSyncSocket() {} |
| 86 | |
| 87 | // Initializes a pair of cancelable sockets. See documentation for |
| 88 | // SyncSocket::CreatePair for more details. |
| 89 | static bool CreatePair(CancelableSyncSocket* socket_a, |
| 90 | CancelableSyncSocket* socket_b); |
| 91 | |
| 92 | // A way to shut down a socket even if another thread is currently performing |
| 93 | // a blocking Receive or Send. |
| 94 | bool Shutdown(); |
| 95 | |
| 96 | #if defined(OS_WIN) |
| 97 | // Since the Linux and Mac implementations actually use a socket, shutting |
| 98 | // them down from another thread is pretty simple - we can just call |
| 99 | // shutdown(). However, the Windows implementation relies on named pipes |
| 100 | // and there isn't a way to cancel a blocking synchronous Read that is |
| 101 | // supported on <Vista. So, for Windows only, we override these |
| 102 | // SyncSocket methods in order to support shutting down the 'socket'. |
| 103 | virtual bool Close() OVERRIDE; |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 104 | virtual size_t Receive(void* buffer, size_t length) OVERRIDE; |
| 105 | #endif |
| 106 | |
[email protected] | 5d27209 | 2012-04-19 10:23:03 | [diff] [blame] | 107 | // Send() is overridden to catch cases where the remote end is not responding |
| 108 | // and we fill the local socket buffer. When the buffer is full, this |
| 109 | // implementation of Send() will not block indefinitely as |
| 110 | // SyncSocket::Send will, but instead return 0, as no bytes could be sent. |
| 111 | // Note that the socket will not be closed in this case. |
| 112 | virtual size_t Send(const void* buffer, size_t length) OVERRIDE; |
| 113 | |
[email protected] | 532e9bd | 2012-01-25 12:04:17 | [diff] [blame] | 114 | private: |
| 115 | #if defined(OS_WIN) |
| 116 | WaitableEvent shutdown_event_; |
| 117 | WaitableEvent file_operation_; |
| 118 | #endif |
| 119 | DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
| 120 | }; |
| 121 | |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 122 | } // namespace base |
| 123 | |
| 124 | #endif // BASE_SYNC_SOCKET_H_ |