|
dodo
0.0.1
A C++ library to create containerized Linux services
|
Used in conjunction with TCPListener to implement high speed, multithreaded TCP services. More...
#include <tcpserver.hpp>


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 TCPServer * | addServer ()=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 TCPConnectionData * | newConnectionData () 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... | |
| TCPListener & | listener_ |
| 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 |
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
At any given moment, each TCPServer is in one of the TCPServer::ServerState states.
Definition at line 56 of file tcpserver.hpp.
The possible states of a TCPServer.
| 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.
| dodo::network::TCPServer::TCPServer | ( | TCPListener & | listener | ) |
Construct a TCPServer for the TCPListener.
| listener | TCPListener. |
Definition at line 36 of file tcpserver.cpp.
References last_active_.
|
pure virtual |
Spawn a new TCPServer.
Referenced by dodo::network::TCPListener::addServers(), dodo::network::TCPListener::cleanStoppedServers(), and dodo::network::TCPListener::run().

| double dodo::network::TCPServer::getIdleSeconds | ( | ) |
Get the number of secodns the TCPServer has been idle.
Definition at line 183 of file tcpserver.cpp.
References dodo::common::getSecondDiff(), and last_active_.

|
inline |
Return true if the TCPServer was requested to stop.
Definition at line 156 of file tcpserver.hpp.
References request_stop_.
|
inline |
Return the current ServerState.
Definition at line 162 of file tcpserver.hpp.
|
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.
| socket | A pointer to the socket to handshake. If this returns false,. the socket will be closed. |
| received | The number of bytes received by the call. |
| sent | The number of bytes sent by the call. |
Referenced by run().

|
inline |
Return true if the TCPServer has stopped working.
Definition at line 133 of file tcpserver.hpp.
References has_stopped_.
Referenced by dodo::network::TCPListener::cleanStoppedServers().

|
inline |
Return true if the TCPServer is busy.
Definition at line 139 of file tcpserver.hpp.
References busy_.
|
inlinevirtual |
Return a new TCPConnectionData pointer or override to return a descendant of TCPConnectionData.
Definition at line 127 of file tcpserver.hpp.
Referenced by dodo::network::TCPListener::run().

|
pure virtual |
Override to perform a request-response cycle.
| work | The work to perform. |
| sent | The number of bytes sent by the call. |
Referenced by run().

|
inline |
Set the request_stop_ flag, this call returns immediately, stopping will follow.
Definition at line 150 of file tcpserver.hpp.
References request_stop_.
|
virtual |
Run the TCPServer thread.
Implements dodo::threads::Thread.
Definition at line 44 of file tcpserver.cpp.
References dodo::network::TCPListener::addReceivedSentBytes(), dodo::network::Address::asString(), busy_, dodo::network::TCPListener::SocketWork::data, dodo::common::DebugObject::debugString(), dodo::common::SystemError::ecEAGAIN, dodo::common::SystemError::ecECONNABORTED, dodo::common::SystemError::ecOK, dodo::network::BaseSocket::getFD(), dodo::network::BaseSocket::getPeerAddress(), dodo::common::getSecondDiff(), handShake(), has_stopped_, last_active_, listener_, log_Debug, log_Error, log_Fatal, dodo::network::TCPListener::New, dodo::network::TCPListener::None, dodo::network::TCPListener::popWork(), dodo::network::TCPListener::Read, dodo::network::TCPConnectionData::readBuffer(), readSocket(), dodo::network::TCPListener::releaseWork(), request_stop_, dodo::network::TCPListener::Shut, shutDown(), dodo::threads::Thread::snapRUsage(), dodo::network::TCPListener::SocketWork::socket, ssAwoken, ssHandshake, ssHandshakeDone, ssReadSocket, ssReadSocketDone, ssReleaseWork, ssReleaseWorkDone, ssShutdown, ssShutdownDone, ssWait, dodo::network::TCPListener::SocketWork::state, state_, and dodo::network::TCPListener::waitForActivity().

|
pure virtual |
Override to perform a shutdown.
| socket | The socket to work on. |
Referenced by run().

|
protected |
If true, the TCPServer is processing a request.
Definition at line 184 of file tcpserver.hpp.
|
protected |
If true, the TCPServer has stopped.
Definition at line 179 of file tcpserver.hpp.
Referenced by hasStopped(), and run().
|
protected |
Time of last request handling.
Definition at line 189 of file tcpserver.hpp.
Referenced by getIdleSeconds(), run(), and TCPServer().
|
protected |
The TCPListener the TCPServer is working for.
Definition at line 162 of file tcpserver.hpp.
Referenced by run().
|
protected |
If true, the TCPServer should stop.
Definition at line 174 of file tcpserver.hpp.
Referenced by getRequestStop(), requestStop(), and run().
|
protected |