Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: base/sync_socket.h

Issue 8965053: Implement support for a cancelable SyncSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add CreatePair explicitly to CancelableSyncSocket and fix bug in the posix Receive implementation Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/sync_socket_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 they are 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"
21 #include "base/synchronization/waitable_event.h"
20 22
21 namespace base { 23 namespace base {
22 24
23 class BASE_EXPORT SyncSocket { 25 class BASE_EXPORT SyncSocket {
24 public: 26 public:
25 #if defined(OS_WIN) 27 #if defined(OS_WIN)
26 typedef HANDLE Handle; 28 typedef HANDLE Handle;
27 #else 29 #else
28 typedef int Handle; 30 typedef int Handle;
29 #endif 31 #endif
30 static const Handle kInvalidHandle; 32 static const Handle kInvalidHandle;
31 33
34 SyncSocket();
35
32 // Creates a SyncSocket from a Handle. Used in transport. 36 // Creates a SyncSocket from a Handle. Used in transport.
33 explicit SyncSocket(Handle handle) : handle_(handle) { } 37 explicit SyncSocket(Handle handle) : handle_(handle) {}
34 ~SyncSocket() { Close(); } 38 virtual ~SyncSocket();
35 39
36 // Creates an unnamed pair of connected sockets. 40 // Initializes and connects a pair of sockets.
37 // pair is a pointer to an array of two SyncSockets in which connected socket 41 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful
38 // descriptors are returned. Returns true on success, false on failure. 42 // return, the sockets will both be valid and connected.
39 static bool CreatePair(SyncSocket* pair[2]); 43 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
40 44
41 // Closes the SyncSocket. Returns true on success, false on failure. 45 // Closes the SyncSocket. Returns true on success, false on failure.
42 bool Close(); 46 virtual bool Close();
43 47
44 // Sends the message to the remote peer of the SyncSocket. 48 // Sends the message to the remote peer of the SyncSocket.
45 // Note it is not safe to send messages from the same socket handle by 49 // Note it is not safe to send messages from the same socket handle by
46 // multiple threads simultaneously. 50 // multiple threads simultaneously.
47 // buffer is a pointer to the data to send. 51 // buffer is a pointer to the data to send.
48 // 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).
49 // Returns the number of bytes sent, or 0 upon failure. 53 // Returns the number of bytes sent, or 0 upon failure.
50 size_t Send(const void* buffer, size_t length); 54 virtual size_t Send(const void* buffer, size_t length);
51 55
52 // Receives a message from an SyncSocket. 56 // Receives a message from an SyncSocket.
53 // buffer is a pointer to the buffer to receive data. 57 // buffer is a pointer to the buffer to receive data.
54 // 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).
55 // Returns the number of bytes received, or 0 upon failure. 59 // Returns the number of bytes received, or 0 upon failure.
56 size_t Receive(void* buffer, size_t length); 60 virtual size_t Receive(void* buffer, size_t length);
57 61
58 // Returns the number of bytes available. If non-zero, Receive() will not 62 // Returns the number of bytes available. If non-zero, Receive() will not
59 // not block when called. NOTE: Some implementations cannot reliably 63 // not block when called. NOTE: Some implementations cannot reliably
60 // determine the number of bytes available so avoid using the returned 64 // determine the number of bytes available so avoid using the returned
61 // size as a promise and simply test against zero. 65 // size as a promise and simply test against zero.
62 size_t Peek(); 66 size_t Peek();
63 67
64 // Extracts the contained handle. Used for transferring between 68 // Extracts the contained handle. Used for transferring between
65 // processes. 69 // processes.
66 Handle handle() const { return handle_; } 70 Handle handle() const { return handle_; }
67 71
68 private: 72 protected:
69 Handle handle_; 73 Handle handle_;
70 74
75 private:
71 DISALLOW_COPY_AND_ASSIGN(SyncSocket); 76 DISALLOW_COPY_AND_ASSIGN(SyncSocket);
72 }; 77 };
73 78
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 thread
81 // that owns the socket.
82 class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
83 public:
84 CancelableSyncSocket();
85 explicit CancelableSyncSocket(Handle handle);
86 virtual ~CancelableSyncSocket() {}
87
88 // A way to shut down a socket even if another thread is currently performing
89 // a blocking Receive or Send.
90 bool Shutdown();
91
92 #if defined(OS_WIN)
93 // Since the Linux and Mac implementations actually use a socket, shutting
94 // them down from another thread is pretty simple - we can just call
95 // shutdown(). However, the Windows implementation relies on named pipes
96 // and there isn't a way to cancel a blocking synchronous Read that is
97 // supported on <Vista. So, for Windows only, we override these
98 // SyncSocket methods in order to support shutting down the 'socket'.
99 virtual bool Close() OVERRIDE;
100 virtual size_t Send(const void* buffer, size_t length) OVERRIDE;
101 virtual size_t Receive(void* buffer, size_t length) OVERRIDE;
102 #endif
103
104 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.
105 CancelableSyncSocket* socket_b);
106
107 private:
108 #if defined(OS_WIN)
109 WaitableEvent shutdown_event_;
110 WaitableEvent file_operation_;
111 #endif
112 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
113 };
114
74 } // namespace base 115 } // namespace base
75 116
76 #endif // BASE_SYNC_SOCKET_H_ 117 #endif // BASE_SYNC_SOCKET_H_
OLDNEW
« no previous file with comments | « no previous file | base/sync_socket_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698