SNode.C
Loading...
Searching...
No Matches
core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT > Class Template Reference

#include <SocketConnection.h>

Inheritance diagram for core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >:
Collaboration diagram for core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >:

Public Member Functions

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

Protected Types

using Super = core::socket::stream::SocketConnection
 
using Config = ConfigT
 
using PhysicalSocket = PhysicalSocketT
 
using SocketReader = SocketReaderT
 
using SocketWriter = SocketWriterT
 
using SocketAddress = typename PhysicalSocket::SocketAddress
 

Protected Member Functions

 SocketConnectionT (PhysicalSocket &&physicalSocket, const std::function< void()> &onDisconnectm, const std::shared_ptr< Config > &config)
 
 ~SocketConnectionT () override
 
void doWriteShutdown (const std::function< void()> &onShutdown) override
 
void onWriteError (int errnum)
 
void onReadError (int errnum)
 
- Protected Member Functions inherited from core::socket::stream::SocketConnection
virtual ~SocketConnection ()
 
void setSocketContext (SocketContext *socketContext)
 
void connectSocketContext (const std::shared_ptr< SocketContextFactory > &socketContextFactory)
 

Private Member Functions

void onReceivedFromPeer (std::size_t available) final
 
bool onSignal (int signum) final
 
void readTimeout () final
 
void writeTimeout () final
 
void unobservedEvent () final
 

Private Attributes

PhysicalSocket physicalSocket
 
std::function< void()> onDisconnect
 
SocketAddress localAddress {}
 
SocketAddress remoteAddress {}
 
std::shared_ptr< Configconfig
 

Additional Inherited Members

- Protected Attributes inherited from core::socket::stream::SocketConnection
core::socket::stream::SocketContextsocketContext = nullptr
 
std::string instanceName
 
std::string connectionName
 
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
 

Detailed Description

template<typename PhysicalSocketT, typename SocketReaderT, typename SocketWriterT, typename ConfigT>
class core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >

Definition at line 154 of file SocketConnection.h.

Member Typedef Documentation

◆ Config

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
using core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::Config = ConfigT
protected

Definition at line 161 of file SocketConnection.h.

◆ PhysicalSocket

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
using core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::PhysicalSocket = PhysicalSocketT
protected

Definition at line 162 of file SocketConnection.h.

◆ SocketAddress

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
using core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::SocketAddress = typename PhysicalSocket::SocketAddress
protected

Definition at line 165 of file SocketConnection.h.

◆ SocketReader

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
using core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::SocketReader = SocketReaderT
protected

Definition at line 163 of file SocketConnection.h.

◆ SocketWriter

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
using core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::SocketWriter = SocketWriterT
protected

Definition at line 164 of file SocketConnection.h.

◆ Super

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
using core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::Super = core::socket::stream::SocketConnection
protected

Definition at line 159 of file SocketConnection.h.

Constructor & Destructor Documentation

◆ SocketConnectionT() [1/2]

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::SocketConnectionT ( )
delete

◆ SocketConnectionT() [2/2]

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::SocketConnectionT ( PhysicalSocket &&  physicalSocket,
const std::function< void()> &  onDisconnectm,
const std::shared_ptr< Config > &  config 
)
protected

Definition at line 107 of file SocketConnection.hpp.

110 : SocketConnection(physicalSocket.getFd(), config.get())
111 , SocketReader(
112 instanceName + " [" + std::to_string(physicalSocket.getFd()) + "]",
113 [this](int errnum) {
114 {
115 const utils::PreserveErrno pe(errnum);
116 if (errno == 0) {
117 LOG(TRACE) << connectionName << " OnReadError: EOF received";
118 } else {
119 PLOG(TRACE) << connectionName << " OnReadError";
120 }
121 }
123
124 onReadError(errnum);
125 },
126 config->getReadTimeout(),
127 config->getReadBlockSize(),
128 config->getTerminateTimeout())
129 , SocketWriter(
130 instanceName + " [" + std::to_string(physicalSocket.getFd()) + "]",
131 [this](int errnum) {
132 {
133 const utils::PreserveErrno pe(errnum);
134 PLOG(TRACE) << connectionName << " OnWriteError";
135 }
137
138 onWriteError(errnum);
139 },
140 config->getWriteTimeout(),
141 config->getWriteBlockSize(),
142 config->getTerminateTimeout())
143 , physicalSocket(std::move(physicalSocket))
144 , onDisconnect(onDisconnectm)
145 , localAddress(getLocalSocketAddress<SocketAddress>(this->physicalSocket, config))
146 , remoteAddress(getRemoteSocketAddress<SocketAddress>(this->physicalSocket, config))
147 , config(config) {
148 if (!SocketReader::enable(this->physicalSocket.getFd())) {
149 delete this;
150 } else if (!SocketWriter::enable(this->physicalSocket.getFd())) {
151 delete this;
152 } else {
154 }
155 }
SocketConnection(int fd, const net::config::ConfigInstance *config)

