Bitcoin Core 31.99.0
P2P Digital Currency
Public Member Functions | Public Attributes | List of all members
http_bitcoin::HTTPRemoteClient Class Reference

#include <httpserver.h>

Collaboration diagram for http_bitcoin::HTTPRemoteClient:
[legend]

Public Member Functions

std::shared_ptr< Sock > m_sock GUARDED_BY (m_sock_mutex)
 Underlying socket. More...
 
 HTTPRemoteClient (HTTPServer::Id id, const CService &addr, std::unique_ptr< Sock > socket)
 
 HTTPRemoteClient (const HTTPRemoteClient &)=delete
 
HTTPRemoteClientoperator= (const HTTPRemoteClient &)=delete
 
bool ReadRequest (HTTPRequest &req)
 Try to read an HTTP request from the receive buffer. More...
 
bool MaybeSendBytesFromBuffer () EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex
 Push data (if there is any) from client's m_send_buffer to the connected socket. More...
 

Public Attributes

const HTTPServer::Id m_id
 ID provided by HTTPServer upon connection and instantiation. More...
 
const CService m_addr
 Remote address of connected client. More...
 
const std::string m_origin
 IP:port of connected client, cached for logging purposes. More...
 
std::vector< std::byte > m_recv_buffer {}
 In lieu of an intermediate transport class like p2p uses, we copy data from the socket buffer to the client object and attempt to read HTTP requests from here. More...
 
std::deque< std::unique_ptr< HTTPRequest > > m_req_queue
 Requests from a client must be processed in the order in which they were received, blocking on a per-client basis. More...
 
std::atomic_bool m_req_busy {false}
 Set to true by the I/O thread when a request is popped off and passed to a worker thread, reset to false by the worker thread. More...
 
std::atomic_bool m_send_ready {false}
 Set true by worker threads after writing a response to m_send_buffer. More...
 
Mutex m_sock_mutex
 Mutex that serializes the Send() and Recv() calls on m_sock. More...
 
std::atomic_bool m_connection_busy {true}
 Initialized to true while server waits for first request from client. More...
 
std::atomic_bool m_keep_alive {false}
 Client has requested to keep the connection open after all requests have been responded to. More...
 
std::atomic_bool m_disconnect {false}
 Flag this client for disconnection on next loop. More...
 
std::atomic< SteadySecondsm_idle_since
 Timestamp of last send or receive activity, used for -rpcservertimeout. More...
 
bool !m_sock_mutex
 
Mutex m_send_mutex
 Response data destined for this client. More...
 
std::vector< std::byte > m_send_buffer GUARDED_BY (m_send_mutex)
 

Detailed Description

Definition at line 447 of file httpserver.h.

Constructor & Destructor Documentation

◆ HTTPRemoteClient() [1/2]

http_bitcoin::HTTPRemoteClient::HTTPRemoteClient ( HTTPServer::Id  id,
const CService addr,
std::unique_ptr< Sock socket 
)
inlineexplicit

Definition at line 533 of file httpserver.h.

◆ HTTPRemoteClient() [2/2]

http_bitcoin::HTTPRemoteClient::HTTPRemoteClient ( const HTTPRemoteClient )
delete

Member Function Documentation

◆ GUARDED_BY() [1/2]

std::vector< std::byte > m_send_buffer http_bitcoin::HTTPRemoteClient::GUARDED_BY ( m_send_mutex  )

◆ GUARDED_BY() [2/2]

std::shared_ptr< Sock > m_sock http_bitcoin::HTTPRemoteClient::GUARDED_BY ( m_sock_mutex  )

Underlying socket.

shared_ptr (instead of unique_ptr) is used to avoid premature close of the underlying file descriptor by one thread while another thread is poll(2)-ing it for activity.

See also
https://github.com/bitcoin/bitcoin/issues/21744 for details.

◆ MaybeSendBytesFromBuffer()

bool http_bitcoin::HTTPRemoteClient::MaybeSendBytesFromBuffer ( )

Push data (if there is any) from client's m_send_buffer to the connected socket.

Returns
false if we are done with this client and HTTPServer can skip the next read operation from it.

Definition at line 1116 of file httpserver.cpp.

Here is the call graph for this function:

◆ operator=()

HTTPRemoteClient & http_bitcoin::HTTPRemoteClient::operator= ( const HTTPRemoteClient )
delete

◆ ReadRequest()

bool http_bitcoin::HTTPRemoteClient::ReadRequest ( HTTPRequest req)

