SNode.C
Loading...
Searching...
No Matches
core::socket::stream::SocketContext Class Referenceabstract

#include <SocketContext.h>

Inheritance diagram for core::socket::stream::SocketContext:
Collaboration diagram for core::socket::stream::SocketContext:

Public Member Functions

 SocketContext (core::socket::stream::SocketConnection *socketConnection)
 
 ~SocketContext () override
 
void sendToPeer (const char *chunk, std::size_t chunkLen) const final
 
bool streamToPeer (core::pipe::Source *source) const
 
void streamEof ()
 
std::size_t readFromPeer (char *chunk, std::size_t chunklen) const final
 
void setTimeout (const utils::Timeval &timeout) final
 
void shutdownRead ()
 
void shutdownWrite (bool forceClose=false)
 
void close () override
 
std::size_t getTotalSent () const override
 
std::size_t getTotalQueued () const override
 
std::size_t getTotalRead () const override
 
std::size_t getTotalProcessed () const override
 
std::string getOnlineSince () const override
 
std::string getOnlineDuration () const override
 
SocketConnectiongetSocketConnection () const
 
virtual void switchSocketContext (SocketContext *newSocketContext)
 
void sendToPeer (const std::string &data) const
 
virtual void sendToPeer (const char *chunk, std::size_t chunkLen) const=0
 
- Public Member Functions inherited from core::socket::SocketContext
 SocketContext (const SocketContext &)=delete
 
 SocketContext (SocketContext &&)=delete
 
SocketContextoperator= (const SocketContext &)=delete
 
SocketContextoperator= (SocketContext &&)=delete
 
void sendToPeer (const std::string &data) const
 

Protected Member Functions

void onWriteError (int errnum) override
 
void onReadError (int errnum) override
 
- Protected Member Functions inherited from core::socket::SocketContext
 SocketContext ()=default
 
virtual std::size_t onReceivedFromPeer ()=0
 
virtual bool onSignal (int sig)=0
 

Private Types

using Super = core::socket::SocketContext
 

Private Member Functions

virtual void onConnected ()=0
 
virtual void onDisconnected ()=0
 
virtual void attach ()
 
virtual void detach ()
 

Static Private Member Functions

static std::string timePointToString (const std::chrono::time_point< std::chrono::system_clock > &timePoint)
 
static std::string durationToString (const std::chrono::time_point< std::chrono::system_clock > &bevore, const std::chrono::time_point< std::chrono::system_clock > &later=std::chrono::system_clock::now())
 

Private Attributes

core::socket::stream::SocketConnectionsocketConnection
 
std::string onlineSince
 
std::size_t alreadyTotalQueued = 0
 
std::size_t alreadyTotalProcessed = 0
 
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
 

Friends

template<typename PhysicalSocketT , class SocketReaderT , class SocketWriterT >
class SocketConnectionT
 
class SocketConnection
 

Detailed Description

Definition at line 66 of file SocketContext.h.

Member Typedef Documentation

◆ Super

Constructor & Destructor Documentation

◆ SocketContext()

core::socket::stream::SocketContext::SocketContext ( core::socket::stream::SocketConnection socketConnection)
explicit

Definition at line 60 of file SocketContext.cpp.

62 , onlineSinceTimePoint(std::chrono::system_clock::now()) {
63 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: created";
64 }
const std::string & getConnectionName() const
core::socket::stream::SocketConnection * socketConnection
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint

References core::socket::stream::SocketConnection::getConnectionName(), onlineSinceTimePoint, and socketConnection.

Referenced by apps::echo::model::EchoSocketContext::EchoSocketContext(), apps::http::SimpleSocketProtocol::SimpleSocketProtocol(), iot::mqtt_fast::SocketContext::SocketContext(), web::http::client::SocketContext::SocketContext(), web::http::server::SocketContext::SocketContext(), and iot::mqtt::SocketContext::SocketContext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~SocketContext()

core::socket::stream::SocketContext::~SocketContext ( )
overridevirtual

Reimplemented from core::socket::SocketContext.

Reimplemented in web::http::client::SocketContext.

Definition at line 66 of file SocketContext.cpp.

