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 static const Handle kInvalidHandle; | 32 static const Handle kInvalidHandle; |
31 | 33 |
| 34 SyncSocket(); |
| 35 |
32 // Creates a SyncSocket from a Handle. Used in transport. | 36 // Creates a SyncSocket from a Handle. Used in transport. |
33 explicit SyncSocket(Handle handle) : handle_(handle) { } | 37 explicit SyncSocket(Handle handle) : handle_(handle) {} |
34 ~SyncSocket() { Close(); } | 38 virtual ~SyncSocket(); |
35 | 39 |
36 // Creates an unnamed pair of connected sockets. | 40 // Initializes and connects a pair of sockets. |
37 // pair is a pointer to an array of two SyncSockets in which connected socket | 41 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful |
38 // descriptors are returned. Returns true on success, false on failure. | 42 // return, the sockets will both be valid and connected. |
39 static bool CreatePair(SyncSocket* pair[2]); | 43 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); |
40 | 44 |
41 // Closes the SyncSocket. Returns true on success, false on failure. | 45 // Closes the SyncSocket. Returns true on success, false on failure. |
42 bool Close(); | 46 virtual bool Close(); |
43 | 47 |
44 // Sends the message to the remote peer of the SyncSocket. | 48 // Sends the message to the remote peer of the SyncSocket. |
45 // Note it is not safe to send messages from the same socket handle by | 49 // Note it is not safe to send messages from the same socket handle by |
46 // multiple threads simultaneously. | 50 // multiple threads simultaneously. |
47 // buffer is a pointer to the data to send. | 51 // buffer is a pointer to the data to send. |
48 // length is the length of the data to send (must be non-zero). | 52 // length is the length of the data to send (must be non-zero). |
49 // Returns the number of bytes sent, or 0 upon failure. | 53 // Returns the number of bytes sent, or 0 upon failure. |
50 size_t Send(const void* buffer, size_t length); | 54 virtual size_t Send(const void* buffer, size_t length); |
51 | 55 |
52 // Receives a message from an SyncSocket. | 56 // Receives a message from an SyncSocket. |
53 // buffer is a pointer to the buffer to receive data. | 57 // buffer is a pointer to the buffer to receive data. |
54 // length is the number of bytes of data to receive (must be non-zero). | 58 // length is the number of bytes of data to receive (must be non-zero). |
55 // Returns the number of bytes received, or 0 upon failure. | 59 // Returns the number of bytes received, or 0 upon failure. |
56 size_t Receive(void* buffer, size_t length); | 60 virtual size_t Receive(void* buffer, size_t length); |
57 | 61 |
58 // Returns the number of bytes available. If non-zero, Receive() will not | 62 // Returns the number of bytes available. If non-zero, Receive() will not |
59 // not block when called. NOTE: Some implementations cannot reliably | 63 // not block when called. NOTE: Some implementations cannot reliably |
60 // determine the number of bytes available so avoid using the returned | 64 // determine the number of bytes available so avoid using the returned |
61 // size as a promise and simply test against zero. | 65 // size as a promise and simply test against zero. |
62 size_t Peek(); | 66 size_t Peek(); |
63 | 67 |
64 // Extracts the contained handle. Used for transferring between | 68 // Extracts the contained handle. Used for transferring between |
65 // processes. | 69 // processes. |
66 Handle handle() const { return handle_; } | 70 Handle handle() const { return handle_; } |
67 | 71 |
68 private: | 72 protected: |
69 Handle handle_; | 73 Handle handle_; |
70 | 74 |
| 75 private: |
71 DISALLOW_COPY_AND_ASSIGN(SyncSocket); | 76 DISALLOW_COPY_AND_ASSIGN(SyncSocket); |
72 }; | 77 }; |
73 | 78 |
| 79 // Derives from SyncSocket and adds support for shutting down the socket from |
| 80 // another thread while a blocking Receive or Send is being done from the thread |
| 81 // that owns the socket. |
| 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; |
| 105 virtual size_t Send(const void* buffer, size_t length) OVERRIDE; |
| 106 virtual size_t Receive(void* buffer, size_t length) OVERRIDE; |
| 107 #endif |
| 108 |
| 109 private: |
| 110 #if defined(OS_WIN) |
| 111 WaitableEvent shutdown_event_; |
| 112 WaitableEvent file_operation_; |
| 113 #endif |
| 114 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
| 115 }; |
| 116 |
74 } // namespace base | 117 } // namespace base |
75 | 118 |
76 #endif // BASE_SYNC_SOCKET_H_ | 119 #endif // BASE_SYNC_SOCKET_H_ |
OLD | NEW |