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_
8 // A socket abstraction used for sending and receiving plain
9 // data. Because the receiving is blocking, they can be used to perform
10 // rudimentary cross-process synchronization with low latency.
12 #include "base/basictypes.h"
16 #include <sys/types.h>
18 #include "base/base_export.h"
19 #include "base/compiler_specific.h"
20 #include "base/synchronization/waitable_event.h"
21 #include "base/time/time.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 // Same as Receive() but only blocks for data until |timeout| has elapsed or
63 // |buffer| |length| is exhausted. Currently only timeouts less than one
64 // second are allowed. Return the amount of data read.
65 virtual size_t ReceiveWithTimeout(void* buffer
,
69 // Returns the number of bytes available. If non-zero, Receive() will not
70 // not block when called. NOTE: Some implementations cannot reliably
71 // determine the number of bytes available so avoid using the returned
72 // size as a promise and simply test against zero.
75 // Extracts the contained handle. Used for transferring between
77 Handle
handle() const { return handle_
; }
83 DISALLOW_COPY_AND_ASSIGN(SyncSocket
);
86 // Derives from SyncSocket and adds support for shutting down the socket from
87 // another thread while a blocking Receive or Send is being done from the
88 // thread that owns the socket.
89 class BASE_EXPORT CancelableSyncSocket
: public SyncSocket
{
91 CancelableSyncSocket();
92 explicit CancelableSyncSocket(Handle handle
);
93 virtual ~CancelableSyncSocket() {}
95 // Initializes a pair of cancelable sockets. See documentation for
96 // SyncSocket::CreatePair for more details.
97 static bool CreatePair(CancelableSyncSocket
* socket_a
,
98 CancelableSyncSocket
* socket_b
);
100 // A way to shut down a socket even if another thread is currently performing
101 // a blocking Receive or Send.
105 // Since the Linux and Mac implementations actually use a socket, shutting
106 // them down from another thread is pretty simple - we can just call
107 // shutdown(). However, the Windows implementation relies on named pipes
108 // and there isn't a way to cancel a blocking synchronous Read that is
109 // supported on <Vista. So, for Windows only, we override these
110 // SyncSocket methods in order to support shutting down the 'socket'.
111 virtual bool Close() OVERRIDE
;
112 virtual size_t Receive(void* buffer
, size_t length
) OVERRIDE
;
113 virtual size_t ReceiveWithTimeout(void* buffer
,
115 TimeDelta timeout
) OVERRIDE
;
118 // Send() is overridden to catch cases where the remote end is not responding
119 // and we fill the local socket buffer. When the buffer is full, this
120 // implementation of Send() will not block indefinitely as
121 // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
122 // Note that the socket will not be closed in this case.
123 virtual size_t Send(const void* buffer
, size_t length
) OVERRIDE
;
127 WaitableEvent shutdown_event_
;
128 WaitableEvent file_operation_
;
130 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket
);
133 #if defined(OS_WIN) && !defined(COMPONENT_BUILD)
134 // TODO(cpu): remove this once chrome is split in two dlls.
135 __declspec(selectany
)
136 const SyncSocket::Handle
SyncSocket::kInvalidHandle
= INVALID_HANDLE_VALUE
;
141 #endif // BASE_SYNC_SOCKET_H_