66 {
67 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: disconnected";
68 }

References core::socket::stream::SocketConnection::getConnectionName(), and socketConnection.

Here is the call graph for this function:

Member Function Documentation

◆ attach()

void core::socket::stream::SocketContext::attach ( )
privatevirtual

Definition at line 152 of file SocketContext.cpp.

152 {
155
156 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: attach";
157
158 onConnected();
159 }
virtual std::size_t getTotalQueued() const =0
virtual std::size_t getTotalProcessed() const =0

References alreadyTotalProcessed, alreadyTotalQueued, core::socket::stream::SocketConnection::getConnectionName(), core::socket::stream::SocketConnection::getTotalProcessed(), core::socket::stream::SocketConnection::getTotalQueued(), onConnected(), and socketConnection.

Referenced by core::socket::stream::SocketConnection::setSocketContext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ close()

void core::socket::stream::SocketContext::close ( )
overridevirtual

Implements core::socket::SocketContext.

Reimplemented in iot::mqtt::SocketContext.

Definition at line 99 of file SocketContext.cpp.

99 {
101 }

References core::socket::stream::SocketConnection::close(), and socketConnection.

Referenced by iot::mqtt::SocketContext::close(), web::http::client::Request::executeUpgrade(), iot::mqtt_fast::SocketContext::onReceivedFromPeer(), web::http::client::Request::onSourceError(), web::http::server::Response::onSourceError(), and web::http::client::Request::upgrade().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ detach()

void core::socket::stream::SocketContext::detach ( )
privatevirtual

Definition at line 161 of file SocketContext.cpp.

161 {
163
164 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: detach";
165 LOG(DEBUG) << " Online Since: " << getOnlineSince();
166 LOG(DEBUG) << " Online Duration: " << getOnlineDuration();
167 LOG(DEBUG) << " Total Sent: " << getTotalQueued();
168 LOG(DEBUG) << " Total Processed: " << getTotalProcessed();
169 }
std::string getOnlineSince() const override
std::size_t getTotalProcessed() const override
std::size_t getTotalQueued() const override
std::string getOnlineDuration() const override

References core::socket::stream::SocketConnection::getConnectionName(), getOnlineDuration(), getOnlineSince(), getTotalProcessed(), getTotalQueued(), onDisconnected(), and socketConnection.

Referenced by core::socket::stream::SocketConnection::disconnectCurrentSocketContext().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ durationToString()

std::string core::socket::stream::SocketContext::durationToString ( const std::chrono::time_point< std::chrono::system_clock > &  bevore,
const std::chrono::time_point< std::chrono::system_clock > &  later = std::chrono::system_clock::now() 
)
staticprivate

Definition at line 186 of file SocketContext.cpp.

187 {
188 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
189
190 const seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
191
192 // Compute days, hours, minutes, and seconds
193 const seconds_duration_type days = totalSeconds / 86400; // 86400 seconds in a day
194 seconds_duration_type remainder = totalSeconds % 86400;
195 const seconds_duration_type hours = remainder / 3600;
196 remainder = remainder % 3600;
197 const seconds_duration_type minutes = remainder / 60;
198 const seconds_duration_type seconds = remainder % 60;
199
200 // Format the components into a string using stringstream
201 std::ostringstream oss;
202 if (days > 0) {
203 oss << days << " day" << (days == 1 ? "" : "s") << ", ";
204 }
205 oss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2)
206 << std::setfill('0') << seconds;
207
208 return oss.str();
209 }

Referenced by getOnlineDuration().

Here is the caller graph for this function:

◆ getOnlineDuration()

std::string core::socket::stream::SocketContext::getOnlineDuration ( ) const
overridevirtual

Implements core::socket::SocketContext.

Reimplemented in web::websocket::SocketContextUpgrade< SubProtocolT, RequestT, ResponseT >, web::websocket::SocketContextUpgrade< SubProtocol, web::http::client::Request, web::http::client::Response >, and web::websocket::SocketContextUpgrade< SubProtocol, web::http::server::Request, web::http::server::Response >.

Definition at line 122 of file SocketContext.cpp.

