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 #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 the receiving is 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" | 20 #include "base/compiler_specific.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 Handle handle() const { return handle_; } | 70 Handle handle() const { return handle_; } |
71 | 71 |
72 protected: | 72 protected: |
73 Handle handle_; | 73 Handle handle_; |
74 | 74 |
75 private: | 75 private: |
76 DISALLOW_COPY_AND_ASSIGN(SyncSocket); | 76 DISALLOW_COPY_AND_ASSIGN(SyncSocket); |
77 }; | 77 }; |
78 | 78 |
79 // Derives from SyncSocket and adds support for shutting down the socket from | 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 | 80 // another thread while a blocking Receive or Send is being done from the thread |
81 // thread that owns the socket. | 81 // that owns the socket. |
82 class BASE_EXPORT CancelableSyncSocket : public SyncSocket { | 82 class BASE_EXPORT CancelableSyncSocket : public SyncSocket { |
83 public: | 83 public: |
84 CancelableSyncSocket(); | 84 CancelableSyncSocket(); |
85 explicit CancelableSyncSocket(Handle handle); | 85 explicit CancelableSyncSocket(Handle handle); |
86 virtual ~CancelableSyncSocket() {} | 86 virtual ~CancelableSyncSocket() {} |
87 | 87 |
88 // Initializes a pair of cancelable sockets. See documentation for | 88 // Initializes a pair of cancelable sockets. See documentation for |
89 // SyncSocket::CreatePair for more details. | 89 // SyncSocket::CreatePair for more details. |
90 static bool CreatePair(CancelableSyncSocket* socket_a, | 90 static bool CreatePair(CancelableSyncSocket* socket_a, |
91 CancelableSyncSocket* socket_b); | 91 CancelableSyncSocket* socket_b); |
92 | 92 |
93 // A way to shut down a socket even if another thread is currently performing | 93 // A way to shut down a socket even if another thread is currently performing |
94 // a blocking Receive or Send. | 94 // a blocking Receive or Send. |
95 bool Shutdown(); | 95 bool Shutdown(); |
96 | 96 |
97 #if defined(OS_WIN) | 97 #if defined(OS_WIN) |
98 // Since the Linux and Mac implementations actually use a socket, shutting | 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 | 99 // them down from another thread is pretty simple - we can just call |
100 // shutdown(). However, the Windows implementation relies on named pipes | 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 | 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 | 102 // supported on <Vista. So, for Windows only, we override these |
103 // SyncSocket methods in order to support shutting down the 'socket'. | 103 // SyncSocket methods in order to support shutting down the 'socket'. |
104 virtual bool Close() OVERRIDE; | 104 virtual bool Close() OVERRIDE; |
| 105 virtual size_t Send(const void* buffer, size_t length) OVERRIDE; |
105 virtual size_t Receive(void* buffer, size_t length) OVERRIDE; | 106 virtual size_t Receive(void* buffer, size_t length) OVERRIDE; |
106 #endif | 107 #endif |
107 | 108 |
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 | |
115 private: | 109 private: |
116 #if defined(OS_WIN) | 110 #if defined(OS_WIN) |
117 WaitableEvent shutdown_event_; | 111 WaitableEvent shutdown_event_; |
118 WaitableEvent file_operation_; | 112 WaitableEvent file_operation_; |
119 #endif | 113 #endif |
120 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); | 114 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
121 }; | 115 }; |
122 | 116 |
123 } // namespace base | 117 } // namespace base |
124 | 118 |
125 #endif // BASE_SYNC_SOCKET_H_ | 119 #endif // BASE_SYNC_SOCKET_H_ |
OLD | NEW |