Bitcoin Core 31.99.0
P2P Digital Currency
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
http_bitcoin::HTTPServer Class Reference

#include <httpserver.h>

Collaboration diagram for http_bitcoin::HTTPServer:
[legend]

Classes

struct  IOReadiness
 Info about which socket has which event ready and a reverse map back to the HTTPRemoteClient that owns the socket. More...
 

Public Types

using Id = uint64_t
 Each connection is assigned an unique id of this type. More...
 

Public Member Functions

 HTTPServer (std::function< void(std::unique_ptr< HTTPRequest > &&)> func)
 
virtual ~HTTPServer ()
 
util::Expected< void, std::string > BindAndStartListening (const CService &to)
 Bind to a new address:port, start listening and add the listen socket to m_listen. More...
 
void StopListening ()
 Stop listening by closing all listening sockets. More...
 
size_t GetListeningSocketCount () const
 Get the number of sockets the server is bound to and listening on. More...
 
size_t GetConnectionsCount () const
 Get the number of HTTPRemoteClients we are connected to. More...
 
void StartSocketsThreads ()
 Start the necessary threads for sockets IO. More...
 
void JoinSocketsThreads ()
 Join (wait for) the threads started by StartSocketsThreads() to exit. More...
 
void InterruptNet ()
 Stop network activity. More...
 
void DisconnectAllClients ()
 Start disconnecting clients when possible in the I/O loop. More...
 
void SetRequestHandler (std::function< void(std::unique_ptr< HTTPRequest > &&)> func) EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex)
 Update the request handler method. More...
 
void StopAccepting ()
 Stop accepting new connections in the I/O loop. More...
 
void SetServerTimeout (std::chrono::seconds seconds)
 Set the idle client timeout (-rpcservertimeout) More...
 
void ClearConnectedClients ()
 Force-remove all remaining clients from m_connected without waiting for graceful disconnection. More...
 

Private Member Functions

std::unique_ptr< SockAcceptConnection (const Sock &listen_sock, CService &addr)
 Accept a connection. More...
 
Id GetNewId ()
 Generate an id for a newly created connection. More...
 
void NewSockAccepted (std::unique_ptr< Sock > &&sock, const CService &addr)
 After a new socket with a client has been created, configure its flags, make a new HTTPRemoteClient and Id and save its shared pointer. More...
 
void SocketHandlerConnected (const IOReadiness &io_readiness) const EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex)
 Do the read/write for connected sockets that are ready for IO. More...
 
void SocketHandlerListening (const Sock::EventsPerSock &events_per_sock)
 Accept incoming connections, one from each read-ready listening socket. More...
 
IOReadiness GenerateWaitSockets () const
 Generate a collection of sockets to check for IO readiness. More...
 
void ThreadSocketHandler () EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex)
 Check connected and listening sockets for IO readiness and process them accordingly. More...
 
void MaybeDispatchRequestsFromClient (const std::shared_ptr< HTTPRemoteClient > &client) const EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex)
 Try to read HTTPRequests from a client's receive buffer. More...
 
void DisconnectClients ()
 Close underlying socket connections for flagged clients by removing their shared pointer from m_connected. More...
 

Private Attributes

std::vector< std::shared_ptr< Sock > > m_listen
 List of listening sockets. More...
 
std::atomic< Idm_next_id {0}
 The id to assign to the next created connection. More...
 
std::vector< std::shared_ptr< HTTPRemoteClient > > m_connected
 List of HTTPRemoteClients with connected sockets. More...
 
std::atomic_bool m_stop_accepting {false}
 Flag used during shutdown to stop accepting new connections. More...
 
std::atomic_bool m_disconnect_all_clients {false}
 Flag used during shutdown. More...
 
std::atomic< size_t > m_connected_size {0}
 The number of connected sockets. More...
 
CThreadInterrupt m_interrupt_net
 This is signaled when network activity should cease. More...
 
std::thread m_thread_socket_handler
 Thread that sends to and receives from sockets and accepts connections. More...
 
std::chrono::seconds m_rpcservertimeout {DEFAULT_HTTP_SERVER_TIMEOUT}
 Idle timeout after which clients are disconnected. More...
 
Mutex m_request_dispatcher_mutex
 
std::function< void(std::unique_ptr< HTTPRequest > &&)> m_request_dispatcher GUARDED_BY (m_request_dispatcher_mutex)
 

Detailed Description

Definition at line 200 of file httpserver.h.

Member Typedef Documentation

◆ Id

using http_bitcoin::HTTPServer::Id = uint64_t

Each connection is assigned an unique id of this type.

