blob: eb3eeb5123c615dd4dbdc6238bf0f4fe6fd957b8 [file] [log] [blame]
[email protected]532e9bd2012-01-25 12:04:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0840cc72009-11-24 16:14:532// 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
[email protected]5d272092012-04-19 10:23:039// data. Because the receiving is blocking, they can be used to perform
[email protected]0840cc72009-11-24 16:14:5310// 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
[email protected]0bea7252011-08-05 15:34:0018#include "base/base_export.h"
[email protected]532e9bd2012-01-25 12:04:1719#include "base/compiler_specific.h"
20#include "base/synchronization/waitable_event.h"
[email protected]9493ee95c2011-03-28 23:48:4421
[email protected]0840cc72009-11-24 16:14:5322namespace base {
23
[email protected]0bea7252011-08-05 15:34:0024class BASE_EXPORT SyncSocket {
[email protected]0840cc72009-11-24 16:14:5325 public:
26#if defined(OS_WIN)
27 typedef HANDLE Handle;
28#else
29 typedef int Handle;
30#endif
[email protected]5895ee12011-12-22 19:33:2731 static const Handle kInvalidHandle;
[email protected]0840cc72009-11-24 16:14:5332
[email protected]532e9bd2012-01-25 12:04:1733 SyncSocket();
[email protected]0840cc72009-11-24 16:14:5334
[email protected]532e9bd2012-01-25 12:04:1735 // Creates a SyncSocket from a Handle. Used in transport.
36 explicit SyncSocket(Handle handle) : handle_(handle) {}
37 virtual ~SyncSocket();
38
39 // Initializes and connects a pair of sockets.
40 // |socket_a| and |socket_b| must not hold a valid handle. Upon successful
41 // return, the sockets will both be valid and connected.
42 static bool CreatePair(SyncSocket* socket_a, SyncSocket* socket_b);
[email protected]0840cc72009-11-24 16:14:5343
44 // Closes the SyncSocket. Returns true on success, false on failure.
[email protected]532e9bd2012-01-25 12:04:1745 virtual bool Close();
[email protected]0840cc72009-11-24 16:14:5346
47 // Sends the message to the remote peer of the SyncSocket.
48 // Note it is not safe to send messages from the same socket handle by
49 // multiple threads simultaneously.
50 // buffer is a pointer to the data to send.
51 // length is the length of the data to send (must be non-zero).
52 // Returns the number of bytes sent, or 0 upon failure.
[email protected]532e9bd2012-01-25 12:04:1753 virtual size_t Send(const void* buffer, size_t length);
[email protected]0840cc72009-11-24 16:14:5354
55 // Receives a message from an SyncSocket.
56 // buffer is a pointer to the buffer to receive data.
57 // length is the number of bytes of data to receive (must be non-zero).
58 // Returns the number of bytes received, or 0 upon failure.
[email protected]532e9bd2012-01-25 12:04:1759 virtual size_t Receive(void* buffer, size_t length);
[email protected]0840cc72009-11-24 16:14:5360
[email protected]d8b65912009-12-04 22:53:2261 // Returns the number of bytes available. If non-zero, Receive() will not
62 // not block when called. NOTE: Some implementations cannot reliably
63 // determine the number of bytes available so avoid using the returned
64 // size as a promise and simply test against zero.
65 size_t Peek();
66
[email protected]0840cc72009-11-24 16:14:5367 // Extracts the contained handle. Used for transferring between
68 // processes.
69 Handle handle() const { return handle_; }
70
[email protected]532e9bd2012-01-25 12:04:1771 protected:
[email protected]0840cc72009-11-24 16:14:5372 Handle handle_;
73
[email protected]532e9bd2012-01-25 12:04:1774 private:
[email protected]0840cc72009-11-24 16:14:5375 DISALLOW_COPY_AND_ASSIGN(SyncSocket);
76};
77
[email protected]532e9bd2012-01-25 12:04:1778// Derives from SyncSocket and adds support for shutting down the socket from
[email protected]5d272092012-04-19 10:23:0379// another thread while a blocking Receive or Send is being done from the
80// thread that owns the socket.
[email protected]532e9bd2012-01-25 12:04:1781class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
82 public:
83 CancelableSyncSocket();
84 explicit CancelableSyncSocket(Handle handle);
85 virtual ~CancelableSyncSocket() {}
86
87 // Initializes a pair of cancelable sockets. See documentation for
88 // SyncSocket::CreatePair for more details.
89 static bool CreatePair(CancelableSyncSocket* socket_a,
90 CancelableSyncSocket* socket_b);
91
92 // A way to shut down a socket even if another thread is currently performing
93 // a blocking Receive or Send.
94 bool Shutdown();
95
96#if defined(OS_WIN)
97 // Since the Linux and Mac implementations actually use a socket, shutting
98 // them down from another thread is pretty simple - we can just call
99 // shutdown(). However, the Windows implementation relies on named pipes
100 // and there isn't a way to cancel a blocking synchronous Read that is
101 // supported on <Vista. So, for Windows only, we override these
102 // SyncSocket methods in order to support shutting down the 'socket'.
103 virtual bool Close() OVERRIDE;
[email protected]532e9bd2012-01-25 12:04:17104 virtual size_t Receive(void* buffer, size_t length) OVERRIDE;
105#endif
106
[email protected]5d272092012-04-19 10:23:03107 // Send() is overridden to catch cases where the remote end is not responding
108 // and we fill the local socket buffer. When the buffer is full, this
109 // implementation of Send() will not block indefinitely as
110 // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
111 // Note that the socket will not be closed in this case.
112 virtual size_t Send(const void* buffer, size_t length) OVERRIDE;
113
[email protected]532e9bd2012-01-25 12:04:17114 private:
115#if defined(OS_WIN)
116 WaitableEvent shutdown_event_;
117 WaitableEvent file_operation_;
118#endif
119 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
120};
121
[email protected]0840cc72009-11-24 16:14:53122} // namespace base
123
124#endif // BASE_SYNC_SOCKET_H_