122 {
124 }
static std::string durationToString(const std::chrono::time_point< std::chrono::system_clock > &bevore, const std::chrono::time_point< std::chrono::system_clock > &later=std::chrono::system_clock::now())

References durationToString(), and onlineSinceTimePoint.

Referenced by detach().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getOnlineSince()

std::string core::socket::stream::SocketContext::getOnlineSince ( ) const
overridevirtual

Implements core::socket::SocketContext.

Reimplemented in web::websocket::SocketContextUpgrade< SubProtocolT, RequestT, ResponseT >, web::websocket::SocketContextUpgrade< SubProtocol, web::http::client::Request, web::http::client::Response >, and web::websocket::SocketContextUpgrade< SubProtocol, web::http::server::Request, web::http::server::Response >.

Definition at line 119 of file SocketContext.cpp.

119 {
121 }
static std::string timePointToString(const std::chrono::time_point< std::chrono::system_clock > &timePoint)

References onlineSinceTimePoint, and timePointToString().

Referenced by detach().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSocketConnection()

SocketConnection * core::socket::stream::SocketContext::getSocketConnection ( ) const

Definition at line 75 of file SocketContext.cpp.

75 {
76 return socketConnection;
77 }

References socketConnection.

Referenced by web::http::server::SocketContext::deliverRequest(), web::http::client::SocketContext::deliverResponse(), web::http::client::SocketContext::deliverResponseParseError(), iot::mqtt::SocketContext::getSocketConnection(), web::http::client::SocketContext::initiateRequest(), web::http::client::SocketContext::onConnected(), web::http::server::SocketContext::onConnected(), web::http::client::SocketContext::onDisconnected(), web::http::server::SocketContext::onDisconnected(), web::http::client::SocketContext::onSignal(), web::http::server::SocketContext::onSignal(), web::http::server::SocketContext::requestCompleted(), web::http::client::SocketContext::requestDelivered(), web::http::client::SocketContext::requestPrepared(), web::http::server::SocketContext::responseCompleted(), web::http::client::SocketContext::responseDelivered(), web::http::client::Request::responseParseError(), web::http::client::SocketContext::responseStarted(), web::http::server::SocketContext::responseStarted(), web::http::client::SocketContext::SocketContext(), web::http::server::SocketContext::SocketContext(), express::middleware::StaticMiddleware::StaticMiddleware(), web::http::server::Response::upgrade(), web::http::client::Request::upgrade(), express::middleware::VerboseRequest::VerboseRequest(), and web::http::client::SocketContext::~SocketContext().

Here is the caller graph for this function:

◆ getTotalProcessed()

std::size_t core::socket::stream::SocketContext::getTotalProcessed ( ) const
overridevirtual

Implements core::socket::SocketContext.

Definition at line 115 of file SocketContext.cpp.

References alreadyTotalProcessed, core::socket::stream::SocketConnection::getTotalProcessed(), and socketConnection.

Referenced by detach().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getTotalQueued()

std::size_t core::socket::stream::SocketContext::getTotalQueued ( ) const
overridevirtual

Implements core::socket::SocketContext.

Definition at line 107 of file SocketContext.cpp.

107 {
109 }

References alreadyTotalQueued, core::socket::stream::SocketConnection::getTotalQueued(), and socketConnection.

Referenced by detach().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getTotalRead()

std::size_t core::socket::stream::SocketContext::getTotalRead ( ) const
overridevirtual

Implements core::socket::SocketContext.

Definition at line 111 of file SocketContext.cpp.

111 {
113 }
virtual std::size_t getTotalRead() const =0

References core::socket::stream::SocketConnection::getTotalRead(), and socketConnection.

Here is the call graph for this function:

◆ getTotalSent()

std::size_t core::socket::stream::SocketContext::getTotalSent ( ) const
overridevirtual

Implements core::socket::SocketContext.

Definition at line 103 of file SocketContext.cpp.

103 {
105 }
virtual std::size_t getTotalSent() const =0

References core::socket::stream::SocketConnection::getTotalSent(), and socketConnection.

Here is the call graph for this function:

◆ onConnected()

◆ onDisconnected()

◆ onReadError()

