blob: d5bfb723f6213dac5df2e1c2227d196eb0e82f92 [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_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]0840cc72009-11-24 16:14:538
9// A socket abstraction used for sending and receiving plain
[email protected]5d272092012-04-19 10:23:0310// data. Because the receiving is blocking, they can be used to perform
[email protected]0840cc72009-11-24 16:14:5311// rudimentary cross-process synchronization with low latency.
12
13#include "base/basictypes.h"
14#if defined(OS_WIN)
15#include <windows.h>
16#endif
17#include <sys/types.h>
18
[email protected]0bea7252011-08-05 15:34:0019#include "base/base_export.h"
[email protected]532e9bd2012-01-25 12:04:1720#include "base/compiler_specific.h"
21#include "base/synchronization/waitable_event.h"
[email protected]9493ee95c2011-03-28 23:48:4422
[email protected]0840cc72009-11-24 16:14:5323namespace base {
24
[email protected]0bea7252011-08-05 15:34:0025class BASE_EXPORT SyncSocket {
[email protected]0840cc72009-11-24 16:14:5326 public:
27#if defined(OS_WIN)
28 typedef HANDLE Handle;
29#else
30 typedef int Handle;
31#endif
[email protected]5895ee12011-12-22 19:33:2732 static const Handle kInvalidHandle;
[email protected]0840cc72009-11-24 16:14:5333
[email protected]532e9bd2012-01-25 12:04:1734 SyncSocket();
[email protected]0840cc72009-11-24 16:14:5335
[email protected]532e9bd2012-01-25 12:04:1736 // 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);
[email protected]0840cc72009-11-24 16:14:5344
45 // Closes the SyncSocket. Returns true on success, false on failure.
[email protected]532e9bd2012-01-25 12:04:1746 virtual bool Close();
[email protected]0840cc72009-11-24 16:14:5347
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.
[email protected]532e9bd2012-01-25 12:04:1754 virtual size_t Send(const void* buffer, size_t length);
[email protected]0840cc72009-11-24 16:14:5355
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.
[email protected]532e9bd2012-01-25 12:04:1760 virtual size_t Receive(void* buffer, size_t length);
[email protected]0840cc72009-11-24 16:14:5361
[email protected]d8b65912009-12-04 22:53:2262 // 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.
66 size_t Peek();
67
[email protected]0840cc72009-11-24 16:14:5368 // Extracts the contained handle. Used for transferring between
69 // processes.
70 Handle handle() const { return handle_; }
71
[email protected]532e9bd2012-01-25 12:04:1772 protected:
[email protected]0840cc72009-11-24 16:14:5373 Handle handle_;
74
[email protected]532e9bd2012-01-25 12:04:1775 private:
[email protected]0840cc72009-11-24 16:14:5376 DISALLOW_COPY_AND_ASSIGN(SyncSocket);
77};
78
[email protected]532e9bd2012-01-25 12:04:1779// Derives from SyncSocket and adds support for shutting down the socket from
[email protected]5d272092012-04-19 10:23:0380// another thread while a blocking Receive or Send is being done from the
81// thread that owns the socket.
[email protected]532e9bd2012-01-25 12:04:1782class BASE_EXPORT CancelableSyncSocket : public SyncSocket {
83 public:
84 CancelableSyncSocket();
85 explicit CancelableSyncSocket(Handle handle);
86 virtual ~CancelableSyncSocket() {}
87
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);
92
93 // A way to shut down a socket even if another thread is currently performing
94 // a blocking Receive or Send.
95 bool Shutdown();
96
97#if defined(OS_WIN)
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;
[email protected]532e9bd2012-01-25 12:04:17105 virtual size_t Receive(void* buffer, size_t length) OVERRIDE;
106#endif
107
[email protected]5d272092012-04-19 10:23:03108 // Send() is overridden to catch cases where the remote end is not responding
109 // and we fill the local socket buffer. When the buffer is full, this
110 // implementation of Send() will not block indefinitely as
111 // SyncSocket::Send will, but instead return 0, as no bytes could be sent.
112 // Note that the socket will not be closed in this case.
113 virtual size_t Send(const void* buffer, size_t length) OVERRIDE;
114
[email protected]532e9bd2012-01-25 12:04:17115 private:
116#if defined(OS_WIN)
117 WaitableEvent shutdown_event_;
118 WaitableEvent file_operation_;
119#endif
120 DISALLOW_COPY_AND_ASSIGN(CancelableSyncSocket);
121};
122
[email protected]0840cc72009-11-24 16:14:53123} // namespace base
124
125#endif // BASE_SYNC_SOCKET_H_