Chromium Code Reviews| 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/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
| 21 #include "base/time/time.h" | |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 | 24 |
| 24 class BASE_EXPORT SyncSocket { | 25 class BASE_EXPORT SyncSocket { |
| 25 public: | 26 public: |
| 26 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 27 typedef HANDLE Handle; | 28 typedef HANDLE Handle; |
| 28 #else | 29 #else |
| 29 typedef int Handle; | 30 typedef int Handle; |
| 30 #endif | 31 #endif |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 51 // 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). |
| 52 // Returns the number of bytes sent, or 0 upon failure. | 53 // Returns the number of bytes sent, or 0 upon failure. |
| 53 virtual size_t Send(const void* buffer, size_t length); | 54 virtual size_t Send(const void* buffer, size_t length); |
| 54 | 55 |
| 55 // Receives a message from an SyncSocket. | 56 // Receives a message from an SyncSocket. |
| 56 // buffer is a pointer to the buffer to receive data. | 57 // buffer is a pointer to the buffer to receive data. |
| 57 // 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). |
| 58 // Returns the number of bytes received, or 0 upon failure. | 59 // Returns the number of bytes received, or 0 upon failure. |
| 59 virtual size_t Receive(void* buffer, size_t length); | 60 virtual size_t Receive(void* buffer, size_t length); |
| 60 | 61 |
| 62 // Same as Receive() but only blocks for data until |timeout| has elapsed. | |
|
jar (doing other things)
2013/10/14 15:31:07
nit: "... or |buffer| |length| is exhausted."
DaleCurtis
2013/10/14 19:25:13
Done.
| |
| 63 // Currently only timeouts less than one second are allowed. Timeouts will | |
| 64 // return the amount of data read prior to timeout. | |
|
jar (doing other things)
2013/10/14 15:31:07
nit: Remove leading words "Timeout will" and trail
DaleCurtis
2013/10/14 19:25:13
Done.
| |
| 65 virtual size_t ReceiveWithTimeout(void* buffer, | |
| 66 size_t length, | |
| 67 TimeDelta timeout); | |
| 68 | |
| 61 // Returns the number of bytes available. If non-zero, Receive() will not | 69 // Returns the number of bytes available. If non-zero, Receive() will not |
| 62 // not block when called. NOTE: Some implementations cannot reliably | 70 // not block when called. NOTE: Some implementations cannot reliably |
| 63 // determine the number of bytes available so avoid using the returned | 71 // determine the number of bytes available so avoid using the returned |
| 64 // size as a promise and simply test against zero. | 72 // size as a promise and simply test against zero. |
| 65 size_t Peek(); | 73 size_t Peek(); |
| 66 | 74 |
| 67 // Extracts the contained handle. Used for transferring between | 75 // Extracts the contained handle. Used for transferring between |
| 68 // processes. | 76 // processes. |
| 69 Handle handle() const { return handle_; } | 77 Handle handle() const { return handle_; } |
| 70 | 78 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 95 | 103 |
| 96 #if defined(OS_WIN) | 104 #if defined(OS_WIN) |
| 97 // Since the Linux and Mac implementations actually use a socket, shutting | 105 // Since the Linux and Mac implementations actually use a socket, shutting |
| 98 // them down from another thread is pretty simple - we can just call | 106 // them down from another thread is pretty simple - we can just call |
| 99 // shutdown(). However, the Windows implementation relies on named pipes | 107 // shutdown(). However, the Windows implementation relies on named pipes |
| 100 // and there isn't a way to cancel a blocking synchronous Read that is | 108 // and there isn't a way to cancel a blocking synchronous Read that is |
| 101 // supported on <Vista. So, for Windows only, we override these | 109 // supported on <Vista. So, for Windows only, we override these |
| 102 // SyncSocket methods in order to support shutting down the 'socket'. | 110 // SyncSocket methods in order to support shutting down the 'socket'. |
| 103 virtual bool Close() OVERRIDE; | 111 virtual bool Close() OVERRIDE; |
| 104 virtual size_t Receive(void* buffer, size_t length) OVERRIDE; | 112 virtual size_t Receive(void* buffer, size_t length) OVERRIDE; |
| 113 virtual size_t ReceiveWithTimeout(void* buffer, | |
| 114 size_t length, | |
| 115 TimeDelta timeout) OVERRIDE; | |
| 105 #endif | 116 #endif |
| 106 | 117 |
| 107 // Send() is overridden to catch cases where the remote end is not responding | 118 // Send() is overridden to catch cases where the remote end is not responding |
| 108 // and we fill the local socket buffer. When the buffer is full, this | 119 // and we fill the local socket buffer. When the buffer is full, this |
| 109 // implementation of Send() will not block indefinitely as | 120 // implementation of Send() will not block indefinitely as |
| 110 // SyncSocket::Send will, but instead return 0, as no bytes could be sent. | 121 // SyncSocket::Send will, but instead return 0, as no bytes could be sent. |
| 111 // Note that the socket will not be closed in this case. | 122 // Note that the socket will not be closed in this case. |
| 112 virtual size_t Send(const void* buffer, size_t length) OVERRIDE; | 123 virtual size_t Send(const void* buffer, size_t length) OVERRIDE; |
| 113 | 124 |
| 114 private: | 125 private: |
| 115 #if defined(OS_WIN) | 126 #if defined(OS_WIN) |
| 116 WaitableEvent shutdown_event_; | 127 WaitableEvent shutdown_event_; |
| 117 WaitableEvent file_operation_; | 128 WaitableEvent file_operation_; |
| 118 #endif | 129 #endif |
| 119 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); | 130 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
| 120 }; | 131 }; |
| 121 | 132 |
| 122 #if defined(OS_WIN) && !defined(COMPONENT_BUILD) | 133 #if defined(OS_WIN) && !defined(COMPONENT_BUILD) |
| 123 // TODO(cpu): remove this once chrome is split in two dlls. | 134 // TODO(cpu): remove this once chrome is split in two dlls. |
| 124 __declspec(selectany) | 135 __declspec(selectany) |
| 125 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; | 136 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; |
| 126 #endif | 137 #endif |
| 127 | 138 |
| 128 } // namespace base | 139 } // namespace base |
| 129 | 140 |
| 130 #endif // BASE_SYNC_SOCKET_H_ | 141 #endif // BASE_SYNC_SOCKET_H_ |
| OLD | NEW |