Chromium Code Reviews| Index: base/sync_socket.h |
| diff --git a/base/sync_socket.h b/base/sync_socket.h |
| index 87b6a97a436b9828fede4584303d8560e2137ebc..2c86698e430fcc42ce6c4d8623c2b0f33b19416c 100644 |
| --- a/base/sync_socket.h |
| +++ b/base/sync_socket.h |
| @@ -17,6 +17,8 @@ |
| #include <sys/types.h> |
| #include "base/base_export.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/synchronization/waitable_event.h" |
| namespace base { |
| @@ -29,17 +31,19 @@ class BASE_EXPORT SyncSocket { |
| #endif |
| static const Handle kInvalidHandle; |
| + SyncSocket(); |
| + |
| // Creates a SyncSocket from a Handle. Used in transport. |
| - explicit SyncSocket(Handle handle) : handle_(handle) { } |
| - ~SyncSocket() { Close(); } |
| + explicit SyncSocket(Handle handle) : handle_(handle) {} |
| + virtual ~SyncSocket(); |
| - // Creates an unnamed pair of connected sockets. |
| - // pair is a pointer to an array of two SyncSockets in which connected socket |
| - // descriptors are returned. Returns true on success, false on failure. |
| - static bool CreatePair(SyncSocket* pair[2]); |
| + // Initializes and connects a pair of sockets. |
| + // |socket_a| and |socket_b| must not hold a valid handle. Upon successful |
| + // return, the sockets will both be valid and connected. |
| + static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b); |
| // Closes the SyncSocket. Returns true on success, false on failure. |
| - bool Close(); |
| + virtual bool Close(); |
| // Sends the message to the remote peer of the SyncSocket. |
| // Note it is not safe to send messages from the same socket handle by |
| @@ -47,13 +51,13 @@ class BASE_EXPORT SyncSocket { |
| // buffer is a pointer to the data to send. |
| // length is the length of the data to send (must be non-zero). |
| // Returns the number of bytes sent, or 0 upon failure. |
| - size_t Send(const void* buffer, size_t length); |
| + virtual size_t Send(const void* buffer, size_t length); |
| // Receives a message from an SyncSocket. |
| // buffer is a pointer to the buffer to receive data. |
| // length is the number of bytes of data to receive (must be non-zero). |
| // Returns the number of bytes received, or 0 upon failure. |
| - size_t Receive(void* buffer, size_t length); |
| + virtual size_t Receive(void* buffer, size_t length); |
| // Returns the number of bytes available. If non-zero, Receive() will not |
| // not block when called. NOTE: Some implementations cannot reliably |
| @@ -65,12 +69,49 @@ class BASE_EXPORT SyncSocket { |
| // processes. |
| Handle handle() const { return handle_; } |
| - private: |
| + protected: |
| Handle handle_; |
| + private: |
| DISALLOW_COPY_AND_ASSIGN(SyncSocket); |
| }; |
| +// Derives from SyncSocket and adds support for shutting down the socket from |
| +// another thread while a blocking Receive or Send is being done from the thread |
| +// that owns the socket. |
| +class BASE_EXPORT CancelableSyncSocket : public SyncSocket { |
| + public: |
| + CancelableSyncSocket(); |
| + explicit CancelableSyncSocket(Handle handle); |
| + virtual ~CancelableSyncSocket() {} |
| + |
| + // A way to shut down a socket even if another thread is currently performing |
| + // a blocking Receive or Send. |
| + bool Shutdown(); |
| + |
| +#if defined(OS_WIN) |
| + // Since the Linux and Mac implementations actually use a socket, shutting |
| + // them down from another thread is pretty simple - we can just call |
| + // shutdown(). However, the Windows implementation relies on named pipes |
| + // and there isn't a way to cancel a blocking synchronous Read that is |
| + // supported on <Vista. So, for Windows only, we override these |
| + // SyncSocket methods in order to support shutting down the 'socket'. |
| + virtual bool Close() OVERRIDE; |
| + virtual size_t Send(const void* buffer, size_t length) OVERRIDE; |
| + virtual size_t Receive(void* buffer, size_t length) OVERRIDE; |
| +#endif |
| + |
| + static bool CreatePair(CancelableSyncSocket* socket_a, |
|
darin (slow to review)
2012/01/25 00:54:59
micro-nit: perhaps it would be good to list this
tommi (sloooow) - chröme
2012/01/25 10:49:13
Done.
|
| + CancelableSyncSocket* socket_b); |
| + |
| + private: |
| +#if defined(OS_WIN) |
| + WaitableEvent shutdown_event_; |
| + WaitableEvent file_operation_; |
| +#endif |
| + DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket); |
| +}; |
| + |
| } // namespace base |
| #endif // BASE_SYNC_SOCKET_H_ |