| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_SYNC_SOCKET_H_ | 5 #ifndef BASE_SYNC_SOCKET_H_ |
| 6 #define BASE_SYNC_SOCKET_H_ | 6 #define BASE_SYNC_SOCKET_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 // A socket abstraction used for sending and receiving plain | 9 // A socket abstraction used for sending and receiving plain |
| 10 // data. Because they are blocking, they can be used to perform | 10 // data. Because they are blocking, they can be used to perform |
| 11 // rudimentary cross-process synchronization with low latency. | 11 // rudimentary cross-process synchronization with low latency. |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
| 15 #include <windows.h> | 15 #include <windows.h> |
| 16 #endif | 16 #endif |
| 17 #include <sys/types.h> | 17 #include <sys/types.h> |
| 18 | 18 |
| 19 #include "base/base_export.h" | 19 #include "base/base_export.h" |
| 20 #include "base/compiler_specific.h" |
| 21 #include "base/synchronization/waitable_event.h" |
| 20 | 22 |
| 21 namespace base { | 23 namespace base { |
| 22 | 24 |
| 23 class BASE_EXPORT SyncSocket { | 25 class BASE_EXPORT SyncSocket { |
| 24 public: | 26 public: |
| 25 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 26 typedef HANDLE Handle; | 28 typedef HANDLE Handle; |
| 27 #else | 29 #else |
| 28 typedef int Handle; | 30 typedef int Handle; |
| 29 #endif | 31 #endif |
| 30 | 32 |
| 33 SyncSocket(); |
| 34 |
| 31 // Creates a SyncSocket from a Handle. Used in transport. | 35 // Creates a SyncSocket from a Handle. Used in transport. |
| 32 explicit SyncSocket(Handle handle) : handle_(handle) { } | 36 explicit SyncSocket(Handle handle) : handle_(handle) {} |
| 33 ~SyncSocket() { Close(); } | 37 virtual ~SyncSocket(); |
| 34 | 38 |
| 35 // Creates an unnamed pair of connected sockets. | 39 // Creates an unnamed pair of connected sockets. |
| 36 // pair is a pointer to an array of two SyncSockets in which connected socket | 40 // |pair| is an array of two closed SyncSockets (they must not hold a valid |
| 37 // descriptors are returned. Returns true on success, false on failure. | 41 // handle) which will be connected together. |
| 38 static bool CreatePair(SyncSocket* pair[2]); | 42 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); |
| 39 | 43 |
| 40 // Closes the SyncSocket. Returns true on success, false on failure. | 44 // Closes the SyncSocket. Returns true on success, false on failure. |
| 41 bool Close(); | 45 virtual bool Close(); |
| 42 | 46 |
| 43 // Sends the message to the remote peer of the SyncSocket. | 47 // Sends the message to the remote peer of the SyncSocket. |
| 44 // Note it is not safe to send messages from the same socket handle by | 48 // Note it is not safe to send messages from the same socket handle by |
| 45 // multiple threads simultaneously. | 49 // multiple threads simultaneously. |
| 46 // buffer is a pointer to the data to send. | 50 // buffer is a pointer to the data to send. |
| 47 // length is the length of the data to send (must be non-zero). | 51 // length is the length of the data to send (must be non-zero). |
| 48 // Returns the number of bytes sent, or 0 upon failure. | 52 // Returns the number of bytes sent, or 0 upon failure. |
| 49 size_t Send(const void* buffer, size_t length); | 53 virtual size_t Send(const void* buffer, size_t length); |
| 50 | 54 |
| 51 // Receives a message from an SyncSocket. | 55 // Receives a message from an SyncSocket. |
| 52 // buffer is a pointer to the buffer to receive data. | 56 // buffer is a pointer to the buffer to receive data. |
| 53 // length is the number of bytes of data to receive (must be non-zero). | 57 // length is the number of bytes of data to receive (must be non-zero). |
| 54 // Returns the number of bytes received, or 0 upon failure. | 58 // Returns the number of bytes received, or 0 upon failure. |
| 55 size_t Receive(void* buffer, size_t length); | 59 virtual size_t Receive(void* buffer, size_t length); |
| 56 | 60 |
| 57 // Returns the number of bytes available. If non-zero, Receive() will not | 61 // Returns the number of bytes available. If non-zero, Receive() will not |
| 58 // not block when called. NOTE: Some implementations cannot reliably | 62 // not block when called. NOTE: Some implementations cannot reliably |
| 59 // determine the number of bytes available so avoid using the returned | 63 // determine the number of bytes available so avoid using the returned |
| 60 // size as a promise and simply test against zero. | 64 // size as a promise and simply test against zero. |
| 61 size_t Peek(); | 65 size_t Peek(); |
| 62 | 66 |
| 63 // Extracts the contained handle. Used for transferring between | 67 // Extracts the contained handle. Used for transferring between |
| 64 // processes. | 68 // processes. |
| 65 Handle handle() const { return handle_; } | 69 Handle handle() const { return handle_; } |
| 66 | 70 |
| 67 private: | 71 protected: |
| 68 Handle handle_; | 72 Handle handle_; |
| 69 | 73 |
| 74 private: |
| 70 DISALLOW_COPY_AND_ASSIGN(SyncSocket); | 75 DISALLOW_COPY_AND_ASSIGN(SyncSocket); |
| 71 }; | 76 }; |
| 72 | 77 |
| 78 // Derives from SyncSocket and adds support for shutting down the socket from |
| 79 // another thread while a blocking Receive or Send is being done from the thread |
| 80 // that owns the socket. |
| 81 class BASE_EXPORT CancelableSyncSocket : public SyncSocket { |
| 82 public: |
| 83 CancelableSyncSocket(); |
| 84 explicit CancelableSyncSocket(Handle handle); |
| 85 virtual ~CancelableSyncSocket() {} |
| 86 |
| 87 // A way to shut down a socket even if another thread is currently performing |
| 88 // a blocking Receive or Send. |
| 89 bool Shutdown(); |
| 90 |
| 91 #if defined(OS_WIN) |
| 92 // Since the Linux and Mac implementations actually use a socket, shutting |
| 93 // them down from another thread is pretty simple - we can just call |
| 94 // shutdown(). However, the Windows implementation relies on named pipes |
| 95 // and there isn't a way to cancel a blocking synchronous Read that is |
| 96 // supported on <Vista. So, for Windows only, we override these |
| 97 // SyncSocket methods in order to support shutting down the 'socket'. |
| 98 virtual bool Close() OVERRIDE; |
| 99 virtual size_t Send(const void* buffer, size_t length) OVERRIDE; |
| 100 virtual size_t Receive(void* buffer, size_t length) OVERRIDE; |
| 101 |
| 102 static bool CreatePair(CancelableSyncSocket* socket_a, |
| 103 CancelableSyncSocket* socket_b); |
| 104 #endif |
| 105 |
| 106 private: |
| 107 #if defined(OS_WIN) |
| 108 WaitableEvent shutdown_event_; |
| 109 WaitableEvent file_operation_; |
| 110 #endif |
| 111 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
| 112 }; |
| 113 |
| 73 } // namespace base | 114 } // namespace base |
| 74 | 115 |
| 75 #endif // BASE_SYNC_SOCKET_H_ | 116 #endif // BASE_SYNC_SOCKET_H_ |
| OLD | NEW |