References core::socket::stream::SocketConnection::connectionName, core::socket::stream::SocketConnection::instanceName, core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::onReadError(), and utils::PreserveErrno::PreserveErrno().

Here is the call graph for this function:

◆ ~SocketConnectionT()

Definition at line 158 of file SocketConnection.hpp.

158 {
159 }

Member Function Documentation

◆ close()

◆ doWriteShutdown()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::doWriteShutdown ( const std::function< void()> &  onShutdown)
overrideprotected

Definition at line 269 of file SocketConnection.hpp.

269 {
270 errno = 0;
271
273
274 LOG(TRACE) << connectionName << ": Shutdown (WR)";
275
276 if (physicalSocket.shutdown(PhysicalSocket::SHUT::WR) == 0) {
277 LOG(DEBUG) << connectionName << " Shutdown (WR): success";
278 } else {
279 PLOG(ERROR) << connectionName << " Shutdown (WR)";
280 }
281
282 onShutdown();
283 }
void setTimeout(const utils::Timeval &timeout) final

References core::socket::stream::SocketConnection::connectionName, core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::physicalSocket, and core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::setTimeout().

Here is the call graph for this function:

◆ getConfig()

◆ getFd()

◆ getLocalAddress()

Implements core::socket::stream::SocketConnection.

Definition at line 174 of file SocketConnection.hpp.

174 {
175 return localAddress;
176 }

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

Here is the caller graph for this function:

◆ getRemoteAddress()

Implements core::socket::stream::SocketConnection.

Definition at line 180 of file SocketConnection.hpp.

180 {
181 return remoteAddress;
182 }

References core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::remoteAddress.

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

Here is the caller graph for this function:

◆ getTotalProcessed()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
std::size_t core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::getTotalProcessed ( ) const
overridevirtual

Implements core::socket::stream::SocketConnection.

Definition at line 264 of file SocketConnection.hpp.

264 {
266 }
std::size_t getTotalProcessed() const

◆ getTotalQueued()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
std::size_t core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::getTotalQueued ( ) const
overridevirtual

Implements core::socket::stream::SocketConnection.

Definition at line 254 of file SocketConnection.hpp.

254 {
256 }

◆ getTotalRead()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
std::size_t core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::getTotalRead ( ) const
overridevirtual

Implements core::socket::stream::SocketConnection.

Definition at line 259 of file SocketConnection.hpp.

259 {
261 }

◆ getTotalSent()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
std::size_t core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::getTotalSent ( ) const
overridevirtual

Implements core::socket::stream::SocketConnection.

Definition at line 249 of file SocketConnection.hpp.

249 {
251 }

◆ onReadError()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::onReadError ( int  errnum)
protected

Definition at line 296 of file SocketConnection.hpp.

296 {
297 socketContext->onReadError(errnum);
298 }
core::socket::stream::SocketContext * socketContext
void onReadError(int errnum) override

References core::socket::stream::SocketContext::onReadError(), and core::socket::stream::SocketConnection::socketContext.

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

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

◆ onReceivedFromPeer()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::onReceivedFromPeer ( std::size_t  available)
finalprivate

Definition at line 286 of file SocketConnection.hpp.

286 {
287 socketContext->readFromPeer(available);
288 }
std::size_t readFromPeer(char *chunk, std::size_t chunklen) const final

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

Here is the call graph for this function:

◆ onSignal()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
bool core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::onSignal ( int  signum)
finalprivate

Definition at line 301 of file SocketConnection.hpp.

301 {
302 switch (signum) {
303 case SIGINT:
304 [[fallthrough]];
305 case SIGTERM:
306 [[fallthrough]];
307 case SIGABRT:
308 [[fallthrough]];
309 case SIGHUP:
310 LOG(DEBUG) << connectionName << ": Shutting down due to signal '" << strsignal(signum) << "' (SIG"
311 << utils::system::sigabbrev_np(signum) << " [" << signum << "])";
312 break;
313 case SIGALRM:
314 break;
315 }
316
317 return socketContext != nullptr ? socketContext->onSignal(signum) : true;
318 }
virtual bool onSignal(int sig)=0
std::string sigabbrev_np(int sig)
Definition signal.cpp:59

References core::socket::stream::SocketConnection::connectionName, core::socket::SocketContext::onSignal(), utils::system::sigabbrev_np(), and core::socket::stream::SocketConnection::socketContext.

Here is the call graph for this function:

◆ onWriteError()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::onWriteError ( int  errnum)
protected

Definition at line 291 of file SocketConnection.hpp.

291 {
293 }
void onWriteError(int errnum) override

References core::socket::stream::SocketContext::onWriteError(), and core::socket::stream::SocketConnection::socketContext.