Definition at line 206 of file httpserver.h.

Constructor & Destructor Documentation

◆ HTTPServer()

http_bitcoin::HTTPServer::HTTPServer ( std::function< void(std::unique_ptr< HTTPRequest > &&)>  func)
inlineexplicit

Definition at line 208 of file httpserver.h.

◆ ~HTTPServer()

virtual http_bitcoin::HTTPServer::~HTTPServer ( )
inlinevirtual

Definition at line 211 of file httpserver.h.

Member Function Documentation

◆ AcceptConnection()

std::unique_ptr< Sock > http_bitcoin::HTTPServer::AcceptConnection ( const Sock listen_sock,
CService addr 
)
private

Accept a connection.

Parameters
[in]listen_sockSocket on which to accept the connection.
[out]addrAddress of the peer that was accepted.
Returns
Newly created socket for the accepted connection.

Definition at line 767 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ BindAndStartListening()

util::Expected< void, std::string > http_bitcoin::HTTPServer::BindAndStartListening ( const CService to)

Bind to a new address:port, start listening and add the listen socket to m_listen.

Parameters
[in]toWhere to bind.
Returns
{} or the reason for failure.

Definition at line 672 of file httpserver.cpp.

Here is the call graph for this function:

◆ ClearConnectedClients()

void http_bitcoin::HTTPServer::ClearConnectedClients ( )

Force-remove all remaining clients from m_connected without waiting for graceful disconnection.

Must only be called after JoinSocketsThreads().

Definition at line 1089 of file httpserver.cpp.

◆ DisconnectAllClients()

void http_bitcoin::HTTPServer::DisconnectAllClients ( )
inline

Start disconnecting clients when possible in the I/O loop.

Definition at line 258 of file httpserver.h.

◆ DisconnectClients()

void http_bitcoin::HTTPServer::DisconnectClients ( )
private

Close underlying socket connections for flagged clients by removing their shared pointer from m_connected.

If an HTTPRemoteClient is busy in a worker thread, its connection will be closed once that job is done and the HTTPRequest is out of scope.

Definition at line 1034 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GenerateWaitSockets()

HTTPServer::IOReadiness http_bitcoin::HTTPServer::GenerateWaitSockets ( ) const
private

Generate a collection of sockets to check for IO readiness.

Returns
Sockets to check for readiness plus an aux map to find the corresponding HTTPRemoteClient given a socket.

Definition at line 924 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GetConnectionsCount()

size_t http_bitcoin::HTTPServer::GetConnectionsCount ( ) const
inline

Get the number of HTTPRemoteClients we are connected to.

Definition at line 238 of file httpserver.h.

◆ GetListeningSocketCount()

size_t http_bitcoin::HTTPServer::GetListeningSocketCount ( ) const
inline

Get the number of sockets the server is bound to and listening on.

Definition at line 233 of file httpserver.h.

◆ GetNewId()

HTTPServer::Id http_bitcoin::HTTPServer::GetNewId ( )
private

Generate an id for a newly created connection.

Definition at line 799 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GUARDED_BY()

std::function< void(std::unique_ptr< HTTPRequest > &&)> m_request_dispatcher http_bitcoin::HTTPServer::GUARDED_BY ( m_request_dispatcher_mutex  )
private

◆ InterruptNet()

void http_bitcoin::HTTPServer::InterruptNet ( )
inline

Stop network activity.

Definition at line 253 of file httpserver.h.

◆ JoinSocketsThreads()

void http_bitcoin::HTTPServer::JoinSocketsThreads ( )

Join (wait for) the threads started by StartSocketsThreads() to exit.

Definition at line 760 of file httpserver.cpp.

◆ MaybeDispatchRequestsFromClient()

void http_bitcoin::HTTPServer::MaybeDispatchRequestsFromClient ( const std::shared_ptr< HTTPRemoteClient > &  client) const
private

Try to read HTTPRequests from a client's receive buffer.

Complete requests are dispatched, incomplete requests are left in the buffer to wait for more data. Some read errors will mark this client for disconnection.

Parameters
[in]clientThe HTTPRemoteClient to read requests from

Definition at line 973 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NewSockAccepted()

void http_bitcoin::HTTPServer::NewSockAccepted ( std::unique_ptr< Sock > &&  sock,
const CService addr 
)
private

After a new socket with a client has been created, configure its flags, make a new HTTPRemoteClient and Id and save its shared pointer.

Parameters
[in]sockThe newly created socket.
[in]addrAddress of the new peer.

Definition at line 804 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetRequestHandler()

