LLVM 20.0.0git
Public Member Functions | Static Public Member Functions | List of all members
llvm::ListeningSocket Class Reference

Manages a passive (i.e., listening) UNIX domain socket. More...

#include "llvm/Support/raw_socket_stream.h"

Public Member Functions

 ~ListeningSocket ()
 
 ListeningSocket (ListeningSocket &&LS)
 
 ListeningSocket (const ListeningSocket &LS)=delete
 
ListeningSocketoperator= (const ListeningSocket &)=delete
 
void shutdown ()
 Closes the FD, unlinks the socket file, and writes to PipeFD.
 
Expected< std::unique_ptr< raw_socket_stream > > accept (const std::chrono::milliseconds &Timeout=std::chrono::milliseconds(-1))
 Accepts an incoming connection on the listening socket.
 

Static Public Member Functions

static Expected< ListeningSocketcreateUnix (StringRef SocketPath, int MaxBacklog=llvm::hardware_concurrency().compute_thread_count())
 Creates a listening socket bound to the specified file system path.
 

Detailed Description

Manages a passive (i.e., listening) UNIX domain socket.

The ListeningSocket class encapsulates a UNIX domain socket that can listen and accept incoming connections. ListeningSocket is portable and supports Windows builds begining with Insider Build 17063. ListeningSocket is designed for server-side operations, working alongside raw_socket_streams that function as client connections.

Usage example:

std::string Path = "/path/to/socket"
if (S) {
if (connection) {
// Use the accepted raw_socket_stream for communication.
}
}
Tagged union holding either a T or a Error.
Definition: Error.h:481
static Expected< ListeningSocket > createUnix(StringRef SocketPath, int MaxBacklog=llvm::hardware_concurrency().compute_thread_count())
Creates a listening socket bound to the specified file system path.

Definition at line 59 of file raw_socket_stream.h.

Constructor & Destructor Documentation

◆ ~ListeningSocket()

ListeningSocket::~ListeningSocket ( )

Definition at line 297 of file raw_socket_stream.cpp.

References shutdown().

◆ ListeningSocket() [1/2]

ListeningSocket::ListeningSocket ( ListeningSocket &&  LS)

Definition at line 101 of file raw_socket_stream.cpp.

◆ ListeningSocket() [2/2]

llvm::ListeningSocket::ListeningSocket ( const ListeningSocket LS)
delete

Member Function Documentation

◆ accept()

Expected< std::unique_ptr< raw_socket_stream > > ListeningSocket::accept ( const std::chrono::milliseconds &  Timeout = std::chrono::milliseconds(-1))

Accepts an incoming connection on the listening socket.

This method can optionally either block until a connection is available or timeout after a specified amount of time has passed. By default the method will block until the socket has recieved a connection. If the accept timesout this method will return std::errc:timed_out

Parameters
TimeoutAn optional timeout duration in milliseconds. Setting Timeout to a negative number causes accept to block indefinitely

Definition at line 255 of file raw_socket_stream.cpp.

References accept(), getLastSocketErrorCode(), and manageTimeout().

Referenced by accept().

◆ createUnix()

Expected< ListeningSocket > ListeningSocket::createUnix ( StringRef  SocketPath,
int  MaxBacklog = llvm::hardware_concurrency().compute_thread_count() 
)
static

Creates a listening socket bound to the specified file system path.

Handles the socket creation, binding, and immediately starts listening for incoming connections.

Parameters
SocketPathThe file system path where the socket will be created
MaxBacklogThe max number of connections in a socket's backlog

Definition at line 111 of file raw_socket_stream.cpp.

References _, Addr, llvm::consumeError(), llvm::sys::fs::exists(), getLastSocketErrorCode(), getSocketFD(), setSocketAddr(), and llvm::Expected< T >::takeError().

◆ operator=()

ListeningSocket & llvm::ListeningSocket::operator= ( const ListeningSocket )
delete

◆ shutdown()

void ListeningSocket::shutdown ( )

Closes the FD, unlinks the socket file, and writes to PipeFD.

After the construction of the ListeningSocket, shutdown is signal safe if it is called during the lifetime of the object. shutdown can be called concurrently with ListeningSocket::accept as writing to PipeFD will cause a blocking call to ::poll to return.

Once shutdown is called there is no way to reinitialize ListeningSocket.

Definition at line 275 of file raw_socket_stream.cpp.

References llvm::write().

Referenced by ~ListeningSocket().


The documentation for this class was generated from the following files: