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) | 13 #if defined(OS_WIN) |
14 #include <windows.h> | 14 #include <windows.h> |
15 #endif | 15 #endif |
16 #include <sys/types.h> | 16 #include <sys/types.h> |
17 | 17 |
18 #include "base/base_export.h" | 18 #include "base/base_export.h" |
19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
| 20 #include "base/process/process_handle.h" |
20 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
22 | 23 |
| 24 #if defined(OS_POSIX) |
| 25 #include "base/file_descriptor_posix.h" |
| 26 #endif |
| 27 |
23 namespace base { | 28 namespace base { |
24 | 29 |
25 class BASE_EXPORT SyncSocket { | 30 class BASE_EXPORT SyncSocket { |
26 public: | 31 public: |
27 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
28 typedef HANDLE Handle; | 33 typedef HANDLE Handle; |
| 34 typedef Handle TransitDescriptor; |
29 #else | 35 #else |
30 typedef int Handle; | 36 typedef int Handle; |
| 37 typedef FileDescriptor TransitDescriptor; |
31 #endif | 38 #endif |
32 static const Handle kInvalidHandle; | 39 static const Handle kInvalidHandle; |
33 | 40 |
34 SyncSocket(); | 41 SyncSocket(); |
35 | 42 |
36 // Creates a SyncSocket from a Handle. Used in transport. | 43 // Creates a SyncSocket from a Handle. Used in transport. |
37 explicit SyncSocket(Handle handle) : handle_(handle) {} | 44 explicit SyncSocket(Handle handle) : handle_(handle) {} |
38 virtual ~SyncSocket(); | 45 virtual ~SyncSocket(); |
39 | 46 |
40 // Initializes and connects a pair of sockets. | 47 // Initializes and connects a pair of sockets. |
41 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful | 48 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful |
42 // return, the sockets will both be valid and connected. | 49 // return, the sockets will both be valid and connected. |
43 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); | 50 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); |
44 | 51 |
| 52 // Returns |Handle| wrapped in a |TransitDescriptor|. |
| 53 static Handle UnwrapHandle(const TransitDescriptor& descriptor); |
| 54 |
| 55 // Prepares a |TransitDescriptor| which wraps |Handle| used for transit. |
| 56 // This is used to prepare the underlying shared resource before passing back |
| 57 // the handle to be used by the peer process. |
| 58 bool PrepareTransitDescriptor(ProcessHandle peer_process_handle, |
| 59 TransitDescriptor* descriptor); |
| 60 |
45 // Closes the SyncSocket. Returns true on success, false on failure. | 61 // Closes the SyncSocket. Returns true on success, false on failure. |
46 virtual bool Close(); | 62 virtual bool Close(); |
47 | 63 |
48 // Sends the message to the remote peer of the SyncSocket. | 64 // 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 | 65 // Note it is not safe to send messages from the same socket handle by |
50 // multiple threads simultaneously. | 66 // multiple threads simultaneously. |
51 // buffer is a pointer to the data to send. | 67 // buffer is a pointer to the data to send. |
52 // length is the length of the data to send (must be non-zero). | 68 // length is the length of the data to send (must be non-zero). |
53 // Returns the number of bytes sent, or 0 upon failure. | 69 // Returns the number of bytes sent, or 0 upon failure. |
54 virtual size_t Send(const void* buffer, size_t length); | 70 virtual size_t Send(const void* buffer, size_t length); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 148 |
133 #if defined(OS_WIN) && !defined(COMPONENT_BUILD) | 149 #if defined(OS_WIN) && !defined(COMPONENT_BUILD) |
134 // TODO(cpu): remove this once chrome is split in two dlls. | 150 // TODO(cpu): remove this once chrome is split in two dlls. |
135 __declspec(selectany) | 151 __declspec(selectany) |
136 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; | 152 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; |
137 #endif | 153 #endif |
138 | 154 |
139 } // namespace base | 155 } // namespace base |
140 | 156 |
141 #endif // BASE_SYNC_SOCKET_H_ | 157 #endif // BASE_SYNC_SOCKET_H_ |
OLD | NEW |