SNode.C
Loading...
Searching...
No Matches
PipeSource.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#include "core/pipe/PipeSource.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "core/system/unistd.h"
25#include "utils/Timeval.h"
26
27#include <cerrno>
28#include <string>
29
30#endif /* DOXYGEN_SHOULD_SKIP_THIS */
31
32#ifndef MAX_SEND_JUNKSIZE
33#define MAX_SEND_JUNKSIZE 16384
34#endif
35
36namespace core::pipe {
37
39 : core::eventreceiver::WriteEventReceiver("PipeSource fd = " + std::to_string(fd), 60) {
41 delete this;
42 } else {
44 }
45 }
46
48 close(getRegisteredFd());
49 }
50
51 void PipeSource::setOnError(const std::function<void(int)>& onError) {
52 this->onError = onError;
53 }
54
55 void PipeSource::send(const char* chunk, std::size_t chunkLen) {
56 writeBuffer.insert(writeBuffer.end(), chunk, chunk + chunkLen);
57
60 }
61 }
62
63 void PipeSource::send(const std::string& data) {
64 send(data.data(), data.size());
65 }
66
70
72 const ssize_t ret = core::system::write(
73 getRegisteredFd(), writeBuffer.data(), (writeBuffer.size() < MAX_SEND_JUNKSIZE) ? writeBuffer.size() : MAX_SEND_JUNKSIZE);
74
75 if (ret > 0) {
76 writeBuffer.erase(writeBuffer.begin(), writeBuffer.begin() + ret);
77
78 if (writeBuffer.empty()) {
80 }
81 } else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
83
84 if (onError) {
85 onError(errno);
86 }
87 }
88 }
89
91 delete this;
92 }
93
94} // namespace core::pipe
#define MAX_SEND_JUNKSIZE
void send(const char *chunk, std::size_t chunkLen)
void send(const std::string &data)
void setOnError(const std::function< void(int)> &onError)
void writeEvent() override
void unobservedEvent() override