void core::socket::stream::SocketContext::onReadError ( int  errnum)
overrideprotectedvirtual

Implements core::socket::SocketContext.

Definition at line 141 of file SocketContext.cpp.

141 {
142 errno = errnum;
143
144 if (errno == 0) {
145 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: EOF received";
146 } else {
147 PLOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: onReadError";
148 }
150 }
void shutdownWrite(bool forceClose=false)

References core::socket::stream::SocketConnection::getConnectionName(), shutdownWrite(), and socketConnection.

Referenced by core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::onReadError(), and apps::http::SimpleSocketProtocol::onReadError().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ onWriteError()

void core::socket::stream::SocketContext::onWriteError ( int  errnum)
overrideprotectedvirtual

Implements core::socket::SocketContext.

Reimplemented in web::http::client::SocketContext, and web::http::server::SocketContext.

Definition at line 134 of file SocketContext.cpp.

134 {
135 errno = errnum;
136
137 PLOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: onWriteError";
138 shutdownRead();
139 }

References core::socket::stream::SocketConnection::getConnectionName(), shutdownRead(), and socketConnection.

Referenced by core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::onWriteError(), and apps::http::SimpleSocketProtocol::onWriteError().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readFromPeer()

std::size_t core::socket::stream::SocketContext::readFromPeer ( char *  chunk,
std::size_t  chunklen 
) const
finalvirtual

Implements core::socket::SocketContext.

Definition at line 91 of file SocketContext.cpp.

91 {
92 return socketConnection->readFromPeer(chunk, chunklen);
93 }
virtual std::size_t readFromPeer(char *chunk, std::size_t chunkLen)=0

References core::socket::stream::SocketConnection::readFromPeer(), and socketConnection.

Referenced by apps::echo::model::EchoSocketContext::onReceivedFromPeer(), web::http::decoder::Fields::read(), web::http::decoder::HTTP10Response::read(), web::http::decoder::Identity::read(), web::http::decoder::Chunked::Chunk::read(), web::http::Parser::readStartLine(), and iot::mqtt::SocketContext::recv().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendToPeer() [1/3]

void core::socket::stream::SocketContext::sendToPeer ( const char *  chunk,
std::size_t  chunkLen 
) const
finalvirtual

Implements core::socket::SocketContext.

Definition at line 79 of file SocketContext.cpp.

79 {
80 socketConnection->sendToPeer(chunk, chunkLen);
81 }
virtual void sendToPeer(const char *chunk, std::size_t chunkLen)=0

References core::socket::stream::SocketConnection::sendToPeer(), and socketConnection.

Referenced by web::http::client::Request::executeSendFragment(), apps::echo::model::EchoSocketContext::onReceivedFromPeer(), iot::mqtt::SocketContext::send(), iot::mqtt_fast::SocketContext::send(), and web::http::server::Response::sendFragment().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendToPeer() [2/3]

virtual void core::socket::SocketContext::sendToPeer ( const char *  chunk,
std::size_t  chunkLen 
) const
virtual

◆ sendToPeer() [3/3]

void core::socket::SocketContext::sendToPeer ( const std::string &  data) const

Definition at line 75 of file SocketContext.cpp.

53 {
54 sendToPeer(data.data(), data.length());
55 }
void sendToPeer(const char *chunk, std::size_t chunkLen) const final

◆ setTimeout()

void core::socket::stream::SocketContext::setTimeout ( const utils::Timeval timeout)
finalvirtual

Implements core::socket::SocketContext.

Definition at line 95 of file SocketContext.cpp.

95 {
97 }
virtual void setTimeout(const utils::Timeval &timeout)=0

References core::socket::stream::SocketConnection::setTimeout(), and socketConnection.

Here is the call graph for this function:

◆ shutdownRead()

void core::socket::stream::SocketContext::shutdownRead ( )

Definition at line 126 of file SocketContext.cpp.

References core::socket::stream::SocketConnection::shutdownRead(), and socketConnection.

Referenced by apps::http::SimpleSocketProtocol::onWriteError(), and onWriteError().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdownWrite()

void core::socket::stream::SocketContext::shutdownWrite ( bool  forceClose = false)

