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

#include <SocketConnection.h>

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

Public Member Functions

 SocketConnection (int fd, const net::config::ConfigInstance *config)
 
 SocketConnection (const SocketConnection &)=delete
 
virtual int getFd () const =0
 
virtual void sendToPeer (const char *chunk, std::size_t chunkLen)=0
 
void sendToPeer (const std::string &data)
 
void sentToPeer (const std::vector< uint8_t > &data)
 
void sentToPeer (const std::vector< char > &data)
 
virtual bool streamToPeer (core::pipe::Source *source)=0
 
virtual void streamEof ()=0
 
virtual std::size_t readFromPeer (char *chunk, std::size_t chunkLen)=0
 
virtual void shutdownRead ()=0
 
virtual void shutdownWrite (bool forceClose)=0
 
const std::string & getInstanceName () const
 
const std::string & getConnectionName () const
 
SocketContextgetSocketContext () const
 
virtual const core::socket::SocketAddressgetLocalAddress () const =0
 
virtual const core::socket::SocketAddressgetRemoteAddress () const =0
 
virtual void close ()=0
 
virtual void setTimeout (const utils::Timeval &timeout)=0
 
virtual std::size_t getTotalSent () const =0
 
virtual std::size_t getTotalQueued () const =0
 
virtual std::size_t getTotalRead () const =0
 
virtual std::size_t getTotalProcessed () const =0
 
std::string getOnlineSince () const
 
std::string getOnlineDuration () const
 
const net::config::ConfigInstancegetConfig () const
 

Protected Member Functions

virtual ~SocketConnection ()
 
void setSocketContext (SocketContext *socketContext)
 
void connectSocketContext (const std::shared_ptr< SocketContextFactory > &socketContextFactory)
 

Protected Attributes

core::socket::stream::SocketContextsocketContext = nullptr
 
std::string instanceName
 
std::string connectionName
 
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
 

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

const net::config::ConfigInstanceconfig
 

Friends

class core::socket::stream::SocketContext
 

Detailed Description

Definition at line 82 of file SocketConnection.h.

Constructor & Destructor Documentation

◆ SocketConnection() [1/2]

core::socket::stream::SocketConnection::SocketConnection ( int  fd,
const net::config::ConfigInstance config 
)

Definition at line 62 of file SocketConnection.cpp.

64 , connectionName("[" + std::to_string(fd) + "]" + (!instanceName.empty() ? " " : "") + instanceName)
65 , onlineSinceTimePoint(std::chrono::system_clock::now())
66 , config(config) {
67 }
const net::config::ConfigInstance * config
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
const std::string & getInstanceName() const

References connectionName, net::config::ConfigInstance::getInstanceName(), instanceName, and onlineSinceTimePoint.

Here is the call graph for this function:

◆ SocketConnection() [2/2]

core::socket::stream::SocketConnection::SocketConnection ( const SocketConnection )
delete

◆ ~SocketConnection()

core::socket::stream::SocketConnection::~SocketConnection ( )
protectedvirtual

Definition at line 69 of file SocketConnection.cpp.

69 {
70 }

Member Function Documentation

◆ close()

◆ connectSocketContext()

void core::socket::stream::SocketConnection::connectSocketContext ( const std::shared_ptr< SocketContextFactory > &  socketContextFactory)
protected

Definition at line 120 of file SocketConnection.cpp.

120 {
121 SocketContext* socketContext = socketContextFactory->create(this);
122
123 if (socketContext != nullptr) {
124 LOG(DEBUG) << connectionName << ": SocketContext created successful";
126 } else {
127 LOG(ERROR) << connectionName << ": SocketContext failed to create";
128 close();
129 }
130 }
core::socket::stream::SocketContext * socketContext
void setSocketContext(SocketContext *socketContext)
friend class core::socket::stream::SocketContext

References close(), connectionName, core::socket::stream::SocketContextFactory::create(), and setSocketContext().

Here is the call graph for this function:

◆ durationToString()

std::string core::socket::stream::SocketConnection::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 147 of file SocketConnection.cpp.

148 {
149 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
150
151 const seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
152
153 // Compute days, hours, minutes, and seconds
154 const seconds_duration_type days = totalSeconds / 86400; // 86400 seconds in a day
155 seconds_duration_type remainder = totalSeconds % 86400;
156 const seconds_duration_type hours = remainder / 3600;
157 remainder = remainder % 3600;
158 const seconds_duration_type minutes = remainder / 60;
159 const seconds_duration_type seconds = remainder % 60;
160
161 // Format the components into a string using stringstream
162 std::ostringstream oss;
163 if (days > 0) {
164 oss << days << " day" << (days == 1 ? "" : "s") << ", ";
165 }
166 oss << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2)
167 << std::setfill('0') << seconds;
168
169 return oss.str();
170 }

Referenced by getOnlineDuration().

Here is the caller graph for this function:

◆ getConfig()

const net::config::ConfigInstance * core::socket::stream::SocketConnection::getConfig ( ) const

Definition at line 116 of file SocketConnection.cpp.

116 {
117 return config;
118 }

◆ getConnectionName()

const std::string & core::socket::stream::SocketConnection::getConnectionName ( ) const

Definition at line 100 of file SocketConnection.cpp.

100 {
101 return connectionName;
102 }

References connectionName.

