OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
8 // A socket abstraction used for sending and receiving plain | 8 // A socket abstraction used for sending and receiving plain |
9 // data. Because the receiving is blocking, they can be used to perform | 9 // data. Because the receiving is blocking, they can be used to perform |
10 // rudimentary cross-process synchronization with low latency. | 10 // rudimentary cross-process synchronization with low latency. |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #if defined(OS_WIN) | |
14 #include <windows.h> | |
15 #endif | |
16 #include <sys/types.h> | 13 #include <sys/types.h> |
17 | 14 |
18 #include "base/base_export.h" | 15 #include "base/base_export.h" |
19 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
20 #include "base/process/process_handle.h" | 17 #include "base/process/process_handle.h" |
21 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
22 #include "base/time/time.h" | 19 #include "base/time/time.h" |
23 | 20 |
24 #if defined(OS_POSIX) | 21 #if defined(OS_POSIX) |
25 #include "base/file_descriptor_posix.h" | 22 #include "base/file_descriptor_posix.h" |
26 #endif | 23 #endif |
27 | 24 |
28 namespace base { | 25 namespace base { |
29 | 26 |
30 class BASE_EXPORT SyncSocket { | 27 class BASE_EXPORT SyncSocket { |
31 public: | 28 public: |
32 #if defined(OS_WIN) | |
33 typedef HANDLE Handle; | |
34 typedef Handle TransitDescriptor; | |
35 #else | |
36 typedef int Handle; | 29 typedef int Handle; |
37 typedef FileDescriptor TransitDescriptor; | 30 typedef FileDescriptor TransitDescriptor; |
38 #endif | |
39 static const Handle kInvalidHandle; | 31 static const Handle kInvalidHandle; |
40 | 32 |
41 SyncSocket(); | 33 SyncSocket(); |
42 | 34 |
43 // Creates a SyncSocket from a Handle. Used in transport. | 35 // Creates a SyncSocket from a Handle. Used in transport. |
44 explicit SyncSocket(Handle handle) : handle_(handle) {} | 36 explicit SyncSocket(Handle handle) : handle_(handle) {} |
45 virtual ~SyncSocket(); | 37 virtual ~SyncSocket(); |
46 | 38 |
47 // Initializes and connects a pair of sockets. | 39 // Initializes and connects a pair of sockets. |
48 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful | 40 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 102 |
111 // Initializes a pair of cancelable sockets. See documentation for | 103 // Initializes a pair of cancelable sockets. See documentation for |
112 // SyncSocket::CreatePair for more details. | 104 // SyncSocket::CreatePair for more details. |
113 static bool CreatePair(CancelableSyncSocket* socket_a, | 105 static bool CreatePair(CancelableSyncSocket* socket_a, |
114 CancelableSyncSocket* socket_b); | 106 CancelableSyncSocket* socket_b); |
115 | 107 |
116 // A way to shut down a socket even if another thread is currently performing | 108 // A way to shut down a socket even if another thread is currently performing |
117 // a blocking Receive or Send. | 109 // a blocking Receive or Send. |
118 bool Shutdown(); | 110 bool Shutdown(); |
119 | 111 |
120 #if defined(OS_WIN) | |
121 // Since the Linux and Mac implementations actually use a socket, shutting | |
122 // them down from another thread is pretty simple - we can just call | |
123 // shutdown(). However, the Windows implementation relies on named pipes | |
124 // and there isn't a way to cancel a blocking synchronous Read that is | |
125 // supported on <Vista. So, for Windows only, we override these | |
126 // SyncSocket methods in order to support shutting down the 'socket'. | |
127 bool Close() override; | |
128 size_t Receive(void* buffer, size_t length) override; | |
129 size_t ReceiveWithTimeout(void* buffer, | |
130 size_t length, | |
131 TimeDelta timeout) override; | |
132 #endif | |
133 | |
134 // Send() is overridden to catch cases where the remote end is not responding | 112 // Send() is overridden to catch cases where the remote end is not responding |
135 // and we fill the local socket buffer. When the buffer is full, this | 113 // and we fill the local socket buffer. When the buffer is full, this |
136 // implementation of Send() will not block indefinitely as | 114 // implementation of Send() will not block indefinitely as |
137 // SyncSocket::Send will, but instead return 0, as no bytes could be sent. | 115 // SyncSocket::Send will, but instead return 0, as no bytes could be sent. |
138 // Note that the socket will not be closed in this case. | 116 // Note that the socket will not be closed in this case. |
139 size_t Send(const void* buffer, size_t length) override; | 117 size_t Send(const void* buffer, size_t length) override; |
140 | 118 |
141 private: | 119 private: |
142 #if defined(OS_WIN) | |
143 WaitableEvent shutdown_event_; | |
144 WaitableEvent file_operation_; | |
145 #endif | |
146 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); | 120 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
147 }; | 121 }; |
148 | 122 |
149 #if defined(OS_WIN) && !defined(COMPONENT_BUILD) | |
150 // TODO(cpu): remove this once chrome is split in two dlls. | |
151 __declspec(selectany) | |
152 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; | |
153 #endif | |
154 | |
155 } // namespace base | 123 } // namespace base |
156 | 124 |
157 #endif // BASE_SYNC_SOCKET_H_ | 125 #endif // BASE_SYNC_SOCKET_H_ |
OLD | NEW |