Try to read an HTTP request from the receive buffer.

Parameters
[in]reqA HTTPRequest to read into
Returns
true upon reading a complete request, otherwise false (may throw).

Definition at line 1098 of file httpserver.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ !m_sock_mutex

bool http_bitcoin::HTTPRemoteClient::!m_sock_mutex

Definition at line 551 of file httpserver.h.

◆ m_addr

const CService http_bitcoin::HTTPRemoteClient::m_addr

Remote address of connected client.

Definition at line 454 of file httpserver.h.

◆ m_connection_busy

std::atomic_bool http_bitcoin::HTTPRemoteClient::m_connection_busy {true}

Initialized to true while server waits for first request from client.

Set to false after data is written to m_send_buffer and then that buffer is flushed to client. Reset to true when we receive new request data from client. Checked during DisconnectClients() and set by read/write operations called in either the HTTPServer I/O loop or by a worker thread during an "optimistic send". m_connection_busy=true can be overridden by m_disconnect=true (we disconnect).

Definition at line 514 of file httpserver.h.

◆ m_disconnect

std::atomic_bool http_bitcoin::HTTPRemoteClient::m_disconnect {false}

Flag this client for disconnection on next loop.

Either we have encountered a permanent error, or both sides of the socket are done with the connection, e.g. our reply to a "Connection: close" request has been sent. Might be set in a worker thread or in the I/O thread. When set to true we disconnect, possibly overriding all other disconnect flags.

Definition at line 526 of file httpserver.h.

◆ m_id

const HTTPServer::Id http_bitcoin::HTTPRemoteClient::m_id

ID provided by HTTPServer upon connection and instantiation.

Definition at line 451 of file httpserver.h.

◆ m_idle_since

std::atomic<SteadySeconds> http_bitcoin::HTTPRemoteClient::m_idle_since

Timestamp of last send or receive activity, used for -rpcservertimeout.

Due to optimistic sends it may be updated in either a worker thread or in the I/O thread. It is checked in the I/O thread to disconnect idle clients.

Definition at line 531 of file httpserver.h.

◆ m_keep_alive

std::atomic_bool http_bitcoin::HTTPRemoteClient::m_keep_alive {false}

Client has requested to keep the connection open after all requests have been responded to.

Set by (potentially multiple) worker threads and checked in the HTTPServer I/O loop. m_keep_alive=true can be overridden by HTTPServer.m_disconnect_all_clients (we disconnect).

Definition at line 519 of file httpserver.h.

◆ m_origin

const std::string http_bitcoin::HTTPRemoteClient::m_origin

IP:port of connected client, cached for logging purposes.

Definition at line 457 of file httpserver.h.

◆ m_recv_buffer

std::vector<std::byte> http_bitcoin::HTTPRemoteClient::m_recv_buffer {}

In lieu of an intermediate transport class like p2p uses, we copy data from the socket buffer to the client object and attempt to read HTTP requests from here.

Definition at line 464 of file httpserver.h.

◆ m_req_busy

std::atomic_bool http_bitcoin::HTTPRemoteClient::m_req_busy {false}

Set to true by the I/O thread when a request is popped off and passed to a worker thread, reset to false by the worker thread.

Definition at line 474 of file httpserver.h.

◆ m_req_queue

std::deque<std::unique_ptr<HTTPRequest> > http_bitcoin::HTTPRemoteClient::m_req_queue

Requests from a client must be processed in the order in which they were received, blocking on a per-client basis.

We won't process the next request in the queue if we are currently busy handling a previous request.

Definition at line 470 of file httpserver.h.

◆ m_send_mutex

Mutex http_bitcoin::HTTPRemoteClient::m_send_mutex

Response data destined for this client.

Written to by http worker threads, read and erased by HTTPServer I/O thread

Definition at line 481 of file httpserver.h.

◆ m_send_ready

std::atomic_bool http_bitcoin::HTTPRemoteClient::m_send_ready {false}

Set true by worker threads after writing a response to m_send_buffer.

Set false by the HTTPServer I/O thread after flushing m_send_buffer. Checked in the HTTPServer I/O loop to avoid locking m_send_mutex if there's nothing to send.

Definition at line 490 of file httpserver.h.

◆ m_sock_mutex

Mutex http_bitcoin::HTTPRemoteClient::m_sock_mutex

Mutex that serializes the Send() and Recv() calls on m_sock.

Reading from the client occurs in the I/O thread but writing back to a client may occur in a worker thread.

Definition at line 497 of file httpserver.h.


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