![]() |
Bitcoin Core 31.99.0
P2P Digital Currency
|
#include <httpserver.h>
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< Sock > | AcceptConnection (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< Id > | m_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) |
Definition at line 200 of file httpserver.h.
| 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.
|
inlineexplicit |
Definition at line 208 of file httpserver.h.
|
inlinevirtual |
Definition at line 211 of file httpserver.h.
|
private |
Accept a connection.
| [in] | listen_sock | Socket on which to accept the connection. |
| [out] | addr | Address of the peer that was accepted. |
Definition at line 767 of file httpserver.cpp.
| 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.
| [in] | to | Where to bind. |
Definition at line 672 of file httpserver.cpp.
| 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.
|
inline |
Start disconnecting clients when possible in the I/O loop.
Definition at line 258 of file httpserver.h.
|
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.
|
private |
Generate a collection of sockets to check for IO readiness.
Definition at line 924 of file httpserver.cpp.
|
inline |
Get the number of HTTPRemoteClients we are connected to.
Definition at line 238 of file httpserver.h.
|
inline |
Get the number of sockets the server is bound to and listening on.
Definition at line 233 of file httpserver.h.
|
private |
Generate an id for a newly created connection.
Definition at line 799 of file httpserver.cpp.
|
private |
|
inline |
Stop network activity.
Definition at line 253 of file httpserver.h.
| void http_bitcoin::HTTPServer::JoinSocketsThreads | ( | ) |
Join (wait for) the threads started by StartSocketsThreads() to exit.
Definition at line 760 of file httpserver.cpp.
|
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.
| [in] | client | The HTTPRemoteClient to read requests from |
Definition at line 973 of file httpserver.cpp.
|
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.
| [in] | sock | The newly created socket. |
| [in] | addr | Address of the new peer. |
Definition at line 804 of file httpserver.cpp.
|
inline |
Update the request handler method.
Used for shutdown to reject new requests.
Definition at line 264 of file httpserver.h.
|
inline |
Set the idle client timeout (-rpcservertimeout)
Definition at line 282 of file httpserver.h.
|
private |
Do the read/write for connected sockets that are ready for IO.
| [in] | io_readiness | Which sockets are ready and their corresponding HTTPRemoteClients. |
Definition at line 831 of file httpserver.cpp.
|
private |
Accept incoming connections, one from each read-ready listening socket.
| [in] | events_per_sock | Sockets that are ready for IO. |
Definition at line 904 of file httpserver.cpp.
| void http_bitcoin::HTTPServer::StartSocketsThreads | ( | ) |
Start the necessary threads for sockets IO.
Definition at line 753 of file httpserver.cpp.
|
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.
| void http_bitcoin::HTTPServer::StopListening | ( | ) |
Stop listening by closing all listening sockets.
Definition at line 748 of file httpserver.cpp.
|
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.
|
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.
|
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.
|
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.
|
private |
This is signaled when network activity should cease.
Definition at line 356 of file httpserver.h.
|
private |
List of listening sockets.
Definition at line 294 of file httpserver.h.
|
private |
The id to assign to the next created connection.
Definition at line 299 of file httpserver.h.
|
mutableprivate |
Definition at line 370 of file httpserver.h.
|
private |
Idle timeout after which clients are disconnected.
Definition at line 377 of file httpserver.h.
|
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.
|
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.