SNode.C
Loading...
Searching...
No Matches
SocketConnection.h
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef CORE_SOCKET_STREAM_SOCKETCONNECTION_H
21#define CORE_SOCKET_STREAM_SOCKETCONNECTION_H
22
23namespace core {
24 namespace pipe {
25 class Source;
26 }
27 namespace socket {
28 class SocketAddress;
29 namespace stream {
31 class SocketContext;
32 } // namespace stream
33 } // namespace socket
34} // namespace core
35
36#ifndef DOXYGEN_SHOULD_SKIP_THIS
37
38namespace utils {
39 class Timeval;
40}
41
42#include <cstddef>
43#include <cstdint>
44#include <functional>
45#include <memory>
46#include <string>
47#include <vector>
48
49#endif /* DOXYGEN_SHOULD_SKIP_THIS */
50
51namespace core::socket::stream {
52
54 public:
55 SocketConnection(const std::string& instanceName, int fd, const std::string& configuredServer);
56
57 virtual int getFd() const = 0;
58
59 protected:
60 virtual ~SocketConnection();
61
62 void setSocketContext(SocketContext* socketContext);
63
64 public:
65 void switchSocketContext(SocketContext* newSocketContext);
66
67 virtual void sendToPeer(const char* chunk, std::size_t chunkLen) = 0;
68 void sendToPeer(const std::string& data);
69 void sentToPeer(const std::vector<uint8_t>& data);
70 void sentToPeer(const std::vector<char>& data);
71
72 virtual bool streamToPeer(core::pipe::Source* source) = 0;
73 virtual void streamEof() = 0;
74
76
77 virtual void shutdownRead() = 0;
78 virtual void shutdownWrite(bool forceClose) = 0;
79
80 const std::string& getInstanceName() const;
81 const std::string& getConnectionName() const;
82
83 const std::string& getConfiguredServer() const;
84
85 virtual const core::socket::SocketAddress& getLocalAddress() const = 0;
86 virtual const core::socket::SocketAddress& getRemoteAddress() const = 0;
87
88 virtual void close() = 0;
89
90 virtual void setTimeout(const utils::Timeval& timeout) = 0;
91
92 protected:
93 void connectSocketContext(const std::shared_ptr<SocketContextFactory>& socketContextFactory);
94
96
99
102
104 };
105
106 template <typename PhysicalSocketT, typename SocketReaderT, typename SocketWriterT>
108 : public SocketConnection
109 , protected SocketReaderT
110 , protected SocketWriterT {
111 protected:
112 using Super = core::socket::stream::SocketConnection;
113
114 using PhysicalSocket = PhysicalSocketT;
115 using SocketReader = SocketReaderT;
116 using SocketWriter = SocketWriterT;
117 using SocketAddress = typename PhysicalSocket::SocketAddress;
118
119 public:
121
122 protected:
123 SocketConnectionT(const std::string& instanceName,
124 PhysicalSocket&& physicalSocket,
125 const std::function<void()>& onDisconnect,
126 const std::string& configuredServer,
127 const SocketAddress& localAddress,
128 const SocketAddress& remoteAddress,
129 const utils::Timeval& readTimeout,
130 const utils::Timeval& writeTimeout,
131 std::size_t readBlockSize,
132 std::size_t writeBlockSize,
133 const utils::Timeval& terminateTimeout);
134
135 ~SocketConnectionT() override;
136
137 public:
138 int getFd() const final;
139
140 void setTimeout(const utils::Timeval& timeout) final;
141
142 const SocketAddress& getLocalAddress() const final;
143 const SocketAddress& getRemoteAddress() const final;
144
146
147 using Super::sendToPeer;
148 void sendToPeer(const char* chunk, std::size_t chunkLen) final;
149
150 bool streamToPeer(core::pipe::Source* source) final;
151 void streamEof() final;
152
153 void shutdownRead() final;
154 void shutdownWrite(bool forceClose) final;
155
156 void close() final;
157
158 protected:
159 void doWriteShutdown(const std::function<void()>& onShutdown) override;
160
161 void onWriteError(int errnum);
162 void onReadError(int errnum);
163
164 private:
165 void onReceivedFromPeer(std::size_t available) final;
166
167 bool onSignal(int signum) final;
168
169 void readTimeout() final;
170 void writeTimeout() final;
171 void unobservedEvent() final;
172
173 PhysicalSocket physicalSocket;
174
176
177 SocketAddress localAddress{};
178 SocketAddress remoteAddress{};
179 };
180
181} // namespace core::socket::stream
182
183#endif // CORE_SOCKET_STREAM_SOCKETCONNECTION_H
void onReceivedFromPeer(std::size_t available) final
void setTimeout(const utils::Timeval &timeout) final
std::size_t readFromPeer(char *chunk, std::size_t chunkLen) final
const SocketAddress & getRemoteAddress() const final
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)
void doWriteShutdown(const std::function< void()> &onShutdown) override
bool streamToPeer(core::pipe::Source *source) final
void shutdownWrite(bool forceClose) final
void sendToPeer(const char *chunk, std::size_t chunkLen) final
const SocketAddress & getLocalAddress() const final
const std::string & getConfiguredServer() const
virtual void sendToPeer(const char *chunk, std::size_t chunkLen)=0
core::socket::stream::SocketContext * newSocketContext
core::socket::stream::SocketContext * socketContext
void connectSocketContext(const std::shared_ptr< SocketContextFactory > &socketContextFactory)
void switchSocketContext(SocketContext *newSocketContext)
virtual const core::socket::SocketAddress & getLocalAddress() const =0
virtual void setTimeout(const utils::Timeval &timeout)=0
void sentToPeer(const std::vector< uint8_t > &data)
const std::string & getConnectionName() const
void setSocketContext(SocketContext *socketContext)
virtual const core::socket::SocketAddress & getRemoteAddress() const =0
void sendToPeer(const std::string &data)
virtual void shutdownWrite(bool forceClose)=0
virtual std::size_t readFromPeer(char *chunk, std::size_t chunkLen)=0
SocketConnection(const std::string &instanceName, int fd, const std::string &configuredServer)
virtual bool streamToPeer(core::pipe::Source *source)=0
const std::string & getInstanceName() const
SocketConnection(const std::string &instanceName, PhysicalSocket &&physicalSocket, const std::function< void(SocketConnection *)> &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)
int main(int argc, char *argv[])
Definition Config.h:37