1 // Copyright (c) 2011 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"
23 class BASE_EXPORT SyncSocket
{
26 typedef HANDLE Handle
;
31 // Creates a SyncSocket from a Handle. Used in transport.
32 explicit SyncSocket(Handle handle
) : handle_(handle
) { }
33 ~SyncSocket() { Close(); }
35 // Creates an unnamed pair of connected sockets.
36 // pair is a pointer to an array of two SyncSockets in which connected socket
37 // descriptors are returned. Returns true on success, false on failure.
38 static bool CreatePair(SyncSocket
* pair
[2]);
40 // Closes the SyncSocket. Returns true on success, false on failure.
43 // Sends the message to the remote peer of the SyncSocket.
44 // Note it is not safe to send messages from the same socket handle by
45 // multiple threads simultaneously.
46 // buffer is a pointer to the data to send.
47 // length is the length of the data to send (must be non-zero).
48 // Returns the number of bytes sent, or 0 upon failure.
49 size_t Send(const void* buffer
, size_t length
);
51 // Receives a message from an SyncSocket.
52 // buffer is a pointer to the buffer to receive data.
53 // length is the number of bytes of data to receive (must be non-zero).
54 // Returns the number of bytes received, or 0 upon failure.
55 size_t Receive(void* buffer
, size_t length
);
57 // Returns the number of bytes available. If non-zero, Receive() will not
58 // not block when called. NOTE: Some implementations cannot reliably
59 // determine the number of bytes available so avoid using the returned
60 // size as a promise and simply test against zero.
63 // Extracts the contained handle. Used for transferring between
65 Handle
handle() const { return handle_
; }
70 DISALLOW_COPY_AND_ASSIGN(SyncSocket
);
75 #endif // BASE_SYNC_SOCKET_H_