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

#include <SocketConnection.h>

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

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
 
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 (const std::string &instanceName, int fd, const std::string &configuredServer)
 
 SocketConnection (const SocketConnection &)=delete
 
void switchSocketContext (SocketContext *newSocketContext)
 
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
 
const std::string & getConfiguredServer () const
 
SocketContextgetSocketContext () const
 
std::string getOnlineSince () const
 
std::string getOnlineDuration () const
 

Protected Types

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

Protected Member Functions

 SocketConnectionT (const std::string &instanceName, PhysicalSocket &&physicalSocket, const std::function< void()> &onDisconnect, const std::string &configuredServer, const SocketAddress &localAddress, const SocketAddress &remoteAddress, const utils::Timeval &readTimeout, const utils::Timeval &writeTimeout, std::size_t readBlockSize, std::size_t writeBlockSize, const utils::Timeval &terminateTimeout)
 
 ~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)
 
void disconnectCurrentSocketContext ()
 

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 {}
 

Additional Inherited Members

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

Detailed Description

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

Definition at line 152 of file SocketConnection.h.

Member Typedef Documentation

◆ PhysicalSocket

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

Definition at line 159 of file SocketConnection.h.

◆ SocketAddress

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

Definition at line 162 of file SocketConnection.h.

◆ SocketReader

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

Definition at line 160 of file SocketConnection.h.

◆ SocketWriter

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

Definition at line 161 of file SocketConnection.h.

◆ Super

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

Definition at line 157 of file SocketConnection.h.

Constructor & Destructor Documentation

◆ SocketConnectionT() [1/2]

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

◆ SocketConnectionT() [2/2]

template<typename PhysicalSocket , typename SocketReader , typename SocketWriter >
core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter >::SocketConnectionT ( const std::string &  instanceName,
PhysicalSocket &&  physicalSocket,
const std::function< void()> &  onDisconnect,
const std::string &  configuredServer,
const SocketAddress localAddress,
const SocketAddress remoteAddress,
const utils::Timeval readTimeout,
const utils::Timeval writeTimeout,
std::size_t  readBlockSize,
std::size_t  writeBlockSize,
const utils::Timeval terminateTimeout 
)
protected

Definition at line 59 of file SocketConnection.hpp.

72 instanceName + " [" + std::to_string(physicalSocket.getFd()) + "]",
73 [this](int errnum) {
74 {
75 const utils::PreserveErrno pe(errnum);
76 if (errno == 0) {
77 LOG(TRACE) << connectionName << " OnReadError: EOF received";
78 } else {
79 PLOG(TRACE) << connectionName << " OnReadError";
80 }
81 }
83
84 onReadError(errnum);
85 },
87 readBlockSize,
88 terminateTimeout)
90 instanceName + " [" + std::to_string(physicalSocket.getFd()) + "]",
91 [this](int errnum) {
92 {
93 const utils::PreserveErrno pe(errnum);
94 PLOG(TRACE) << connectionName << " OnWriteError";
95 }
97
98 onWriteError(errnum);
99 },
101 writeBlockSize,
102 terminateTimeout)
103 , physicalSocket(std::move(physicalSocket))
107 if (!SocketReader::enable(this->physicalSocket.getFd())) {
108 delete this;
109 } else if (!SocketWriter::enable(this->physicalSocket.getFd())) {
110 delete this;
111 } else {
113 }
114 }
SocketConnection(const std::string &instanceName, int fd, const std::string &configuredServer)

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

Here is the call graph for this function:

◆ ~SocketConnectionT()

Definition at line 117 of file SocketConnection.hpp.

117 {
118 }

Member Function Documentation

◆ close()

◆ doWriteShutdown()

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

Definition at line 231 of file SocketConnection.hpp.

231 {
232 errno = 0;
233
235
236 LOG(TRACE) << connectionName << ": Shutdown (WR)";
237
238 if (physicalSocket.shutdown(PhysicalSocket::SHUT::WR) == 0) {
239 LOG(DEBUG) << connectionName << " Shutdown (WR): success";
240 } else {
241 PLOG(ERROR) << connectionName << " Shutdown (WR)";
242 }
243
244 onShutdown();
245 }
void setTimeout(const utils::Timeval &timeout) final

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

Here is the call graph for this function:

◆ getFd()

◆ getLocalAddress()

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

Definition at line 133 of file SocketConnection.hpp.

133 {
134 return localAddress;
135 }

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

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 139 of file SocketConnection.hpp.

139 {
140 return remoteAddress;
141 }

References core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::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 >
std::size_t core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter >::getTotalProcessed ( ) const
overridevirtual

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

Definition at line 226 of file SocketConnection.hpp.

226 {
228 }
std::size_t getTotalProcessed() const

◆ getTotalQueued()

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

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

Definition at line 216 of file SocketConnection.hpp.

216 {
218 }

◆ getTotalRead()

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

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

Definition at line 221 of file SocketConnection.hpp.

221 {
223 }

◆ getTotalSent()

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

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

Definition at line 211 of file SocketConnection.hpp.

211 {
213 }

◆ onReadError()

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

Definition at line 271 of file SocketConnection.hpp.

271 {
272 socketContext->onReadError(errnum);
273 }
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 >::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 >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter >::onReceivedFromPeer ( std::size_t  available)
finalprivate

Definition at line 248 of file SocketConnection.hpp.

