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::SocketAddressgetBindAddress () const =0
 
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 void setReadTimeout (const utils::Timeval &timeout)=0
 
virtual void setWriteTimeout (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 (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
 

Private Member Functions

void setSocketContext (SocketContext *socketContext)
 

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()

◆ 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 145 of file SocketConnection.cpp.

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

Referenced by getOnlineDuration().

Here is the caller graph for this function:

◆ getBindAddress()

◆ getConfig()

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

Definition at line 126 of file SocketConnection.cpp.

126 {
127 return config;
128 }

◆ getConnectionName()

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

Definition at line 110 of file SocketConnection.cpp.

110 {
111 return connectionName;
112 }

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::MasterRequest::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::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::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::MasterRequest::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 106 of file SocketConnection.cpp.

106 {
107 return instanceName;
108 }

References instanceName.

◆ getLocalAddress()

◆ getOnlineDuration()

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

Definition at line 122 of file SocketConnection.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.

Here is the call graph for this function:

◆ getOnlineSince()

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

Definition at line 118 of file SocketConnection.cpp.

118 {
120 }
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 114 of file SocketConnection.cpp.

114 {
115 return socketContext;
116 }
core::socket::stream::SocketContext * socketContext

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 94 of file SocketConnection.cpp.

94 {
95 sendToPeer(data.data(), data.size());
96 }
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 102 of file SocketConnection.cpp.

102 {
103 sendToPeer(data.data(), data.size());
104 }

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 98 of file SocketConnection.cpp.

98 {
99 sendToPeer(reinterpret_cast<const char*>(data.data()), data.size());
100 }

◆ setReadTimeout()

◆ setSocketContext() [1/2]

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

Definition at line 72 of file SocketConnection.cpp.

72 {
73 SocketContext* socketContext = socketContextFactory->create(this);
74
75 if (socketContext != nullptr) {
76 LOG(DEBUG) << connectionName << ": SocketContext created successful";
78 } else {
79 LOG(ERROR) << connectionName << ": SocketContext failed to create";
80 close();
81 }
82 }
void setSocketContext(const std::shared_ptr< SocketContextFactory > &socketContextFactory)
friend class core::socket::stream::SocketContext

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

Here is the call graph for this function:

◆ setSocketContext() [2/2]

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

Definition at line 84 of file SocketConnection.cpp.

84 {
85 if (this->socketContext != nullptr) {
86 this->socketContext->detach();
87 }
88
90
92 }

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

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

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

◆ setTimeout()

◆ setWriteTimeout()

◆ 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 130 of file SocketConnection.cpp.

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

Member Data Documentation

◆ config

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

Definition at line 152 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 149 of file SocketConnection.h.

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

◆ socketContext


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