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
57 SocketWriter::SocketWriter(const std::string& instanceName,
58 const std::function<void(int)>& onStatus,
59 const utils::Timeval& timeout,
60 std::size_t blockSize,
61 const utils::Timeval& terminateTimeout)
62 : core::eventreceiver::WriteEventReceiver(instanceName, timeout)
63 , onStatus(onStatus)
64 , blockSize(blockSize)
65 , terminateTimeout(terminateTimeout) {
66 }
67
70 }
71
72 void SocketWriter::signalEvent(int sigNum) {
73 if (onSignal(sigNum)) {
74 shutdownWrite([this]() {
76 });
77 }
78 }
79
80 ssize_t SocketWriter::write(const char* chunk, std::size_t chunkLen) {
81 return core::system::send(this->getRegisteredFd(), chunk, chunkLen, MSG_NOSIGNAL);
82 }
83
85 if (!writePuffer.empty()) {
86 const std::size_t writeLen = (writePuffer.size() < blockSize) ? writePuffer.size() : blockSize;
87 const ssize_t retWrite = write(writePuffer.data(), writeLen);
88
89 if (retWrite > 0) {
90 writePuffer.erase(writePuffer.begin(), writePuffer.begin() + retWrite);
91
92 if (writePuffer.capacity() > writePuffer.size() * 2) {
93 writePuffer.shrink_to_fit();
94 }
95
96 if (!isSuspended()) {
98 }
99 span();
100 } else if ((errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) && isSuspended()) {
101 resume();
102 } else {
103 onStatus(retWrite == 0 ? 0 : errno);
104 }
105 } else {
106 if (!isSuspended()) {
107 suspend();
108 }
109
110 if (markShutdown) {
111 LOG(TRACE) << getName() << ": Shutdown restart";
113 } else if (source != nullptr) {
115 }
116 }
117 }
118
119 void SocketWriter::setBlockSize(std::size_t writeBlockSize) {
120 blockSize = writeBlockSize;
121 }
122
123 void SocketWriter::sendToPeer(const char* chunk, std::size_t chunkLen) {
125 if (isEnabled()) {
126 if (writePuffer.empty()) {
127 resume();
128 }
129
130 writePuffer.insert(writePuffer.end(), chunk, chunk + chunkLen);
131
132 if (source != nullptr && writePuffer.size() > 5 * blockSize) {
134 }
135 } else {
136 LOG(WARNING) << getName() << ": Send while not enabled";
137 }
138 } else {
139 LOG(WARNING) << getName() << ": Send while shutdown in progress";
140 }
141 }
142
143 bool SocketWriter::streamToPeer(core::pipe::Source* source) {
144 bool success = false;
145
147 if (isEnabled()) {
148 success = source != nullptr;
149
150 if (success) {
151 LOG(TRACE) << getName() << ": Stream started";
152 } else {
153 LOG(WARNING) << getName() << ": Stream source is nullptr";
154 }
155 } else {
156 LOG(WARNING) << getName() << ": Stream while not enabled";
157 }
158 } else {
159 LOG(WARNING) << getName() << ": Stream while shutdown in progress";
160 }
161
162 this->source = source;
163
164 return success;
165 }
166
168 LOG(TRACE) << getName() << ": Stream EOF";
169 this->source = nullptr;
170 }
171
172 void SocketWriter::shutdownWrite(const std::function<void()>& onShutdown) {
173 if (!shutdownInProgress) {
174 shutdownInProgress = true;
175
176 SocketWriter::onShutdown = onShutdown;
177 if (writePuffer.empty()) {
178 LOG(TRACE) << getName() << ": Shutdown start";
179 doWriteShutdown(onShutdown);
180 } else {
181 markShutdown = true;
182 LOG(TRACE) << getName() << ": Shutdown delayed due to queued data";
183 }
184 }
185 }
186
187} // 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