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)
 
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
 
void switchSocketContext (SocketContext *newSocketContext)
 
virtual std::size_t readFromPeer (char *chunk, std::size_t chunklen) const=0
 
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
 
void readFromPeer (std::size_t available)
 
- Protected Member Functions inherited from core::socket::SocketContext
 SocketContext ()=default
 
virtual ~SocketContext ()
 
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
 
core::socket::stream::SocketContextnewSocketContext = nullptr
 
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 , typename ConfigT >
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 }
core::socket::stream::SocketConnection * socketConnection
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint

References 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 caller graph for this function:

Member Function Documentation

◆ attach()

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

Definition at line 178 of file SocketContext.cpp.

References alreadyTotalProcessed, alreadyTotalQueued, 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 125 of file SocketContext.cpp.

125 {
127 }

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

Referenced by iot::mqtt::SocketContext::close(), web::http::client::MasterRequest::executeUpgrade(), iot::mqtt_fast::SocketContext::onReceivedFromPeer(), web::http::client::MasterRequest::onSourceError(), web::http::server::Response::onSourceError(), readFromPeer(), 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 185 of file SocketContext.cpp.

185 {
187
188 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: detached";
189 LOG(DEBUG) << " Online Since: " << getOnlineSince();
190 LOG(DEBUG) << " Online Duration: " << getOnlineDuration();
191 LOG(DEBUG) << " Total Sent: " << getTotalQueued();
192 LOG(DEBUG) << " Total Processed: " << getTotalProcessed();
193
194 delete this;
195 }
const std::string & getConnectionName() const
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::setSocketContext(), and core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::unobservedEvent().

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 212 of file SocketContext.cpp.

213 {
214 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
215
216 const seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
217
218 // Compute days, hours, minutes, and seconds
219 const seconds_duration_type days = totalSeconds / 86400; // 86400 seconds in a day
220 seconds_duration_type remainder = totalSeconds % 86400;
221 const seconds_duration_type hours = remainder / 3600;
222 remainder = remainder % 3600;
223 const seconds_duration_type minutes = remainder / 60;
224 const seconds_duration_type seconds = remainder % 60;
225
226 // Format the components into a string using stringstream
227 std::ostringstream oss;
228 if (days > 0) {
229 oss << days << " day" << (days == 1 ? "" : "s") << ", ";
230 }
231 oss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2)
232 << std::setfill('0') << seconds;
233
234 return oss.str();
235 }

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 148 of file SocketContext.cpp.

148 {
150 }
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 145 of file SocketContext.cpp.

145 {
147 }
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 70 of file SocketContext.cpp.

70 {
71 return socketConnection;
72 }

References socketConnection.

Referenced by web::http::server::SocketContext::deliverRequest(), web::http::client::SocketContext::deliverResponse(), web::http::client::SocketContext::deliverResponseParseError(), express::dispatcher::ApplicationDispatcher::dispatch(), express::dispatcher::MiddlewareDispatcher::dispatch(), express::dispatcher::RouterDispatcher::dispatch(), web::http::client::MasterRequest::executeUpgrade(), iot::mqtt::SocketContext::getSocketConnection(), web::http::client::SocketContext::initiateRequest(), main(), 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::requestCompleted(), web::http::client::SocketContext::requestDelivered(), web::http::client::MasterRequest::requestEventSource(), web::http::client::SocketContext::requestPrepared(), web::http::server::SocketContext::responseCompleted(), web::http::client::SocketContext::responseStarted(), web::http::server::SocketContext::responseStarted(), web::http::client::SocketContext::setSseEventReceiver(), web::http::server::SocketContext::SocketContext(), express::middleware::StaticMiddleware::StaticMiddleware(), web::http::server::Response::upgrade(), web::http::client::Request::upgrade(), web::http::client::MasterRequest::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 141 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 133 of file SocketContext.cpp.

133 {
135 }

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 137 of file SocketContext.cpp.

137 {
139 }
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 129 of file SocketContext.cpp.

129 {
131 }
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 167 of file SocketContext.cpp.

167 {
168 errno = errnum;
169
170 if (errno == 0) {
171 LOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: EOF received";
172 } else {
173 PLOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: onReadError";
174 }
176 }
void shutdownWrite(bool forceClose=false)

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

Referenced by core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::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 160 of file SocketContext.cpp.

160 {
161 errno = errnum;
162
163 PLOG(DEBUG) << socketConnection->getConnectionName() << " SocketContext: onWriteError";
164 shutdownRead();
165 }

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

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

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

