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
void setSocketContext (SocketContext *socketContext)
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::ConfigInstancegetConfigInstance () const

Protected Member Functions

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

Protected Attributes

core::socket::stream::SocketContextsocketContext = nullptr
core::socket::stream::SocketContextnewSocketContext = 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.

63 : instanceName(config->getInstanceName())
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

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 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:

◆ getBindAddress()

◆ getConfigInstance()

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

Definition at line 128 of file SocketConnection.cpp.

128 {
129 return config;
130 }

◆ getConnectionName()

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

Definition at line 112 of file SocketConnection.cpp.

112 {
113 return connectionName;
114 }

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(), web::http::client::SocketContext::requestCompleted(), web::http::server::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(), core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::SocketConnectionT(), web::http::server::SocketContext::SocketContext(), express::middleware::StaticMiddleware::StaticMiddleware(), web::websocket::SubProtocol< SocketContextUpgradeT >::SubProtocol(), web::http::client::MasterRequest::upgrade(), web::http::client::Request::upgrade(), web::http::server::Response::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 108 of file SocketConnection.cpp.

108 {
109 return instanceName;
110 }

References instanceName.

◆ getLocalAddress()

◆ getOnlineDuration()

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

Definition at line 124 of file SocketConnection.cpp.

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

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

116 {
117 return socketContext;
118 }
core::socket::stream::SocketContext * socketContext

References socketContext.

◆ getTotalProcessed()

◆ getTotalQueued()

◆ getTotalRead()

◆ getTotalSent()

◆ readFromPeer()

◆ sendToPeer() [1/2]

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

◆ sendToPeer() [2/2]

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

Definition at line 96 of file SocketConnection.cpp.

96 {
97 sendToPeer(data.data(), data.size());
98 }
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 104 of file SocketConnection.cpp.

104 {
105 sendToPeer(data.data(), data.size());
106 }

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

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

References sendToPeer().

Here is the call graph for this function:

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

Definition at line 84 of file SocketConnection.cpp.

84 {
85 if (this->socketContext == nullptr) {
87
88 socketContext->attach();
89 } else {
90 LOG(DEBUG) << connectionName << " SocketContext: switch";
91
93 }
94 }
core::socket::stream::SocketContext * newSocketContext

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

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

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 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:

◆ 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

Definition at line 146 of file SocketConnection.h.

Referenced by getInstanceName(), and SocketConnection().

◆ newSocketContext

core::socket::stream::SocketContext* core::socket::stream::SocketConnection::newSocketContext = nullptr
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: