SNode.C
Loading...
Searching...
No Matches
PipeSink.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/PipeSink.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_READ_JUNKSIZE
33#define MAX_READ_JUNKSIZE 16384
34#endif
35
36namespace core::pipe {
37
39 : core::eventreceiver::ReadEventReceiver("PipeSink fd = " + std::to_string(fd), 60) {
41 delete this;
42 }
43 }
44
46 close(getRegisteredFd());
47 }
48
50 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, hicpp-avoid-c-arrays, modernize-avoid-c-arrays)
51 static char chunk[MAX_READ_JUNKSIZE];
52
53 const ssize_t ret = core::system::read(getRegisteredFd(), chunk, MAX_READ_JUNKSIZE);
54
55 if (ret > 0) {
56 if (onData) {
57 onData(chunk, static_cast<std::size_t>(ret));
58 }
59 } else {
61
62 if (ret == 0) {
63 if (onEof) {
64 onEof();
65 }
66 } else {
67 if (onError) {
68 onError(errno);
69 }
70 }
71 }
72 }
73
74 void PipeSink::setOnData(const std::function<void(const char*, std::size_t)>& onData) {
75 this->onData = onData;
76 }
77
78 void PipeSink::setOnEof(const std::function<void()>& onEof) {
79 this->onEof = onEof;
80 }
81
82 void PipeSink::setOnError(const std::function<void(int)>& onError) {
83 this->onError = onError;
84 }
85
87 delete this;
88 }
89
90} // namespace core::pipe
#define MAX_READ_JUNKSIZE
Definition PipeSink.cpp:33
~PipeSink() override
Definition PipeSink.cpp:45
void readEvent() override
Definition PipeSink.cpp:49
void setOnError(const std::function< void(int)> &onError)
Definition PipeSink.cpp:82
void setOnEof(const std::function< void()> &onEof)
Definition PipeSink.cpp:78
void setOnData(const std::function< void(const char *, std::size_t)> &onData)
Definition PipeSink.cpp:74
void unobservedEvent() override
Definition PipeSink.cpp:86
ssize_t read(int fd, void *buf, std::size_t count)
Definition unistd.cpp:35