◆ readFromPeer() [1/3]

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

Implements core::socket::SocketContext.

Definition at line 108 of file SocketContext.cpp.

108 {
109 std::size_t ret = 0;
110
111 if (newSocketContext == nullptr) {
112 ret = socketConnection->readFromPeer(chunk, chunklen);
113 } else {
115 << " ReadFromPeer: New SocketContext != nullptr: SocketContextSwitch still in progress";
116 }
117
118 return ret;
119 }
virtual std::size_t readFromPeer(char *chunk, std::size_t chunkLen)=0
core::socket::stream::SocketContext * newSocketContext

References core::socket::stream::SocketConnection::getConnectionName(), newSocketContext, 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:

◆ readFromPeer() [2/3]

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

◆ readFromPeer() [3/3]

void core::socket::stream::SocketContext::readFromPeer ( std::size_t  available)
protected

Definition at line 86 of file SocketContext.cpp.

86 {
87 const std::size_t consumed = onReceivedFromPeer();
88
89 const std::string connectionName = socketConnection->getConnectionName();
90
91 if (available != 0 && consumed == 0) {
92 LOG(TRACE) << connectionName << ": Data available: " << available << " but nothing read";
93
94 close();
95
96 delete newSocketContext; // delete of nullptr is valid since C++14!
97 newSocketContext = nullptr;
98 } else if (newSocketContext != nullptr) { // Perform a pending SocketContextSwitch
101
102 socketConnection->setSocketContext(newSocketContext);
103
104 LOG(DEBUG) << connectionName << " SocketContext: switch completed";
105 }
106 }
virtual std::size_t onReceivedFromPeer()=0

References close(), core::socket::stream::SocketConnection::getConnectionName(), newSocketContext, core::socket::SocketContext::onReceivedFromPeer(), core::socket::stream::SocketConnection::setSocketContext(), and socketConnection.

Referenced by core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::onReceivedFromPeer().

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 74 of file SocketContext.cpp.

74 {
75 socketConnection->sendToPeer(chunk, chunkLen);
76 }
virtual void sendToPeer(const char *chunk, std::size_t chunkLen)=0

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

Referenced by web::http::client::MasterRequest::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 121 of file SocketContext.cpp.

121 {
123 }
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 152 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 156 of file SocketContext.cpp.

156 {
157 socketConnection->shutdownWrite(forceClose);
158 }
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::client::MasterRequest::requestEventSource(), 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 82 of file SocketContext.cpp.

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

Referenced by web::http::client::MasterRequest::onSourceEof(), web::http::server::Response::onSourceEof(), web::http::client::MasterRequest::onSourceError(), web::http::server::Response::onSourceError(), web::http::client::MasterRequest::~MasterRequest(), 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 78 of file SocketContext.cpp.

78 {
79 return socketConnection->streamToPeer(source);
80 }
virtual bool streamToPeer(core::pipe::Source *source)=0

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

Referenced by web::http::client::MasterRequest::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)

Definition at line 65 of file SocketContext.cpp.

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

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

Referenced by web::http::server::Response::upgrade(), 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 197 of file SocketContext.cpp.

197 {
198 const std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
199 std::tm* tm_ptr = std::gmtime(&time);
200
201 char buffer[100];
202 std::string onlineSince = "Formatting error";
203
204 // Format: "2025-02-02 14:30:00"
205 if (std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_ptr) > 0) {
206 onlineSince = std::string(buffer) + " UTC";
207 }
208
209 return onlineSince;
210 }
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 130 of file SocketContext.h.

◆ SocketConnectionT

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

Definition at line 129 of file SocketContext.h.

Member Data Documentation

◆ alreadyTotalProcessed

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

Definition at line 124 of file SocketContext.h.

Referenced by attach(), and getTotalProcessed().

◆ alreadyTotalQueued

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

Definition at line 123 of file SocketContext.h.

Referenced by attach(), and getTotalQueued().

◆ newSocketContext

core::socket::stream::SocketContext* core::socket::stream::SocketContext::newSocketContext = nullptr
private

Definition at line 120 of file SocketContext.h.

Referenced by readFromPeer(), readFromPeer(), and switchSocketContext().

◆ onlineSince

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

Definition at line 122 of file SocketContext.h.

◆ onlineSinceTimePoint

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

Definition at line 126 of file SocketContext.h.

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

◆ socketConnection


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