dodo  0.0.1
A C++ library to create containerized Linux services
dodo::network::TCPServer Class Referenceabstract

Used in conjunction with TCPListener to implement high speed, multithreaded TCP services. More...

#include <tcpserver.hpp>

Inheritance diagram for dodo::network::TCPServer:
Collaboration diagram for dodo::network::TCPServer:

Public Types

enum  ServerState {
  ssWait = 0, ssAwoken = 1, ssHandshake = 2, ssHandshakeDone = 3,
  ssReadSocket = 4, ssReadSocketDone = 5, ssShutdown = 6, ssShutdownDone = 7,
  ssReleaseWork = 8, ssReleaseWorkDone = 9
}
 The possible states of a TCPServer. More...
 

Public Member Functions

 TCPServer (TCPListener &listener)
 Construct a TCPServer for the TCPListener. More...
 
virtual TCPServeraddServer ()=0
 Spawn a new TCPServer. More...
 
double getIdleSeconds ()
 Get the number of secodns the TCPServer has been idle. More...
 
bool getRequestStop () const
 Return true if the TCPServer was requested to stop. More...
 
ServerState getState () const
 Return the current ServerState. More...
 
virtual bool handShake (network::BaseSocket *socket, ssize_t &received, ssize_t &sent)=0
 Override to perform a protocol handshake. More...
 
bool hasStopped () const
 Return true if the TCPServer has stopped working. More...
 
bool isBusy () const
 Return true if the TCPServer is busy. More...
 
virtual TCPConnectionDatanewConnectionData () const
 Return a new TCPConnectionData pointer or override to return a descendant of TCPConnectionData. More...
 
virtual common::SystemError readSocket (TCPListener::SocketWork &work, ssize_t &sent)=0
 Override to perform a request-response cycle. More...
 
void requestStop ()
 Set the request_stop_ flag, this call returns immediately, stopping will follow. More...
 
virtual void run ()
 Run the TCPServer thread. More...
 
virtual void shutDown (network::BaseSocket *socket)=0
 Override to perform a shutdown. More...
 
- Public Member Functions inherited from dodo::threads::Thread
 Thread ()
 Constructor. More...
 
virtual ~Thread ()
 Destructor. More...
 
double getAvgBlkInRate ()
 Get the average block in rate since thread start() More...
 
double getAvgBlkOutRate ()
 Get the average block out rate since thread start() More...
 
double getAvgICtx ()
 Get the average involuntary context switch rate since thread start() More...
 
double getAvgMajFltRate ()
 Get the average major fault rate since thread start() More...
 
double getAvgMinFltRate ()
 Get the average minor fault rate since thread start() More...
 
double getAvgSysCPU ()
 Get the average system mode cpu (cpu seconds/second) since thread start() More...
 
double getAvgUserCPU ()
 Get the average user mode cpu (cpu seconds/second) since thread start() More...
 
double getAvgVCtx ()
 Get the average voluntary context switch rate since thread start() More...
 
std::thread::id getId () const
 Get the thread id. More...
 
double getLastBlkInRate ()
 Get last block in rate since last sample. More...
 
double getLastBlkOutRate ()
 Get last block out rate since last sample. More...
 
double getLastICtx ()
 Get involuntary context switch rate since last sample. More...
 
double getLastMajFltRate ()
 Get the major fault rate since last sample. More...
 
double getLastMinFltRate ()
 Get the minor fault rate since last sample. More...
 
double getLastSysCPU ()
 Get the system mode cpu (cpu seconds/second) since last sample. More...
 
double getLastUserCPU ()
 Get the user mode cpu (cpu seconds/second) since last sample. More...
 
double getLastVCtx ()
 Get voluntary context switch rate since last sample. More...
 
long getMaxRSS ()
 Get the maximum resident set size seen on the thread. More...
 
double getRunTime ()
 Time, in seconds, since thread start. More...
 
double getSnapDiffTime ()
 Time, in seconds, since last statistic update. More...
 
