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
36
namespace
core::
pipe
{
37
38
PipeSource
::
PipeSource
(
int
fd)
39
:
core
::
eventreceiver
::
WriteEventReceiver
(
"PipeSource fd = "
+
std
::
to_string
(
fd
), 60) {
40
if
(!
WriteEventReceiver
::
enable
(fd)) {
41
delete
this
;
42
}
else
{
43
WriteEventReceiver
::
suspend
();
44
}
45
}
46
47
PipeSource
::~
PipeSource
() {
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
58
if
(
WriteEventReceiver
::
isSuspended
()) {
59
WriteEventReceiver
::
resume
();
60
}
61
}
62
63
void
PipeSource
::
send
(
const
std::string& data) {
64
send(data.data(), data.size());
65
}
66
67
void
PipeSource
::
eof
() {
68
WriteEventReceiver
::
disable
();
69
}
70
71
void
PipeSource
::
writeEvent
() {
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()) {
79
WriteEventReceiver
::
suspend
();
80
}
81
}
else
if
(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) {
82
WriteEventReceiver
::
disable
();
83
84
if
(onError) {
85
onError(errno);
86
}
87
}
88
}
89
90
void
PipeSource
::
unobservedEvent
() {
91
delete
this
;
92
}
93
94
}
// namespace core::pipe
MAX_SEND_JUNKSIZE
#define MAX_SEND_JUNKSIZE
Definition
PipeSource.cpp:33
core::DescriptorEventReceiver::disable
void disable()
Definition
DescriptorEventReceiver.cpp:86
core::DescriptorEventReceiver::getRegisteredFd
int getRegisteredFd() const
Definition
DescriptorEventReceiver.cpp:62
core::DescriptorEventReceiver::suspend
void suspend()
Definition
DescriptorEventReceiver.cpp:104
core::DescriptorEventReceiver::isSuspended
bool isSuspended() const
Definition
DescriptorEventReceiver.cpp:131
core::DescriptorEventReceiver::enable
bool enable(int fd)
Definition
DescriptorEventReceiver.cpp:66
core::DescriptorEventReceiver::resume
void resume()
Definition
DescriptorEventReceiver.cpp:117
core::eventreceiver::WriteEventReceiver
Definition
WriteEventReceiver.h:35
core::pipe::PipeSource
Definition
PipeSource.h:36
core::pipe::PipeSource::send
void send(const char *chunk, std::size_t chunkLen)
Definition
PipeSource.cpp:55
core::pipe::PipeSource::eof
void eof()
Definition
PipeSource.cpp:67
core::pipe::PipeSource::~PipeSource
~PipeSource() override
Definition
PipeSource.cpp:47
core::pipe::PipeSource::send
void send(const std::string &data)
Definition
PipeSource.cpp:63
core::pipe::PipeSource::PipeSource
PipeSource(int fd)
Definition
PipeSource.cpp:38
core::pipe::PipeSource::setOnError
void setOnError(const std::function< void(int)> &onError)
Definition
PipeSource.cpp:51
core::pipe::PipeSource::writeEvent
void writeEvent() override
Definition
PipeSource.cpp:71
core::pipe::PipeSource::unobservedEvent
void unobservedEvent() override
Definition
PipeSource.cpp:90
core::pipe
Definition
SocketContext.h:25
core
pipe
PipeSource.cpp
Generated on Mon Feb 10 2025 20:20:48 for SNode.C by
1.11.0