Definition at line 130 of file SocketContext.cpp.

130 {
131 socketConnection->shutdownWrite(forceClose);
132 }
virtual void shutdownWrite(bool forceClose)=0

References core::socket::stream::SocketConnection::shutdownWrite(), and socketConnection.

Referenced by web::http::client::SocketContext::deliverResponseParseError(), iot::mqtt::SocketContext::end(), apps::http::SimpleSocketProtocol::onReadError(), onReadError(), web::http::server::SocketContext::requestCompleted(), web::http::client::SocketContext::requestDelivered(), web::http::server::SocketContext::responseCompleted(), and web::http::client::SocketContext::responseStarted().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ streamEof()

void core::socket::stream::SocketContext::streamEof ( )

Definition at line 87 of file SocketContext.cpp.

References socketConnection, and core::socket::stream::SocketConnection::streamEof().

Referenced by web::http::client::Request::onSourceEof(), web::http::server::Response::onSourceEof(), web::http::client::Request::onSourceError(), web::http::server::Response::onSourceError(), web::http::client::Request::~Request(), and web::http::server::Response::~Response().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ streamToPeer()

bool core::socket::stream::SocketContext::streamToPeer ( core::pipe::Source source) const

Definition at line 83 of file SocketContext.cpp.

83 {
84 return socketConnection->streamToPeer(source);
85 }
virtual bool streamToPeer(core::pipe::Source *source)=0

References socketConnection, and core::socket::stream::SocketConnection::streamToPeer().

Referenced by web::http::client::Request::onSourceConnect(), and web::http::server::Response::onSourceConnect().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ switchSocketContext()

void core::socket::stream::SocketContext::switchSocketContext ( SocketContext newSocketContext)
virtual

Definition at line 70 of file SocketContext.cpp.

70 {
71 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: switch";
72 socketConnection->switchSocketContext(newSocketContext);
73 }
void switchSocketContext(SocketContext *newSocketContext)

References core::socket::stream::SocketConnection::getConnectionName(), socketConnection, and core::socket::stream::SocketConnection::switchSocketContext().

Referenced by web::http::server::Response::sendCompleted(), and web::http::client::Request::upgrade().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ timePointToString()

std::string core::socket::stream::SocketContext::timePointToString ( const std::chrono::time_point< std::chrono::system_clock > &  timePoint)
staticprivate

Definition at line 171 of file SocketContext.cpp.

171 {
172 const std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
173 std::tm* tm_ptr = std::gmtime(&time);
174
175 char buffer[100];
176 std::string onlineSince = "Formatting error";
177
178 // Format: "2025-02-02 14:30:00"
179 if (std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_ptr) > 0) {
180 onlineSince = std::string(buffer) + " UTC";
181 }
182
183 return onlineSince;
184 }
time_t time(time_t *tloc)
Definition time.cpp:52

Referenced by getOnlineSince().

Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ SocketConnection

friend class SocketConnection
friend

Definition at line 126 of file SocketContext.h.

◆ SocketConnectionT

template<typename PhysicalSocketT , class SocketReaderT , class SocketWriterT >
friend class SocketConnectionT
friend

Definition at line 125 of file SocketContext.h.

Member Data Documentation

◆ alreadyTotalProcessed

std::size_t core::socket::stream::SocketContext::alreadyTotalProcessed = 0
private

Definition at line 120 of file SocketContext.h.

Referenced by attach(), and getTotalProcessed().

◆ alreadyTotalQueued

std::size_t core::socket::stream::SocketContext::alreadyTotalQueued = 0
private

Definition at line 119 of file SocketContext.h.

Referenced by attach(), and getTotalQueued().

◆ onlineSince

std::string core::socket::stream::SocketContext::onlineSince
private

Definition at line 118 of file SocketContext.h.

◆ onlineSinceTimePoint

std::chrono::time_point<std::chrono::system_clock> core::socket::stream::SocketContext::onlineSinceTimePoint
private

Definition at line 122 of file SocketContext.h.

Referenced by getOnlineDuration(), getOnlineSince(), and SocketContext().

◆ socketConnection


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