blob: 71addd1e163a9ab16d201a961ba3ae5b3ed6972f [file] [log] [blame]
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
sehr@google.com4f325aa2009-11-25 01:14:53 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_SYNC_SOCKET_H_
6#define BASE_SYNC_SOCKET_H_
7
8// A socket abstraction used for sending and receiving plain
xians@chromium.orgf7d9af82012-04-19 19:23:03 +09009// data. Because the receiving is blocking, they can be used to perform
sehr@google.com4f325aa2009-11-25 01:14:53 +090010// rudimentary cross-process synchronization with low latency.
11
12#include "base/basictypes.h"
13#if defined(OS_WIN)
14#include <windows.h>
15#endif
16#include <sys/types.h>
17
darin@chromium.orge585bed2011-08-06 00:34:00 +090018#include "base/base_export.h"
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090019#include "base/compiler_specific.h"
20#include "base/synchronization/waitable_event.h"
dalecurtis@chromium.org96b19df2013-10-20 07:13:19 +090021#include "base/time/time.h"
rvargas@google.comc3b65182011-03-29 08:48:44 +090022
sehr@google.com4f325aa2009-11-25 01:14:53 +090023namespace base {
24
darin@chromium.orge585bed2011-08-06 00:34:00 +090025class BASE_EXPORT SyncSocket {
sehr@google.com4f325aa2009-11-25 01:14:53 +090026 public:
27#if defined(OS_WIN)
28 typedef HANDLE Handle;
29#else
30 typedef int Handle;
31#endif
groby@chromium.orge870b642011-12-23 04:33:27 +090032 static const Handle kInvalidHandle;
sehr@google.com4f325aa2009-11-25 01:14:53 +090033
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090034 SyncSocket();
sehr@google.com4f325aa2009-11-25 01:14:53 +090035
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090036 // Creates a SyncSocket from a Handle. Used in transport.
37 explicit SyncSocket(Handle handle) : handle_(handle) {}
38 virtual ~SyncSocket();
39
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);
sehr@google.com4f325aa2009-11-25 01:14:53 +090044
45 // Closes the SyncSocket. Returns true on success, false on failure.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090046 virtual bool Close();
sehr@google.com4f325aa2009-11-25 01:14:53 +090047
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.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090054 virtual size_t Send(const void* buffer, size_t length);
sehr@google.com4f325aa2009-11-25 01:14:53 +090055
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.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090060 virtual size_t Receive(void* buffer, size_t length);
sehr@google.com4f325aa2009-11-25 01:14:53 +090061
dalecurtis@chromium.org96b19df2013-10-20 07:13:19 +090062 // 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,
66 size_t length,
67 TimeDelta timeout);
68
cpu@chromium.org750392c2009-12-05 07:53:22 +090069 // 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.
73 size_t Peek();
74
sehr@google.com4f325aa2009-11-25 01:14:53 +090075 // Extracts the contained handle. Used for transferring between
76 // processes.
77 Handle handle() const { return handle_; }
78
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090079 protected:
sehr@google.com4f325aa2009-11-25 01:14:53 +090080 Handle handle_;
81
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090082 private:
sehr@google.com4f325aa2009-11-25 01:14:53 +090083 DISALLOW_COPY_AND_ASSIGN(SyncSocket);
84};
85
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090086// Derives from SyncSocket and adds support for shutting down the socket from
xians@chromium.orgf7d9af82012-04-19 19:23:03 +090087// another thread while a blocking Receive or Send is being done from the
88// thread that owns the socket.
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +090089class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
90 public:
91 CancelableSyncSocket();
92 explicit CancelableSyncSocket(Handle handle);
93 virtual ~CancelableSyncSocket() {}
94
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);
99
100 // A way to shut down a socket even if another thread is currently performing
101 // a blocking Receive or Send.
102 bool Shutdown();
103
104#if defined(OS_WIN)
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;
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900112 virtual size_t Receive(void* buffer, size_t length) OVERRIDE;
dalecurtis@chromium.org96b19df2013-10-20 07:13:19 +0900113 virtual size_t ReceiveWithTimeout(void* buffer,
114 size_t length,
115 TimeDelta timeout) OVERRIDE;
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900116#endif
117
xians@chromium.orgf7d9af82012-04-19 19:23:03 +0900118 // 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;
124
tommi@chromium.orgd5f359a2012-01-25 21:04:17 +0900125 private:
126#if defined(OS_WIN)
127 WaitableEvent shutdown_event_;
128 WaitableEvent file_operation_;
129#endif
130 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
131};
132
cpu@chromium.org434a48c2013-05-23 13:34:11 +0900133#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;
137#endif
138
sehr@google.com4f325aa2009-11-25 01:14:53 +0900139} // namespace base
140
141#endif // BASE_SYNC_SOCKET_H_