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
67 const std::size_t available = doRead();
68
69 if (available > 0) {
70 onReceivedFromPeer(available);
71 }
72 }
73
74 void SocketReader::signalEvent([[maybe_unused]] int sigNum) { // Do nothing in case a signal was received
75 }
76
77 ssize_t SocketReader::read(char* chunk, std::size_t chunkLen) {
78 return core::system::recv(this->getRegisteredFd(), chunk, chunkLen, 0);
79 }
80
81 std::size_t SocketReader::doRead() {
82 errno = 0;
83
84 if (size == 0) {
85 cursor = 0;
86
87 ssize_t retRead = 0;
88 if (!shutdownInProgress) {
89 const std::size_t readLen = blockSize - size;
90 retRead = read(readBuffer.data() + size, readLen);
91 }
92 if (retRead > 0) {
93 size += static_cast<std::size_t>(retRead);
94
95 if (!isSuspended()) {
97 }
98 span();
99 } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
100 if (isSuspended()) {
101 resume();
102 }
103 } else {
104 onStatus(errno);
105 }
106 } else {
107 span();
108 }
109
110 return size;
111 }
112
113 void SocketReader::setBlockSize(std::size_t readBlockSize) {
114 readBuffer.resize(readBlockSize);
115 blockSize = readBlockSize;
116 }
117
118 std::size_t SocketReader::readFromPeer(char* chunk, std::size_t chunkLen) {
119 const std::size_t maxReturn = std::min(chunkLen, size);
120
121 std::copy(readBuffer.data() + cursor, readBuffer.data() + cursor + maxReturn, chunk);
122
123 cursor += maxReturn;
124 size -= maxReturn;
125
126 return maxReturn;
127 }
128
130 readBuffer.clear();
131
132 size = 0;
133 cursor = 0;
134
135 shutdownInProgress = true;
136
138 }
139
140} // 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)
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