pid_t getTID () const
 Return the tid. More...
 
void snapRUsage ()
 Take a snapshot of the thread's resource usage. More...
 
void start ()
 Start the thread. More...
 
void wait ()
 Wait for the thread to join the current thread / wait for the thread to finish. More...
 

Protected Attributes

bool busy_
 If true, the TCPServer is processing a request. More...
 
bool has_stopped_
 If true, the TCPServer has stopped. More...
 
struct timeval last_active_
 Time of last request handling. More...
 
TCPListenerlistener_
 The TCPListener the TCPServer is working for. More...
 
bool request_stop_
 If true, the TCPServer should stop. More...
 
ServerState state_
 State of the TCPServer. More...
 
- Protected Attributes inherited from dodo::threads::Thread
struct rusage prev_rusage_
 Previous statistics. More...
 
struct timeval prev_snap_time_
 Time of previous statistics snapshot. More...
 
struct rusage rusage_
 Last statistics. More...
 
struct timeval snap_time_
 Time of last statistics snapshot. More...
 
struct timeval start_time_
 Time Thread started. More...
 
std::thread * thread_
 The std::tread object. More...
 

Additional Inherited Members

Detailed Description

Used in conjunction with TCPListener to implement high speed, multithreaded TCP services.

Subclass TCPServer and implement virtual

Each TCPServer is a thread that consumes work from the TCPListener. There can be multiple TCPServer threads consuming work together, and the TCPListener auto-scales TCPServers depending on the load.

A TCPServer is tied to a TCPListener, The TCPListener produces work to the pool of TCPServer, either

  • new sockets.
  • readable sockets (incoming data present).
  • shutdown sockets (The TCPListener is informing the pool of TCPServer objects the BaseSocket will disappear).

At any given moment, each TCPServer is in one of the TCPServer::ServerState states.

See also
TCPListener

Definition at line 56 of file tcpserver.hpp.

Member Enumeration Documentation

◆ ServerState

The possible states of a TCPServer.

See also
getState()
Enumerator
ssWait 

The TCPServer has entered waiting for activity or wait timeout.

ssAwoken 

The TCPServer has woken up from a wait either due to an event or the wait timing out.

ssHandshake 

The TCPServer is about to invoke handShake(BaseSocket*).

ssHandshakeDone 

ssHandshake completed.

ssReadSocket 

The TCPServer is about to invoke readSocket(BaseSocket*).

ssReadSocketDone 

ssReadSocket completed.

ssShutdown 

The TCPServer is about to invoke shutdown(BaseSocket*).

ssShutdownDone 

ssShutdown completed.

ssReleaseWork 

The TCPServer is releasing the request.

ssReleaseWorkDone 

ssReleaseWork completed.

Definition at line 63 of file tcpserver.hpp.

Constructor & Destructor Documentation

◆ TCPServer()

dodo::network::TCPServer::TCPServer ( TCPListener listener)

Construct a TCPServer for the TCPListener.

Parameters
listenerTCPListener.

Definition at line 36 of file tcpserver.cpp.

References last_active_.

Member Function Documentation

◆ addServer()

virtual TCPServer* dodo::network::TCPServer::addServer ( )
pure virtual

Spawn a new TCPServer.

Returns
a pointer to a new TCPServer

Referenced by dodo::network::TCPListener::addServers(), dodo::network::TCPListener::cleanStoppedServers(), and dodo::network::TCPListener::run().

Here is the caller graph for this function:

◆ getIdleSeconds()

double dodo::network::TCPServer::getIdleSeconds ( )

Get the number of secodns the TCPServer has been idle.

Returns
The idle seconds.

Definition at line 183 of file tcpserver.cpp.

References dodo::common::getSecondDiff(), and last_active_.

Here is the call graph for this function:

◆ getRequestStop()

bool dodo::network::TCPServer::getRequestStop ( ) const
inline

Return true if the TCPServer was requested to stop.

