2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
#include "BufferedSocket.hxx"
|
2015-02-10 21:46:23 +01:00
|
|
|
#include "net/SocketError.hxx"
|
2013-11-28 11:50:54 +01:00
|
|
|
|
2019-07-05 09:59:00 +02:00
|
|
|
#include <stdexcept>
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2023-09-27 10:28:26 +02:00
|
|
|
inline BufferedSocket::ssize_t
|
|
|
|
BufferedSocket::DirectRead(std::span<std::byte> dest) noexcept
|
2013-01-10 19:05:47 +01:00
|
|
|
{
|
2023-09-27 10:28:26 +02:00
|
|
|
const auto nbytes = GetSocket().Read((char *)dest.data(), dest.size());
|
2023-03-06 15:57:36 +01:00
|
|
|
if (nbytes > 0) [[likely]]
|
2013-01-10 19:05:47 +01:00
|
|
|
return nbytes;
|
|
|
|
|
|
|
|
if (nbytes == 0) {
|
|
|
|
OnSocketClosed();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto code = GetSocketError();
|
2021-01-21 22:04:14 +01:00
|
|
|
if (IsSocketErrorReceiveWouldBlock(code))
|
2013-01-10 19:05:47 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (IsSocketErrorClosed(code))
|
|
|
|
OnSocketClosed();
|
|
|
|
else
|
2016-11-02 10:38:05 +01:00
|
|
|
OnSocketError(std::make_exception_ptr(MakeSocketError(code, "Failed to receive from socket")));
|
2013-01-10 19:05:47 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-12-20 10:42:17 +01:00
|
|
|
BufferedSocket::ReadToBuffer() noexcept
|
2013-01-10 19:05:47 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-10-15 10:28:52 +02:00
|
|
|
const auto buffer = input.Write();
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(!buffer.empty());
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2023-09-27 10:28:26 +02:00
|
|
|
const auto nbytes = DirectRead(buffer);
|
2013-01-10 19:05:47 +01:00
|
|
|
if (nbytes > 0)
|
2013-10-15 10:28:52 +02:00
|
|
|
input.Append(nbytes);
|
2013-01-10 19:05:47 +01:00
|
|
|
|
|
|
|
return nbytes >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-12-20 10:42:17 +01:00
|
|
|
BufferedSocket::ResumeInput() noexcept
|
2013-01-10 19:05:47 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
|
|
|
while (true) {
|
2013-10-15 10:28:52 +02:00
|
|
|
const auto buffer = input.Read();
|
2017-11-10 19:24:33 +01:00
|
|
|
if (buffer.empty()) {
|
2020-10-14 14:24:16 +02:00
|
|
|
event.ScheduleRead();
|
2013-01-10 19:05:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-10 17:26:41 +02:00
|
|
|
const auto result = OnSocketInput(buffer.data(), buffer.size());
|
2013-01-10 19:05:47 +01:00
|
|
|
switch (result) {
|
|
|
|
case InputResult::MORE:
|
2013-10-15 10:28:52 +02:00
|
|
|
if (input.IsFull()) {
|
2016-11-02 10:38:05 +01:00
|
|
|
OnSocketError(std::make_exception_ptr(std::runtime_error("Input buffer is full")));
|
2013-01-10 19:05:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
event.ScheduleRead();
|
2013-01-10 19:05:47 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case InputResult::PAUSE:
|
2020-10-14 14:24:16 +02:00
|
|
|
event.CancelRead();
|
2013-01-10 19:05:47 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case InputResult::AGAIN:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case InputResult::CLOSED:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
void
|
2017-11-10 20:20:07 +01:00
|
|
|
BufferedSocket::OnSocketReady(unsigned flags) noexcept
|
2013-01-10 19:05:47 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2023-03-06 15:57:36 +01:00
|
|
|
if (flags & (SocketEvent::ERROR|SocketEvent::HANGUP)) [[unlikely]] {
|
2013-01-10 19:05:47 +01:00
|
|
|
OnSocketClosed();
|
2020-10-14 14:24:16 +02:00
|
|
|
return;
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
if (flags & SocketEvent::READ) {
|
2013-10-15 10:28:52 +02:00
|
|
|
assert(!input.IsFull());
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2019-04-03 23:23:56 +02:00
|
|
|
if (!ReadToBuffer() || !ResumeInput())
|
2020-10-14 14:24:16 +02:00
|
|
|
return;
|
2013-01-10 19:05:47 +01:00
|
|
|
|
2014-08-07 16:03:44 +02:00
|
|
|
if (!input.IsFull())
|
2020-10-14 14:24:16 +02:00
|
|
|
event.ScheduleRead();
|
2013-01-10 19:05:47 +01:00
|
|
|
}
|
|
|
|
}
|