dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
23 #ifndef network_tcplistener_hpp
24 #define network_tcplistener_hpp
27 #include <condition_variable>
33 #include <sys/epoll.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
36 #include <yaml-cpp/yaml.h>
50 class TCPListenerTimer;
159 Params(
const YAML::Node &node );
376 void pushWork(
const SocketWork &work );
596 return static_cast<int>(
self) &
static_cast<int>(other);
625 os <<
" (" <<
static_cast<int>(state) <<
")";
const common::Bytes & getReadBuffer() const
Get a reference to the read buffer.
BaseSocket * listen_socket_
The listening BaseSocket.
Stats last_stats_
The last Stats.
Stats prev_stats_
The previous Stats.
Waits for and locks the Mutex on construction, unlocks the Mutex when this Mutexer is destructed.
threads::Mutex now_mutex_
Synchronize access to now_.
TCPConnectionData * data
Data context.
void addServers()
Add a server if work_q_sz_ exceeds servers.size() and servers_.size() < params_.maxservers.
size_t event_maxconnections_reached_
The number of times a connection was closed after accept because params_.maxconnectctions was reached...
void cleanStoppedServers()
Cleanup stopped TCPServers.
const dodo::common::Puts & operator<<(const dodo::common::Puts &os, TCPListener::SockState state)
Nice-write to a Puts.
TCPListener::SockState & operator^=(TCPListener::SockState &self, TCPListener::SockState other)
Bitwise ^= on SocketWork.
SockState
BaseSocket lifecycle states.
socklen_t sendbufsz
The send buffer size, set on the listening socket, inherited by accepted sockets.
@ None
Undefined / initial.
friend TCPListener::SockState & operator|=(TCPListener::SockState &, TCPListener::SockState)
Bitwise |= on SocketWork.
void clearBuffer()
Clears the read_buffer.
size_t cycle_max_throttles
Maximum number of throttles per epoll_wait cycle.
ssize_t received
The number of bytes received.
SockState state
State of the socket.
@ New
New connection, TCPServer::handShake() will be called.
threads::Mutex clientmutex_
Protects both client_ and workload_.
ssize_t sent
The number of bytes sent.
An array of Octets with size elements.
@ Shut
BaseSocket is hung up or in error, TCPServer::shutDown() will be called.
Generic network Address, supporting ipv4 and ipv6 transparently.
int backlog_
The backlog (incoming connection queue before accept calls clearing it) used by listen.
Updates the attribute now_ in the TCPListener at a regular interval to avoid excessive number of call...
TCPServer * init_server_
The initial TCPServer, which will live as long as the TCPListener runs, and is sued to create new TCP...
size_t minservers
The minimum number of TCPServers.
time_t stat_trc_interval_s
The epol__wait timeout in ms.
size_t maxqdepth
Maximum size of connection and request work queues.
friend bool operator&(const TCPListener::SockState &, const TCPListener::SockState &)
Bitwise & on SocketWork.
void closeSocket(BaseSocket *socket)
Tell the listener to close the socket.
void stop()
Request a stop on the TCPListener.
TCPConnectionData()
Construct.
uint32_t read_event_mask_
The event mask for read events.
void start()
Start the thread.
struct timeval stat_time_
Time of last statistics.
size_t maxconnections
The maximum number of connections (in effect, sockets) allowed.
BaseSocket * socket
Pointer to the socket.
struct timeval warn_queue_time_
Time of last queueing warning.
virtual void run()
Entrypoint, override of Thread::run()
virtual ~TCPConnectionData()
Destruct.
socklen_t recvbufsz
The receive buffer size, set on the listening socket, inherited by accepted sockets.
std::condition_variable cv_signal_
Condition variable used to wakeup TCPServer threads with mutex mt_signal_.
void pollAdd(const BaseSocket *s, uint32_t events)
Add an epoll event for the BaseSocket.
void releaseWork(const SocketWork &work)
Called by a TCPServer to signal that the work has been handled and event detection on it can resume.
Address listen_address_
The listening address.
A Mutex to synchronize access to resources between threads.
A C++ platform interface to lean Linux services tailored for containerized deployment.
std::deque< SocketWork * > workload_
Queue of sockets with work for TCPServer instances.
int receive_timeout_seconds
Receive timeout in seconds.
std::list< TCPServer * > servers_
List of TCPServers.
Params()
Construct with default parameters.
std::mutex mt_signal_
Mutex used to wakeup TCPServer threads with condition variable cv_signal_.
bool waitForActivity(TCPServer *server)
Called by the TCPServer, enters wait state until woken up by either a timeout or a notify by TCPListe...
void pollDel(const BaseSocket *s)
Delete the epoll event for the BaseSocket.
size_t throttle_sleep_us
The time to sleep when the queue gets too big (letting workers clear from the queue without accepting...
SocketWork * popWork()
Pop work.
@ Read
Data is ready to be read, TCPServer::readSocket() will be called.
ssize_t throttles
The number of throttles.
void throttle()
Throttle the listener when work_q_sz_ exceeds params_.maxqdepth.
Parameters affecting TCPListener behavior.
Used in conjunction with TCPListener to implement high speed, multithreaded TCP services.
const friend dodo::common::Puts & operator<<(const dodo::common::Puts &, TCPListener::SockState)
Nice-write to a Puts.
struct timeval prev_stat_time_
Time of previous statistics.
int listener_sleep_ms
The TCPListener epoll_wait timeout in ms.
friend TCPListener::SockState & operator^=(TCPListener::SockState &, TCPListener::SockState)
Bitwise ^= on SocketWork.
common::SystemError readBuffer(BaseSocket *socket, ssize_t &received)
Reads and appends data to read_buffer.
TCPListener(const Address &address, const Params ¶ms)
Constructor.
void pollMod(const BaseSocket *s, uint32_t events)
Modify an epoll event for the BaseSocket.
BaseSocket socket and state pair.
std::map< int, SocketWork > clients_
Map of file descriptors to SocketWork for all connected clients.
void addReceivedSentBytes(ssize_t r, ssize_t s)
Add received bytes.
void logStats()
Log TCPListener statistics to Logger::getLogger().
std::atomic< unsigned long long > work_q_sz_
The number of queued work items.
ssize_t requests
The number of requests.
Helper class to write strings in stream format, eg.
Class to track a connection.
Linux system error primitive to provide a consistent interface to Linux error codes.
void construct(const Address &address, const Params ¶ms)
Common constructor code.
int send_timeout_seconds
Send timeout in seconds.
uint32_t hangup_event_mask_
The event mask for hangup events.
bool tcp_keep_alive
Toggle TCP keep-alive.
ssize_t connections
The number of connections.
struct timeval now_
Frequently updated by a TCPListenerTimer with enough precision for its use.
The TCPListener listens, accepts connections and generates socket events to produce TCP work to a poo...
size_t maxservers
The maximum number of TCPServers.
void pushWork(const SocketWork &work)
Push work.
Params params_
TCPListener parameters.
bool operator&(const TCPListener::SockState &self, const TCPListener::SockState &other)
Bitwise & on SocketWork.
int epoll_fd_
The epoll interface file descriptor.
common::Bytes read_buffer
Buffer for (incomplete) request data.
double server_idle_ttl_s
The maximum TCPServer idle time in seconds before stopping the server, honoring minservers.
threads::Mutex stats_mutex_
Protects prev_stats_, last_stats_.
bool stop_server_
If true, the TCPListener will gracefully stop and finish.
TCPListener::SockState & operator|=(TCPListener::SockState &self, TCPListener::SockState other)
Bitwise |= on SocketWork.
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
struct timeval server_stopped_check_time_
Time of last check for stopped servers.
int pollbatch
The number of epoll_wait events read in one epol__wait wake-up.