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/*
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/pipe/Source.h"
43
44#include "core/pipe/Sink.h"
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48#include <cerrno>
49
50#endif /* DOXYGEN_SHOULD_SKIP_THIS */
51
52namespace core::pipe {
53
55 if (sink != nullptr) {
57 }
58 }
59
60 void Source::pipe(Sink* sink, const std::function<void(int)>& callback) {
61 if (sink != nullptr) {
62 if (isOpen()) {
63 callback(0);
64
65 this->sink = sink;
66 sink->pipe(this);
67 } else {
68 callback(errno);
69 stop();
70 }
71 } else {
72 callback(EINVAL);
73 stop();
74 }
75 }
76
77 void Source::pipe(const std::shared_ptr<Sink>& sink, const std::function<void(int)>& callback) {
78 pipe(sink.get(), callback);
79 }
80
81 void Source::disconnect(const Sink* sink) {
82 if (sink == this->sink) {
84 this->sink = nullptr;
85
86 stop();
87 }
88 }
89
90 ssize_t Source::send(const char* chunk, std::size_t chunkLen) {
91 ssize_t ret = static_cast<ssize_t>(chunkLen);
92
93 if (sink != nullptr) {
94 sink->streamData(chunk, chunkLen);
95 } else {
96 ret = -1;
97 errno = EPIPE;
98 }
99
100 return ret;
101 }
102
103 void Source::eof() {
104 if (sink != nullptr) {
106 }
107 }
108
109 void Source::error(int errnum) {
110 if (sink != nullptr) {
112 }
113 }
114
115} // namespace core::pipe
void pipe(Source *source)
Definition Sink.cpp:58
void streamError(int errnum)
Definition Sink.cpp:82
void disconnect(const Source *source)
Definition Sink.cpp:86
void streamData(const char *chunk, std::size_t chunkLen)
Definition Sink.cpp:74
void streamEof()
Definition Sink.cpp:78
void error(int errnum)
Definition Source.cpp:109
virtual void stop()=0
ssize_t send(const char *chunk, std::size_t chunkLen)
Definition Source.cpp:90
void pipe(const std::shared_ptr< Sink > &sink, const std::function< void(int)> &callback)
Definition Source.cpp:77
virtual ~Source()
Definition Source.cpp:54
virtual bool isOpen()=0
void disconnect(const Sink *sink)
Definition Source.cpp:81
void pipe(Sink *sink, const std::function< void(int)> &callback)
Definition Source.cpp:60