void http_bitcoin::HTTPServer::SetRequestHandler ( std::function< void(std::unique_ptr< HTTPRequest > &&)>  func)
inline

Update the request handler method.

Used for shutdown to reject new requests.

Definition at line 264 of file httpserver.h.

◆ SetServerTimeout()

void http_bitcoin::HTTPServer::SetServerTimeout ( std::chrono::seconds  seconds)
inline

Set the idle client timeout (-rpcservertimeout)

Definition at line 282 of file httpserver.h.

◆ SocketHandlerConnected()

void http_bitcoin::HTTPServer::SocketHandlerConnected ( const IOReadiness io_readiness) const
private

Do the read/write for connected sockets that are ready for IO.

Parameters
[in]io_readinessWhich sockets are ready and their corresponding HTTPRemoteClients.

Definition at line 831 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SocketHandlerListening()

void http_bitcoin::HTTPServer::SocketHandlerListening ( const Sock::EventsPerSock events_per_sock)
private

Accept incoming connections, one from each read-ready listening socket.

Parameters
[in]events_per_sockSockets that are ready for IO.

Definition at line 904 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ StartSocketsThreads()

void http_bitcoin::HTTPServer::StartSocketsThreads ( )

Start the necessary threads for sockets IO.

Definition at line 753 of file httpserver.cpp.

Here is the call graph for this function:

◆ StopAccepting()

void http_bitcoin::HTTPServer::StopAccepting ( )
inline

Stop accepting new connections in the I/O loop.

Must be called first in StopHTTPServer() before DisconnectAllClients(). A connection accepted after the "wait for 0 connections" loop exits would remain in m_connected when the destructor is called.

Definition at line 277 of file httpserver.h.

◆ StopListening()

void http_bitcoin::HTTPServer::StopListening ( )

Stop listening by closing all listening sockets.

Definition at line 748 of file httpserver.cpp.

◆ ThreadSocketHandler()

void http_bitcoin::HTTPServer::ThreadSocketHandler ( )
private

Check connected and listening sockets for IO readiness and process them accordingly.

This is the main I/O loop of the server.

Definition at line 947 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_connected

std::vector<std::shared_ptr<HTTPRemoteClient> > http_bitcoin::HTTPServer::m_connected
private

List of HTTPRemoteClients with connected sockets.

Connections will only be added and removed in the I/O thread, but shared pointers may be passed to worker threads to handle requests and send replies.

Definition at line 307 of file httpserver.h.

◆ m_connected_size

std::atomic<size_t> http_bitcoin::HTTPServer::m_connected_size {0}
private

The number of connected sockets.

Updated from the I/O thread but safely readable from the main thread without locks.

Definition at line 327 of file httpserver.h.

◆ m_disconnect_all_clients

std::atomic_bool http_bitcoin::HTTPServer::m_disconnect_all_clients {false}
private

Flag used during shutdown.

Overrides HTTPRemoteClient flags m_keep_alive and m_connection_busy. Set by main thread and read by the I/O thread.

Definition at line 320 of file httpserver.h.

◆ m_interrupt_net

CThreadInterrupt http_bitcoin::HTTPServer::m_interrupt_net
private

This is signaled when network activity should cease.

Definition at line 356 of file httpserver.h.

◆ m_listen

std::vector<std::shared_ptr<Sock> > http_bitcoin::HTTPServer::m_listen
private

List of listening sockets.

Definition at line 294 of file httpserver.h.

◆ m_next_id

std::atomic<Id> http_bitcoin::HTTPServer::m_next_id {0}
private

The id to assign to the next created connection.

Definition at line 299 of file httpserver.h.

◆ m_request_dispatcher_mutex

Mutex http_bitcoin::HTTPServer::m_request_dispatcher_mutex
mutableprivate

Definition at line 370 of file httpserver.h.

◆ m_rpcservertimeout

std::chrono::seconds http_bitcoin::HTTPServer::m_rpcservertimeout {DEFAULT_HTTP_SERVER_TIMEOUT}
private

Idle timeout after which clients are disconnected.

Definition at line 377 of file httpserver.h.

◆ m_stop_accepting

std::atomic_bool http_bitcoin::HTTPServer::m_stop_accepting {false}
private

Flag used during shutdown to stop accepting new connections.

Set by main thread and read by the I/O thread.

Definition at line 313 of file httpserver.h.

◆ m_thread_socket_handler

std::thread http_bitcoin::HTTPServer::m_thread_socket_handler
private

Thread that sends to and receives from sockets and accepts connections.

Executes the I/O loop of the server.

Definition at line 362 of file httpserver.h.


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