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/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef CORE_SOCKET_STREAM_SOCKETCONNECTION_H
43#define CORE_SOCKET_STREAM_SOCKETCONNECTION_H
44
45namespace core {
46 namespace pipe {
47 class Source;
48 }
49 namespace socket {
50 class SocketAddress;
51 namespace stream {
53 class SocketContext;
54 } // namespace stream
55 } // namespace socket
56} // namespace core
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59
60namespace utils {
61 class Timeval;
62}
63
64#include <cstddef>
65#include <cstdint>
66#include <functional>
67#include <memory>
68#include <string>
69#include <vector>
70
71#endif /* DOXYGEN_SHOULD_SKIP_THIS */
72
73namespace core::socket::stream {
74
76 public:
77 SocketConnection(const std::string& instanceName, int fd, const std::string& configuredServer);
78
79 virtual int getFd() const = 0;
80
81 protected:
82 virtual ~SocketConnection();
83
84 void setSocketContext(SocketContext* socketContext);
85
86 public:
87 void switchSocketContext(SocketContext* newSocketContext);
88
89 virtual void sendToPeer(const char* chunk, std::size_t chunkLen) = 0;
90 void sendToPeer(const std::string& data);
91 void sentToPeer(const std::vector<uint8_t>& data);
92 void sentToPeer(const std::vector<char>& data);
93
94 virtual bool streamToPeer(core::pipe::Source* source) = 0;
95 virtual void streamEof() = 0;
96
98
99 virtual void shutdownRead() = 0;
100 virtual void shutdownWrite(bool forceClose) = 0;
101
102 const std::string& getInstanceName() const;
103 const std::string& getConnectionName() const;
104
105 const std::string& getConfiguredServer() const;
106
107 virtual const core::socket::SocketAddress& getLocalAddress() const = 0;
108 virtual const core::socket::SocketAddress& getRemoteAddress() const = 0;
109
110 virtual void close() = 0;
111
112 virtual void setTimeout(const utils::Timeval& timeout) = 0;
113
114 protected:
115 void connectSocketContext(const std::shared_ptr<SocketContextFactory>& socketContextFactory);
116
118
121
124
126 };
127
128 template <typename PhysicalSocketT, typename SocketReaderT, typename SocketWriterT>
130 : public SocketConnection
131 , protected SocketReaderT
132 , protected SocketWriterT {
133 protected:
134 using Super = core::socket::stream::SocketConnection;
135
136 using PhysicalSocket = PhysicalSocketT;
137 using SocketReader = SocketReaderT;
138 using SocketWriter = SocketWriterT;
139 using SocketAddress = typename PhysicalSocket::SocketAddress;
140
141 public:
143
144 protected:
145 SocketConnectionT(const std::string& instanceName,
146 PhysicalSocket&& physicalSocket,
147 const std::function<void()>& onDisconnect,
148 const std::string& configuredServer,
149 const SocketAddress& localAddress,
150 const SocketAddress& remoteAddress,
151 const utils::Timeval& readTimeout,
152 const utils::Timeval& writeTimeout,
153 std::size_t readBlockSize,
154 std::size_t writeBlockSize,
155 const utils::Timeval& terminateTimeout);
156
157 ~SocketConnectionT() override;
158
159 public:
160 int getFd() const final;
161
162 void setTimeout(const utils::Timeval& timeout) final;
163
164 const SocketAddress& getLocalAddress() const final;
165 const SocketAddress& getRemoteAddress() const final;
166
168
169 using Super::sendToPeer;
170 void sendToPeer(const char* chunk, std::size_t chunkLen) final;
171
172 bool streamToPeer(core::pipe::Source* source) final;
173 void streamEof() final;
174
175 void shutdownRead() final;
176 void shutdownWrite(bool forceClose) final;
177
178 void close() final;
179
180 protected:
181 void doWriteShutdown(const std::function<void()>& onShutdown) override;
182
183 void onWriteError(int errnum);
184 void onReadError(int errnum);
185
186 private:
187 void onReceivedFromPeer(std::size_t available) final;
188
189 bool onSignal(int signum) final;
190
191 void readTimeout() final;
192 void writeTimeout() final;
193 void unobservedEvent() final;
194
195 PhysicalSocket physicalSocket;
196
198
199 SocketAddress localAddress{};
200 SocketAddress remoteAddress{};
201 };
202
203} // namespace core::socket::stream
204
205#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:59