Referenced by web::websocket::SubProtocol< SocketContextUpgradeT >::attach(), web::http::server::SocketContext::deliverRequest(), web::http::client::SocketContext::deliverResponse(), web::http::client::SocketContext::deliverResponseParseError(), core::socket::stream::SocketContext::detach(), web::websocket::SubProtocol< SocketContextUpgradeT >::detach(), express::dispatcher::ApplicationDispatcher::dispatch(), express::dispatcher::MiddlewareDispatcher::dispatch(), express::dispatcher::RouterDispatcher::dispatch(), web::http::client::Request::executeUpgrade(), 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::websocket::SubProtocol< SocketContextUpgradeT >::onPongReceived(), core::socket::stream::SocketContext::onReadError(), web::http::client::SocketContext::onSignal(), web::http::server::SocketContext::onSignal(), core::socket::stream::SocketContext::onWriteError(), core::socket::stream::SocketContext::readFromPeer(), core::socket::stream::SocketContext::readFromPeer(), 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::websocket::SubProtocol< SocketContextUpgradeT >::sendPing(), web::http::server::SocketContext::SocketContext(), express::middleware::StaticMiddleware::StaticMiddleware(), web::websocket::SubProtocol< SocketContextUpgradeT >::SubProtocol(), core::socket::stream::SocketContext::switchSocketContext(), web::http::server::Response::upgrade(), web::http::client::Request::upgrade(), web::http::client::Request::upgrade(), express::middleware::VerboseRequest::VerboseRequest(), and web::http::client::SocketContext::~SocketContext().

Here is the caller graph for this function:

◆ getFd()

◆ getInstanceName()

const std::string & core::socket::stream::SocketConnection::getInstanceName ( ) const

Definition at line 96 of file SocketConnection.cpp.

96 {
97 return instanceName;
98 }

References instanceName.

◆ getLocalAddress()

◆ getOnlineDuration()

std::string core::socket::stream::SocketConnection::getOnlineDuration ( ) const

Definition at line 112 of file SocketConnection.cpp.

112 {
114 }
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.

Here is the call graph for this function:

◆ getOnlineSince()

std::string core::socket::stream::SocketConnection::getOnlineSince ( ) const

Definition at line 108 of file SocketConnection.cpp.

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

References onlineSinceTimePoint, and timePointToString().

Here is the call graph for this function:

◆ getRemoteAddress()

◆ getSocketContext()

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

Definition at line 104 of file SocketConnection.cpp.

104 {
105 return socketContext;
106 }

References socketContext.

◆ getTotalProcessed()

◆ getTotalQueued()

◆ getTotalRead()

◆ getTotalSent()

◆ readFromPeer()

virtual std::size_t core::socket::stream::SocketConnection::readFromPeer ( char *  chunk,
std::size_t  chunkLen 
)
pure virtual

◆ sendToPeer() [1/2]

◆ sendToPeer() [2/2]

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

Definition at line 84 of file SocketConnection.cpp.

84 {
85 sendToPeer(data.data(), data.size());
86 }
virtual void sendToPeer(const char *chunk, std::size_t chunkLen)=0

References sendToPeer().

Referenced by tls::getClient(), and legacy::getLegacyClient().

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

◆ sentToPeer() [1/2]

void core::socket::stream::SocketConnection::sentToPeer ( const std::vector< char > &  data)

Definition at line 92 of file SocketConnection.cpp.

92 {
93 sendToPeer(data.data(), data.size());
94 }

References sendToPeer().

Here is the call graph for this function:

◆ sentToPeer() [2/2]

void core::socket::stream::SocketConnection::sentToPeer ( const std::vector< uint8_t > &  data)

Definition at line 88 of file SocketConnection.cpp.

88 {
89 sendToPeer(reinterpret_cast<const char*>(data.data()), data.size());
90 }

References sendToPeer().

Here is the call graph for this function:

◆ setSocketContext()

void core::socket::stream::SocketConnection::setSocketContext ( SocketContext socketContext)
protected

Definition at line 72 of file SocketConnection.cpp.

72 {
73 if (socketContext != nullptr) { // Perform a pending SocketContextSwitch
75
76 LOG(DEBUG) << connectionName << ": SocketContext switch completed";
77
79 } else {
80 LOG(ERROR) << connectionName << ": SocketContext switch failed: no new SocketContext";
81 }
82 }

References core::socket::stream::SocketContext::attach(), connectionName, and socketContext.

Referenced by connectSocketContext(), and core::socket::stream::SocketContext::readFromPeer().

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

◆ setTimeout()

◆ shutdownRead()

◆ shutdownWrite()

◆ streamEof()

◆ streamToPeer()

◆ timePointToString()

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

Definition at line 132 of file SocketConnection.cpp.

132 {
133 const std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
134 std::tm* tm_ptr = std::gmtime(&time);
135
136 char buffer[100];
137 std::string onlineSince = "Formatting error";
138
139 // Format: "2025-02-02 14:30:00"
140 if (std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_ptr) > 0) {
141 onlineSince = std::string(buffer) + " UTC";
142 }
143
144 return onlineSince;
145 }
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

◆ core::socket::stream::SocketContext

Definition at line 150 of file SocketConnection.h.

Member Data Documentation

◆ config

const net::config::ConfigInstance* core::socket::stream::SocketConnection::config
private

Definition at line 148 of file SocketConnection.h.

◆ connectionName

◆ instanceName

std::string core::socket::stream::SocketConnection::instanceName
protected

◆ onlineSinceTimePoint

std::chrono::time_point<std::chrono::system_clock> core::socket::stream::SocketConnection::onlineSinceTimePoint
protected

Definition at line 145 of file SocketConnection.h.

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

◆ socketContext


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