class UnixSocket
Defined at line 263 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
A non-blocking UNIX domain socket. Allows also to transfer file descriptors.
None of the methods in this class are blocking.
The main design goal is making strong guarantees on the EventListener
callbacks, in order to avoid ending in some undefined state.
In case of any error it will aggressively just shut down the socket and
notify the failure with OnConnect(false) or OnDisconnect() depending on the
state of the socket (see below).
EventListener callbacks stop happening as soon as the instance is destroyed.
Lifecycle of a client socket:
Connect()
|
+------------------+------------------+
| (success) | (failure or Shutdown())
V V
OnConnect(true) OnConnect(false)
|
V
OnDataAvailable()
|
V
OnDisconnect() (failure or shutdown)
Lifecycle of a server socket:
Listen() --> returns false in case of errors.
|
V
OnNewIncomingConnection(new_socket)
(|new_socket| inherits the same EventListener)
|
V
OnDataAvailable()
| (failure or Shutdown())
V
OnDisconnect()
Public Methods
std::string GetSockAddr ()
Defined at line 368 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
std::unique_ptr<UnixSocket> Listen (const std::string & socket_name, EventListener * , TaskRunner * , SockFamily , SockType )
Creates a socket and starts listening. If SockFamily::kUnix and
|socket_name| starts with a '
@
', an abstract UNIX dmoain socket will be
created instead of a filesystem-linked UNIX socket (Linux/Android only).
If SockFamily::kInet, |socket_name| is host:port (e.g., "1.2.3.4:8000").
If SockFamily::kInet6, |socket_name| is [host]:port (e.g., "[::1]:8000").
Returns nullptr if the socket creation or bind fails. If listening fails,
(e.g. if another socket with the same name is already listening) the
returned socket will have is_listening() == false.
std::unique_ptr<UnixSocket> Listen (ScopedSocketHandle , EventListener * , TaskRunner * , SockFamily , SockType )
Attaches to a pre-existing socket. The socket must have been created in
SOCK_STREAM mode and the caller must have called bind() on it.
std::unique_ptr<UnixSocket> Connect (const std::string & socket_name, EventListener * , TaskRunner * , SockFamily , SockType , SockPeerCredMode )
Creates a Unix domain socket and connects to the listening endpoint.
Returns always an instance. EventListener::OnConnect(bool success) will
be called always, whether the connection succeeded or not.
std::unique_ptr<UnixSocket> AdoptConnected (ScopedSocketHandle , EventListener * , TaskRunner * , SockFamily , SockType , SockPeerCredMode )
Constructs a UnixSocket using the given connected socket.
void UnixSocket (const UnixSocket & )
Defined at line 345 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
UnixSocket & operator= (const UnixSocket & )
Defined at line 346 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
void UnixSocket (UnixSocket && )
Cannot be easily moved because of tasks from the FileDescriptorWatch.
Defined at line 348 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
UnixSocket & operator= (UnixSocket && )
Defined at line 349 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
void SetTxTimeout (uint32_t timeout_ms)
Defined at line 361 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
void SetRxTimeout (uint32_t timeout_ms)
Defined at line 364 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
void ~UnixSocket ()
This class gives the hard guarantee that no callback is called on the
passed EventListener immediately after the object has been destroyed.
Any queued callback will be silently dropped.
void Shutdown (bool notify)
Shuts down the current connection, if any. If the socket was Listen()-ing,
stops listening. The socket goes back to kNotInitialized state, so it can
be reused with Listen() or Connect().
bool Send (const void * msg, size_t len, const int * send_fds, size_t num_fds)
Returns true is the message was queued, false if there was no space in the
output buffer, in which case the client should retry or give up.
If any other error happens the socket will be shutdown and
EventListener::OnDisconnect() will be called.
If the socket is not connected, Send() will just return false.
Does not append a null string terminator to msg in any case.
size_t Receive (void * msg, size_t len, ScopedFile * , size_t max_files)
Returns the number of bytes (
<
= |len|) written in |msg| or 0 if there
is no data in the buffer to read or an error occurs (in which case a
EventListener::OnDisconnect() will follow).
If the ScopedFile pointer is not null and a FD is received, it moves the
received FD into that. If a FD is received but the ScopedFile pointer is
null, the FD will be automatically closed.
bool Send (const void * msg, size_t len, int send_fd)
Defined at line 378 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
bool SendStr (const std::string & msg)
Defined at line 384 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
size_t Receive (void * msg, size_t len)
Defined at line 396 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
std::string ReceiveString (size_t max_length)
Only for tests. This is slower than Receive() as it requires a heap
allocation and a copy for the std::string. Guarantees that the returned
string is null terminated even if the underlying message sent by the peer
is not.
UnixSocketRaw ReleaseSocket ()
This makes the UnixSocket unusable.
bool is_connected ()
Defined at line 406 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
bool is_listening ()
Defined at line 407 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
SocketHandle fd ()
Defined at line 408 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
SockFamily family ()
Defined at line 409 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h
Enumerations
enum State
| Name | Value |
|---|---|
| kDisconnected | 0 |
| kConnecting | 1 |
| kConnected | 2 |
| kListening | 3 |
Defined at line 296 of file ../../third_party/perfetto/include/perfetto/ext/base/unix_socket.h