SNode.C
Loading...
Searching...
No Matches
SocketWriter.cpp
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#include "core/socket/stream/SocketWriter.h"
43
44#include "core/pipe/Source.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include "core/system/socket.h"
49#include "log/Logger.h"
50
51#include <cerrno>
52
53#endif // DOXYGEN_SHOULD_SKIP_THIS
54
55namespace core::socket::stream {
56 SocketWriter::SocketWriter(const std::string& instanceName,
57 const std::function<void(int)>& onStatus,
58 const utils::Timeval& timeout,
59 std::size_t blockSize,
60 const utils::Timeval& terminateTimeout)
61 : core::eventreceiver::WriteEventReceiver(instanceName, timeout)
62 , onStatus(onStatus)
63 , blockSize(blockSize)
64 , terminateTimeout(terminateTimeout) {
65 }
66
67 std::size_t SocketWriter::getTotalSent() const {
68 return totalSent;
69 }
70
71 std::size_t SocketWriter::getTotalQueued() const {
72 return totalQueued;
73 }
74
77 }
78
79 void SocketWriter::signalEvent(int sigNum) {
80 if (onSignal(sigNum)) {
81 shutdownWrite([this]() {
83 });
84 }
85 }
86
87 ssize_t SocketWriter::write(const char* chunk, std::size_t chunkLen) {
88 return core::system::send(this->getRegisteredFd(), chunk, chunkLen, MSG_NOSIGNAL);
89 }
90
92 if (!writePuffer.empty()) {
93 const std::size_t writeLen = (writePuffer.size() < blockSize) ? writePuffer.size() : blockSize;
94 const ssize_t retWrite = write(writePuffer.data(), writeLen);
95
96 if (retWrite > 0) {
97 totalSent += static_cast<std::size_t>(retWrite);
98 writePuffer.erase(writePuffer.begin(), writePuffer.begin() + retWrite);
99
100 if (writePuffer.capacity() > writePuffer.size() * 2) {
101 writePuffer.shrink_to_fit();
102 }
103
104 if (!isSuspended()) {
105 suspend();
106 }
107 span();
108 } else if ((errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) && isSuspended()) {
109 resume();
110 } else {
111 onStatus(retWrite == 0 ? 0 : errno);
112 }
113 } else {
114 if (!isSuspended()) {
115 suspend();
116 }
117
118 if (markShutdown) {
119 LOG(TRACE) << getName() << ": Shutdown restart";
121 } else if (source != nullptr) {
123 }
124 }
125 }
126
127 void SocketWriter::setBlockSize(std::size_t writeBlockSize) {
128 blockSize = writeBlockSize;
129 }
130
131 void SocketWriter::sendToPeer(const char* chunk, std::size_t chunkLen) {
133 if (isEnabled()) {
134 if (writePuffer.empty()) {
135 resume();
136 }
137
138 writePuffer.insert(writePuffer.end(), chunk, chunk + chunkLen);
139 totalQueued += chunkLen;
140
141 if (source != nullptr && writePuffer.size() > 5 * blockSize) {
143 }
144 } else {
145 LOG(WARNING) << getName() << ": Send while not enabled";
146 }
147 } else {
148 LOG(WARNING) << getName() << ": Send while shutdown in progress";
149 }
150 }
151
152 bool SocketWriter::streamToPeer(core::pipe::Source* source) {
153 bool success = false;
154
156 if (isEnabled()) {
157 success = source != nullptr;
158
159 if (success) {
160 LOG(TRACE) << getName() << ": Stream started";
161 } else {
162 LOG(WARNING) << getName() << ": Stream source is nullptr";
163 }
164 } else {
165 LOG(WARNING) << getName() << ": Stream while not enabled";
166 }
167 } else {
168 LOG(WARNING) << getName() << ": Stream while shutdown in progress";
169 }
170
171 this->source = source;
172
173 return success;
174 }
175
177 LOG(TRACE) << getName() << ": Stream EOF";
178 this->source = nullptr;
179 }
180
181 void SocketWriter::shutdownWrite(const std::function<void()>& onShutdown) {
182 if (!shutdownInProgress) {
183 shutdownInProgress = true;
184
185 SocketWriter::onShutdown = onShutdown;
186 if (writePuffer.empty()) {
187 LOG(TRACE) << getName() << ": Shutdown start";
188 doWriteShutdown(onShutdown);
189 } else {
190 markShutdown = true;
191 LOG(TRACE) << getName() << ": Shutdown delayed due to queued data";
192 }
193 }
194 }
195
196} // namespace core::socket::stream
const std::string & getName() const
WriteEventReceiver(const std::string &name, const utils::Timeval &timeout)
virtual void suspend()=0
virtual void resume()=0
virtual void doWriteShutdown(const std::function< void()> &onShutdown)=0
std::function< void(int)> onStatus
void setBlockSize(std::size_t writeBlockSize)
void sendToPeer(const char *chunk, std::size_t chunkLen)
void signalEvent(int sigNum) final
std::function< void()> onShutdown
virtual bool onSignal(int sigNum)=0
void shutdownWrite(const std::function< void()> &onShutdown)
bool streamToPeer(core::pipe::Source *source)
virtual ssize_t write(const char *chunk, std::size_t chunkLen)
SocketWriter(const std::string &instanceName, const std::function< void(int)> &onStatus, const utils::Timeval &timeout, std::size_t blockSize, const utils::Timeval &terminateTimeout)
Timeval(const Timeval &timeVal) noexcept=default
ssize_t send(int sockfd, const void *buf, std::size_t len, int flags)
Definition socket.cpp:96