SNode.C
Loading...
Searching...
No Matches
Source.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/Source.h"
21
22#include "core/pipe/Sink.h"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include <cerrno>
27
28#endif /* DOXYGEN_SHOULD_SKIP_THIS */
29
30namespace core::pipe {
31
33 if (sink != nullptr) {
34 sink->disconnect(this);
35 }
36 }
37
38 void Source::pipe(Sink* sink, const std::function<void(int)>& callback) {
39 if (sink != nullptr) {
40 if (isOpen()) {
41 callback(0);
42
43 this->sink = sink;
44 sink->pipe(this);
45 } else {
46 callback(errno);
47 stop();
48 }
49 } else {
50 callback(EINVAL);
51 stop();
52 }
53 }
54
55 void Source::pipe(const std::shared_ptr<Sink>& sink, const std::function<void(int)>& callback) {
56 pipe(sink.get(), callback);
57 }
58
59 void Source::disconnect(const Sink* sink) {
60 if (sink == this->sink) {
61 this->sink->disconnect(this);
62 this->sink = nullptr;
63
64 stop();
65 }
66 }
67
68 ssize_t Source::send(const char* chunk, std::size_t chunkLen) {
69 ssize_t ret = static_cast<ssize_t>(chunkLen);
70
71 if (sink != nullptr) {
72 sink->streamData(chunk, chunkLen);
73 } else {
74 ret = -1;
75 errno = EPIPE;
76 }
77
78 return ret;
79 }
80
81 void Source::eof() {
82 if (sink != nullptr) {
83 sink->streamEof();
84 }
85 }
86
87 void Source::error(int errnum) {
88 if (sink != nullptr) {
89 sink->streamError(errnum);
90 }
91 }
92
93} // namespace core::pipe
void error(int errnum)
Definition Source.cpp:87
virtual void stop()=0
ssize_t send(const char *chunk, std::size_t chunkLen)
Definition Source.cpp:68
virtual ~Source()
Definition Source.cpp:32
virtual bool isOpen()=0