Here is the call graph for this function:

◆ readFromPeer()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
std::size_t core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::readFromPeer ( char *  chunk,
std::size_t  chunkLen 
)
finalvirtual

Implements core::socket::stream::SocketConnection.

Definition at line 185 of file SocketConnection.hpp.

185 {
186 return SocketReader::readFromPeer(chunk, chunkLen);
187 }
std::size_t readFromPeer(char *chunk, std::size_t chunkLen)

◆ readTimeout()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::readTimeout ( )
finalprivate

Definition at line 321 of file SocketConnection.hpp.

321 {
322 LOG(WARNING) << connectionName << ": Read timeout";
323 close();
324 }

References core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::close(), and core::socket::stream::SocketConnection::connectionName.

Here is the call graph for this function:

◆ sendToPeer() [1/3]

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::sendToPeer ( const char *  chunk,
std::size_t  chunkLen 
)
finalvirtual

Implements core::socket::stream::SocketConnection.

Definition at line 190 of file SocketConnection.hpp.

190 {
191 SocketWriter::sendToPeer(chunk, chunkLen);
192 }
void sendToPeer(const char *chunk, std::size_t chunkLen)

◆ sendToPeer() [2/3]

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
virtual void core::socket::stream::SocketConnection::sendToPeer ( const char *  chunk,
std::size_t  chunkLen 
)
virtual

◆ sendToPeer() [3/3]

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
void core::socket::stream::SocketConnection::sendToPeer ( const std::string &  data)

Definition at line 96 of file SocketConnection.cpp.

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

◆ setTimeout()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::setTimeout ( const utils::Timeval timeout)
finalvirtual

Implements core::socket::stream::SocketConnection.

Definition at line 162 of file SocketConnection.hpp.

162 {
165 }
void setTimeout(const utils::Timeval &timeout)

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

Here is the caller graph for this function:

◆ shutdownRead()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::shutdownRead ( )
finalvirtual

Implements core::socket::stream::SocketConnection.

Definition at line 205 of file SocketConnection.hpp.

205 {
206 LOG(TRACE) << connectionName << ": Shutdown (RD)";
207
209
210 if (physicalSocket.shutdown(PhysicalSocket::SHUT::RD) == 0) {
211 LOG(DEBUG) << connectionName << " Shutdown (RD): success";
212 } else {
213 PLOG(ERROR) << connectionName << " Shutdown (RD)";
214 }
215 }

References core::socket::stream::SocketConnection::connectionName.

◆ shutdownWrite()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::shutdownWrite ( bool  forceClose)
finalvirtual

Implements core::socket::stream::SocketConnection.

Definition at line 218 of file SocketConnection.hpp.

218 {
220 LOG(TRACE) << connectionName << ": Stop writing";
221
222 SocketWriter::shutdownWrite([forceClose, this]() {
225 }
226 if (forceClose && SocketReader::isEnabled()) {
227 close();
228 }
229 });
230 }
231 }
void shutdownWrite(const std::function< void()> &onShutdown)

◆ streamEof()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::streamEof ( )
finalvirtual

◆ streamToPeer()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
bool core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::streamToPeer ( core::pipe::Source source)
finalvirtual

Implements core::socket::stream::SocketConnection.

Definition at line 195 of file SocketConnection.hpp.

195 {
196 return SocketWriter::streamToPeer(source);
197 }
bool streamToPeer(core::pipe::Source *source)

◆ unobservedEvent()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::unobservedEvent ( )
finalprivate

Definition at line 333 of file SocketConnection.hpp.

333 {
334 if (socketContext != nullptr) {
336
337 LOG(DEBUG) << connectionName << ": SocketContext detached";
338 }
339
340 onDisconnect();
341
342 delete this;
343 }

References core::socket::stream::SocketConnection::connectionName, core::socket::stream::SocketContext::detach(), core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::onDisconnect, and core::socket::stream::SocketConnection::socketContext.

Here is the call graph for this function:

◆ writeTimeout()

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter , typename Config >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter, Config >::writeTimeout ( )
finalprivate

Definition at line 327 of file SocketConnection.hpp.

327 {
328 LOG(WARNING) << connectionName << ": Write timeout";
329 close();
330 }

References core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::close(), and core::socket::stream::SocketConnection::connectionName.

Here is the call graph for this function:

Member Data Documentation

◆ config

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
std::shared_ptr<Config> core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::config
private

◆ localAddress

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
SocketAddress core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::localAddress {}
private

Definition at line 225 of file SocketConnection.h.

225{};

◆ onDisconnect

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
std::function<void()> core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::onDisconnect
private

◆ physicalSocket

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
PhysicalSocket core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::physicalSocket
private

◆ remoteAddress

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT , typename ConfigT >
SocketAddress core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::remoteAddress {}
private

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