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
3 // found in the LICENSE file.
5 #ifndef BASE_SYNC_SOCKET_H_
6 #define BASE_SYNC_SOCKET_H_
9 // A socket abstraction used for sending and receiving plain
10 // data. Because they are blocking, they can be used to perform
11 // rudimentary cross-process synchronization with low latency.
13 #include "base/basictypes.h"
17 #include <sys/types.h>
19 #include "base/base_export.h"
20 #include "base/compiler_specific.h"
21 #include "base/synchronization/waitable_event.h"
25 class BASE_EXPORT SyncSocket
{
28 typedef HANDLE Handle
;
32 static const Handle kInvalidHandle
;
36 // Creates a SyncSocket from a Handle. Used in transport.
37 explicit SyncSocket(Handle handle
) : handle_(handle
) {}
38 virtual ~SyncSocket();
40 // Initializes and connects a pair of sockets.
41 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful
42 // return, the sockets will both be valid and connected.
43 static bool CreatePair(SyncSocket
* socket_a
, SyncSocket
* socket_b
);
45 // Closes the SyncSocket. Returns true on success, false on failure.
48 // 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
50 // multiple threads simultaneously.
51 // buffer is a pointer to the data to send.
52 // length is the length of the data to send (must be non-zero).
53 // Returns the number of bytes sent, or 0 upon failure.
54 virtual size_t Send(const void* buffer
, size_t length
);
56 // Receives a message from an SyncSocket.
57 // buffer is a pointer to the buffer to receive data.
58 // length is the number of bytes of data to receive (must be non-zero).
59 // Returns the number of bytes received, or 0 upon failure.
60 virtual size_t Receive(void* buffer
, size_t length
);
62 // Returns the number of bytes available. If non-zero, Receive() will not
63 // not block when called. NOTE: Some implementations cannot reliably
64 // determine the number of bytes available so avoid using the returned
65 // size as a promise and simply test against zero.
68 // Extracts the contained handle. Used for transferring between
70 Handle
handle() const { return handle_
; }
76 DISALLOW_COPY_AND_ASSIGN(SyncSocket
);
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
{
84 CancelableSyncSocket();
85 explicit CancelableSyncSocket(Handle handle
);
86 virtual ~CancelableSyncSocket() {}
88 // Initializes a pair of cancelable sockets. See documentation for
89 // SyncSocket::CreatePair for more details.
90 static bool CreatePair(CancelableSyncSocket
* socket_a
,
91 CancelableSyncSocket
* socket_b
);
93 // A way to shut down a socket even if another thread is currently performing
94 // a blocking Receive or Send.
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
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
102 // supported on <Vista. So, for Windows only, we override these
103 // SyncSocket methods in order to support shutting down the 'socket'.
104 virtual bool Close() OVERRIDE
;
105 virtual size_t Send(const void* buffer
, size_t length
) OVERRIDE
;
106 virtual size_t Receive(void* buffer
, size_t length
) OVERRIDE
;
111 WaitableEvent shutdown_event_
;
112 WaitableEvent file_operation_
;
114 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket
);
119 #endif // BASE_SYNC_SOCKET_H_