248 {
249 std::size_t consumed = socketContext->onReceivedFromPeer();
250
251 if (available != 0 && consumed == 0) {
252 LOG(TRACE) << connectionName << ": Data available: " << available << " but nothing read";
253
254 close();
255
256 delete newSocketContext; // delete of nullptr is valid since C++14!
257 newSocketContext = nullptr;
258 } else if (newSocketContext != nullptr) { // Perform a pending SocketContextSwitch
261 newSocketContext = nullptr;
262 }
263 }
virtual std::size_t onReceivedFromPeer()=0
core::socket::stream::SocketContext * newSocketContext
void setSocketContext(SocketContext *socketContext)

References core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::close(), core::socket::stream::SocketConnection::connectionName, core::socket::stream::SocketConnection::disconnectCurrentSocketContext(), core::socket::stream::SocketConnection::newSocketContext, core::socket::SocketContext::onReceivedFromPeer(), core::socket::stream::SocketConnection::setSocketContext(), and core::socket::stream::SocketConnection::socketContext.

Here is the call graph for this function:

◆ onSignal()

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

Definition at line 276 of file SocketConnection.hpp.

276 {
277 switch (signum) {
278 case SIGINT:
279 [[fallthrough]];
280 case SIGTERM:
281 [[fallthrough]];
282 case SIGABRT:
283 [[fallthrough]];
284 case SIGHUP:
285 LOG(DEBUG) << connectionName << ": Shutting down due to signal '" << strsignal(signum) << "' (SIG"
286 << utils::system::sigabbrev_np(signum) << " [" << signum << "])";
287 break;
288 case SIGALRM:
289 break;
290 }
291
292 return socketContext != nullptr ? socketContext->onSignal(signum) : true;
293 }
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 >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter >::onWriteError ( int  errnum)
protected

Definition at line 266 of file SocketConnection.hpp.

266 {
268 }
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 >
std::size_t core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter >::readFromPeer ( char *  chunk,
std::size_t  chunkLen 
)
finalvirtual

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

Definition at line 144 of file SocketConnection.hpp.

144 {
145 std::size_t ret = 0;
146
147 if (newSocketContext == nullptr) {
148 ret = SocketReader::readFromPeer(chunk, chunkLen);
149 } else {
150 LOG(TRACE) << connectionName << " ReadFromPeer: New SocketContext != nullptr: SocketContextSwitch still in progress";
151 }
152
153 return ret;
154 }
std::size_t readFromPeer(char *chunk, std::size_t chunkLen)

References core::socket::stream::SocketConnection::connectionName, and core::socket::stream::SocketConnection::newSocketContext.

◆ readTimeout()

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

Definition at line 296 of file SocketConnection.hpp.

296 {
297 LOG(WARNING) << connectionName << ": Read timeout";
298 close();
299 }

References core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::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 >
void core::socket::stream::SocketConnectionT< PhysicalSocket, SocketReader, SocketWriter >::sendToPeer ( const char *  chunk,
std::size_t  chunkLen 
)
finalvirtual

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

Definition at line 157 of file SocketConnection.hpp.

157 {
158 SocketWriter::sendToPeer(chunk, chunkLen);
159 }
void sendToPeer(const char *chunk, std::size_t chunkLen)

◆ sendToPeer() [2/3]

template<typename PhysicalSocketT , typename SocketReaderT , typename SocketWriterT >
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 >
void core::socket::stream::SocketConnection::sendToPeer ( const std::string &  data)

Definition at line 94 of file SocketConnection.cpp.

87 {
88 sendToPeer(data.data(), data.size());
89 }
void sendToPeer(const char *chunk, std::size_t chunkLen) final

◆ setTimeout()

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

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

Definition at line 121 of file SocketConnection.hpp.

121 {
124 }
void setTimeout(const utils::Timeval &timeout)

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

Here is the caller graph for this function:

◆ shutdownRead()

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

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

Definition at line 172 of file SocketConnection.hpp.

172 {
173 LOG(TRACE) << connectionName << ": Shutdown (RD)";
174
176
177 if (physicalSocket.shutdown(PhysicalSocket::SHUT::RD) == 0) {
178 LOG(DEBUG) << connectionName << " Shutdown (RD): success";
179 } else {
180 PLOG(ERROR) << connectionName << " Shutdown (RD)";
181 }
182 }

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

◆ shutdownWrite()

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

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

Definition at line 185 of file SocketConnection.hpp.

185 {
187 LOG(TRACE) << connectionName << ": Stop writing";
188
189 SocketWriter::shutdownWrite([forceClose, this]() {
192 }
193 if (forceClose && SocketReader::isEnabled()) {
194 close();
195 }
196 });
197 }
198 }
void shutdownWrite(const std::function< void()> &onShutdown)

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

Here is the call graph for this function:

◆ streamEof()

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

◆ streamToPeer()

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

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

Definition at line 162 of file SocketConnection.hpp.

162 {
163 return SocketWriter::streamToPeer(source);
164 }
bool streamToPeer(core::pipe::Source *source)

◆ unobservedEvent()

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

Definition at line 308 of file SocketConnection.hpp.

308 {
310
311 onDisconnect();
312
313 delete this;
314 }

References core::socket::stream::SocketConnection::disconnectCurrentSocketContext(), and core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::onDisconnect.

Here is the call graph for this function:

◆ writeTimeout()

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

Definition at line 302 of file SocketConnection.hpp.

302 {
303 LOG(WARNING) << connectionName << ": Write timeout";
304 close();
305 }

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

Here is the call graph for this function:

Member Data Documentation

◆ localAddress

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

◆ onDisconnect

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

◆ physicalSocket

◆ remoteAddress

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

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