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