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

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
 

Detailed Description

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

Definition at line 129 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 136 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 139 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 137 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 138 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 134 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)

◆ ~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 211 of file SocketConnection.hpp.

211 {
212 errno = 0;
213
215
216 LOG(TRACE) << connectionName << ": Shutdown (WR)";
217
218 if (physicalSocket.shutdown(PhysicalSocket::SHUT::WR) == 0) {
219 LOG(DEBUG) << connectionName << " Shutdown (WR): success";
220 } else {
221 PLOG(ERROR) << connectionName << " Shutdown (WR)";
222 }
223
224 onShutdown();
225 }
void setTimeout(const utils::Timeval &timeout) final

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

◆ getRemoteAddress()

◆ onReadError()

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

Definition at line 251 of file SocketConnection.hpp.

251 {
252 socketContext->onReadError(errnum);
253 }
core::socket::stream::SocketContext * socketContext
void onReadError(int errnum) override

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

228 {
229 std::size_t consumed = socketContext->onReceivedFromPeer();
230
231 if (available != 0 && consumed == 0) {
232 LOG(TRACE) << connectionName << ": Data available: " << available << " but nothing read";
233
234 close();
235
236 delete newSocketContext; // delete of nullptr is valid since C++14!
237 newSocketContext = nullptr;
238 } else if (newSocketContext != nullptr) { // Perform a pending SocketContextSwitch
241 newSocketContext = nullptr;
242 }
243 }
virtual std::size_t onReceivedFromPeer()=0
core::socket::stream::SocketContext * newSocketContext
void setSocketContext(SocketContext *socketContext)

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

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

256 {
257 switch (signum) {
258 case SIGINT:
259 [[fallthrough]];
260 case SIGTERM:
261 [[fallthrough]];
262 case SIGABRT:
263 [[fallthrough]];
264 case SIGHUP:
265 LOG(DEBUG) << connectionName << ": Shutting down due to signal '" << strsignal(signum) << "' (SIG"
266 << utils::system::sigabbrev_np(signum) << " [" << signum << "])";
267 break;
268 case SIGALRM:
269 break;
270 }
271
272 return socketContext != nullptr ? socketContext->onSignal(signum) : true;
273 }
virtual bool onSignal(int sig)=0
std::string sigabbrev_np(int sig)
Definition signal.cpp:59

◆ onWriteError()

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

Definition at line 246 of file SocketConnection.hpp.

246 {
248 }
void onWriteError(int errnum) override

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

◆ readTimeout()

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

Definition at line 276 of file SocketConnection.hpp.

276 {
277 LOG(WARNING) << connectionName << ": Read timeout";
278 close();
279 }

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

Here is the call graph for this function:

◆ sendToPeer() [1/2]

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/2]

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

Definition at line 90 of file SocketConnection.cpp.

78 {
79 sendToPeer(data.data(), data.size());
80 }
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::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().

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

288 {
290
291 onDisconnect();
292
293 delete this;
294 }

References core::socket::stream::SocketConnection::disconnectCurrentSocketContext().

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

282 {
283 LOG(WARNING) << connectionName << ": Write timeout";
284 close();
285 }

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

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

Definition at line 197 of file SocketConnection.h.

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