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 107 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 114 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 117 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 115 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 116 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 112 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 37 of file SocketConnection.hpp.

50 instanceName + " [" + std::to_string(physicalSocket.getFd()) + "]",
51 [this](int errnum) {
52 {
53 const utils::PreserveErrno pe(errnum);
54 if (errno == 0) {
55 LOG(TRACE) << connectionName << " OnReadError: EOF received";
56 } else {
57 PLOG(TRACE) << connectionName << " OnReadError";
58 }
59 }
61
62 onReadError(errnum);
63 },
65 readBlockSize,
66 terminateTimeout)
68 instanceName + " [" + std::to_string(physicalSocket.getFd()) + "]",
69 [this](int errnum) {
70 {
71 const utils::PreserveErrno pe(errnum);
72 PLOG(TRACE) << connectionName << " OnWriteError";
73 }
75
76 onWriteError(errnum);
77 },
79 writeBlockSize,
80 terminateTimeout)
81 , physicalSocket(std::move(physicalSocket))
85 if (!SocketReader::enable(this->physicalSocket.getFd())) {
86 delete this;
87 } else if (!SocketWriter::enable(this->physicalSocket.getFd())) {
88 delete this;
89 } else {
91 }
92 }
SocketConnection(const std::string &instanceName, int fd, const std::string &configuredServer)

◆ ~SocketConnectionT()

Definition at line 95 of file SocketConnection.hpp.

95 {
96 }

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

189 {
190 errno = 0;
191
193
194 LOG(TRACE) << connectionName << ": Shutdown (WR)";
195
196 if (physicalSocket.shutdown(PhysicalSocket::SHUT::WR) == 0) {
197 LOG(DEBUG) << connectionName << " Shutdown (WR): success";
198 } else {
199 PLOG(ERROR) << connectionName << " Shutdown (WR)";
200 }
201
202 onShutdown();
203 }
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 229 of file SocketConnection.hpp.

229 {
230 socketContext->onReadError(errnum);
231 }
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 206 of file SocketConnection.hpp.

206 {
207 std::size_t consumed = socketContext->onReceivedFromPeer();
208
209 if (available != 0 && consumed == 0) {
210 LOG(TRACE) << connectionName << ": Data available: " << available << " but nothing read";
211
212 close();
213
214 delete newSocketContext; // delete of nullptr is valid since C++14!
215 newSocketContext = nullptr;
216 } else if (newSocketContext != nullptr) { // Perform a pending SocketContextSwitch
219 newSocketContext = nullptr;
220 }
221 }
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 234 of file SocketConnection.hpp.

234 {
235 switch (signum) {
236 case SIGINT:
237 [[fallthrough]];
238 case SIGTERM:
239 [[fallthrough]];
240 case SIGABRT:
241 [[fallthrough]];
242 case SIGHUP:
243 LOG(DEBUG) << connectionName << ": Shutting down due to signal '" << strsignal(signum) << "' (SIG"
244 << utils::system::sigabbrev_np(signum) << " [" << signum << "])";
245 break;
246 case SIGALRM:
247 break;
248 }
249
250 return socketContext != nullptr ? socketContext->onSignal(signum) : true;
251 }
virtual bool onSignal(int sig)=0
std::string sigabbrev_np(int sig)
Definition signal.cpp:37

◆ onWriteError()

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

Definition at line 224 of file SocketConnection.hpp.

224 {
226 }
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 122 of file SocketConnection.hpp.

122 {
123 std::size_t ret = 0;
124
125 if (newSocketContext == nullptr) {
126 ret = SocketReader::readFromPeer(chunk, chunkLen);
127 } else {
128 LOG(TRACE) << connectionName << " ReadFromPeer: New SocketContext != nullptr: SocketContextSwitch still in progress";
129 }
130
131 return ret;
132 }
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 254 of file SocketConnection.hpp.

254 {
255 LOG(WARNING) << connectionName << ": Read timeout";
256 close();
257 }

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

135 {
136 SocketWriter::sendToPeer(chunk, chunkLen);
137 }
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 68 of file SocketConnection.cpp.

56 {
57 sendToPeer(data.data(), data.size());
58 }
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 99 of file SocketConnection.hpp.

99 {
102 }
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 150 of file SocketConnection.hpp.

150 {
151 LOG(TRACE) << connectionName << ": Shutdown (RD)";
152
154
155 if (physicalSocket.shutdown(PhysicalSocket::SHUT::RD) == 0) {
156 LOG(DEBUG) << connectionName << " Shutdown (RD): success";
157 } else {
158 PLOG(ERROR) << connectionName << " Shutdown (RD)";
159 }
160 }

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

163 {
165 LOG(TRACE) << connectionName << ": Stop writing";
166
167 SocketWriter::shutdownWrite([forceClose, this]() {
170 }
171 if (forceClose && SocketReader::isEnabled()) {
172 close();
173 }
174 });
175 }
176 }
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 140 of file SocketConnection.hpp.

140 {
141 return SocketWriter::streamToPeer(source);
142 }
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 266 of file SocketConnection.hpp.

266 {
268
269 onDisconnect();
270
271 delete this;
272 }

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

260 {
261 LOG(WARNING) << connectionName << ": Write timeout";
262 close();
263 }

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