SNode.C
Loading...
Searching...
No Matches
SocketReader.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/socket/stream/SocketReader.h"
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "core/system/socket.h"
47
48#include <algorithm>
49#include <cerrno>
50
51#endif // DOXYGEN_SHOULD_SKIP_THIS
52
53namespace core::socket::stream {
54
55 SocketReader::SocketReader(const std::string& instanceName,
56 const std::function<void(int)>& onStatus,
57 const utils::Timeval& timeout,
58 std::size_t blockSize,
59 const utils::Timeval& terminateTimeout)
60 : core::eventreceiver::ReadEventReceiver(instanceName, timeout)
61 , onStatus(onStatus)
62 , terminateTimeout(terminateTimeout) {
63 setBlockSize(blockSize);
64 }
65
66 std::size_t SocketReader::getTotalRead() const {
67 return totalRead;
68 }
69
70 std::size_t SocketReader::getTotalProcessed() const {
71 return totalProcessed;
72 }
73
75 const std::size_t available = doRead();
76
77 if (available > 0) {
78 onReceivedFromPeer(available);
79 }
80 }
81
82 void SocketReader::signalEvent([[maybe_unused]] int sigNum) { // Do nothing in case a signal was received
83 }
84
85 ssize_t SocketReader::read(char* chunk, std::size_t chunkLen) {
86 return core::system::recv(this->getRegisteredFd(), chunk, chunkLen, 0);
87 }
88
89 std::size_t SocketReader::doRead() {
90 errno = 0;
91
92 if (size == 0) {
93 cursor = 0;
94
95 ssize_t retRead = 0;
96 if (!shutdownInProgress) {
97 const std::size_t readLen = blockSize - size;
98 retRead = read(readBuffer.data() + size, readLen);
99 }
100 if (retRead > 0) {
101 totalRead += static_cast<std::size_t>(retRead);
102
103 size += static_cast<std::size_t>(retRead);
104
105 if (!isSuspended()) {
106 suspend();
107 }
108 span();
109 } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
110 if (isSuspended()) {
111 resume();
112 }
113 } else {
114 onStatus(errno);
115 }
116 } else {
117 span();
118 }
119
120 return size;
121 }
122
123 void SocketReader::setBlockSize(std::size_t readBlockSize) {
124 readBuffer.resize(readBlockSize);
125 blockSize = readBlockSize;
126 }
127
128 std::size_t SocketReader::readFromPeer(char* chunk, std::size_t chunkLen) {
129 const std::size_t maxReturn = std::min(chunkLen, size);
130
131 std::copy(readBuffer.data() + cursor, readBuffer.data() + cursor + maxReturn, chunk);
132
133 cursor += maxReturn;
134 size -= maxReturn;
135 totalProcessed += maxReturn;
136
137 return maxReturn;
138 }
139
141 readBuffer.clear();
142
143 size = 0;
144 cursor = 0;
145
146 shutdownInProgress = true;
147
149 }
150
151} // namespace core::socket::stream
void setTimeout(const utils::Timeval &timeout)
ReadEventReceiver(const std::string &name, const utils::Timeval &timeout)
void signalEvent(int sigNum) final
SocketReader(const std::string &instanceName, const std::function< void(int)> &onStatus, const utils::Timeval &timeout, std::size_t blockSize, const utils::Timeval &terminateTimeout)
std::size_t readFromPeer(char *chunk, std::size_t chunkLen)
void setBlockSize(std::size_t readBlockSize)
std::size_t getTotalProcessed() const
virtual void onReceivedFromPeer(std::size_t available)=0
virtual ssize_t read(char *chunk, std::size_t chunkLen)
std::function< void(int)> onStatus
Timeval(const Timeval &timeVal) noexcept=default
ssize_t recv(int sockfd, void *buf, std::size_t len, int flags)
Definition socket.cpp:91