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#include "core/socket/stream/SocketReader.h"
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "core/system/socket.h"
25
26#include <algorithm>
27#include <cerrno>
28
29#endif // DOXYGEN_SHOULD_SKIP_THIS
30
31namespace core::socket::stream {
32
33 SocketReader::SocketReader(const std::string& instanceName,
34 const std::function<void(int)>& onStatus,
35 const utils::Timeval& timeout,
36 std::size_t blockSize,
37 const utils::Timeval& terminateTimeout)
38 : core::eventreceiver::ReadEventReceiver(instanceName, timeout)
40 , terminateTimeout(terminateTimeout) {
41 setBlockSize(blockSize);
42 }
43
45 const std::size_t available = doRead();
46
47 if (available > 0) {
48 onReceivedFromPeer(available);
49 }
50 }
51
52 void SocketReader::signalEvent([[maybe_unused]] int sigNum) { // Do nothing in case a signal was received
53 }
54
55 ssize_t SocketReader::read(char* chunk, std::size_t chunkLen) {
56 return core::system::recv(this->getRegisteredFd(), chunk, chunkLen, 0);
57 }
58
59 std::size_t SocketReader::doRead() {
60 errno = 0;
61
62 if (size == 0) {
63 cursor = 0;
64
65 ssize_t retRead = 0;
66 if (!shutdownInProgress) {
67 const std::size_t readLen = blockSize - size;
68 retRead = read(readBuffer.data() + size, readLen);
69 }
70 if (retRead > 0) {
71 size += static_cast<std::size_t>(retRead);
72
73 if (!isSuspended()) {
74 suspend();
75 }
76 span();
77 } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
78 if (isSuspended()) {
79 resume();
80 }
81 } else {
82 onStatus(errno);
83 }
84 } else {
85 span();
86 }
87
88 return size;
89 }
90
91 void SocketReader::setBlockSize(std::size_t readBlockSize) {
92 readBuffer.resize(readBlockSize);
93 blockSize = readBlockSize;
94 }
95
96 std::size_t SocketReader::readFromPeer(char* chunk, std::size_t chunkLen) {
97 const std::size_t maxReturn = std::min(chunkLen, size);
98
99 std::copy(readBuffer.data() + cursor, readBuffer.data() + cursor + maxReturn, chunk);
100
101 cursor += maxReturn;
102 size -= maxReturn;
103
104 return maxReturn;
105 }
106
108 readBuffer.clear();
109
110 size = 0;
111 cursor = 0;
112
113 shutdownInProgress = true;
114
116 }
117
118} // 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)
Timeval(const Timeval &timeVal) noexcept=default
ssize_t recv(int sockfd, void *buf, std::size_t len, int flags)
Definition socket.cpp:69