Returns
true if the TCPServer was requested to stop.

Definition at line 156 of file tcpserver.hpp.

References request_stop_.

◆ getState()

ServerState dodo::network::TCPServer::getState ( ) const
inline

Return the current ServerState.

Returns
The current ServerState.

Definition at line 162 of file tcpserver.hpp.

◆ handShake()

virtual bool dodo::network::TCPServer::handShake ( network::BaseSocket socket,
ssize_t &  received,
ssize_t &  sent 
)
pure virtual

Override to perform a protocol handshake.

Note that the TCP handshake has already taken place, and handShake() will only be called for new sockets.

Parameters
socketA pointer to the socket to handshake. If this returns false,. the socket will be closed.
receivedThe number of bytes received by the call.
sentThe number of bytes sent by the call.
Returns
true if the handshake succeeded.

Referenced by run().

Here is the caller graph for this function:

◆ hasStopped()

bool dodo::network::TCPServer::hasStopped ( ) const
inline

Return true if the TCPServer has stopped working.

Returns
True if the TCPServer has stopped on request.

Definition at line 133 of file tcpserver.hpp.

References has_stopped_.

Referenced by dodo::network::TCPListener::cleanStoppedServers().

Here is the caller graph for this function:

◆ isBusy()

bool dodo::network::TCPServer::isBusy ( ) const
inline

Return true if the TCPServer is busy.

Returns
True if busy.

Definition at line 139 of file tcpserver.hpp.

References busy_.

◆ newConnectionData()

virtual TCPConnectionData* dodo::network::TCPServer::newConnectionData ( ) const
inlinevirtual

Return a new TCPConnectionData pointer or override to return a descendant of TCPConnectionData.

Returns
the new TCPConnectionData*.

Definition at line 127 of file tcpserver.hpp.

Referenced by dodo::network::TCPListener::run().

Here is the caller graph for this function:

◆ readSocket()

virtual common::SystemError dodo::network::TCPServer::readSocket ( TCPListener::SocketWork work,
ssize_t &  sent 
)
pure virtual

Override to perform a request-response cycle.

Parameters
workThe work to perform.
sentThe number of bytes sent by the call.
Returns

Referenced by run().

Here is the caller graph for this function:

◆ requestStop()

void dodo::network::TCPServer::requestStop ( )
inline

Set the request_stop_ flag, this call returns immediately, stopping will follow.

Definition at line 150 of file tcpserver.hpp.

References request_stop_.

◆ run()

◆ shutDown()

virtual void dodo::network::TCPServer::shutDown ( network::BaseSocket socket)
pure virtual

Override to perform a shutdown.

Parameters
socketThe socket to work on.

Referenced by run().

Here is the caller graph for this function:

Field Documentation

◆ busy_

bool dodo::network::TCPServer::busy_
protected

If true, the TCPServer is processing a request.

Definition at line 184 of file tcpserver.hpp.

Referenced by isBusy(), and run().

◆ has_stopped_

bool dodo::network::TCPServer::has_stopped_
protected

If true, the TCPServer has stopped.

Definition at line 179 of file tcpserver.hpp.

Referenced by hasStopped(), and run().

◆ last_active_

struct timeval dodo::network::TCPServer::last_active_
protected

Time of last request handling.

Definition at line 189 of file tcpserver.hpp.

Referenced by getIdleSeconds(), run(), and TCPServer().

◆ listener_

TCPListener& dodo::network::TCPServer::listener_
protected

The TCPListener the TCPServer is working for.

Definition at line 162 of file tcpserver.hpp.

Referenced by run().

◆ request_stop_

bool dodo::network::TCPServer::request_stop_
protected

If true, the TCPServer should stop.

Definition at line 174 of file tcpserver.hpp.

Referenced by getRequestStop(), requestStop(), and run().

◆ state_

ServerState dodo::network::TCPServer::state_
protected

State of the TCPServer.

Definition at line 194 of file tcpserver.hpp.

Referenced by run().


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