2013-01-10 19:05:47 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2013-01-10 19:05:47 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BufferedSocket.hxx"
|
2015-02-10 21:46:23 +01:00
|
|
|
#include "net/SocketError.hxx"
|
2018-08-20 16:19:17 +02:00
|
|
|
#include "util/Compiler.h"
|
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
|
|
|
|
2013-01-30 10:39:17 +01:00
|
|
|
BufferedSocket::ssize_t
|
2017-12-20 10:42:17 +01:00
|
|
|
BufferedSocket::DirectRead(void *data, size_t length) noexcept
|
2013-01-10 19:05:47 +01:00
|
|
|
{
|
2017-11-10 20:31:32 +01:00
|
|
|
const auto nbytes = GetSocket().Read((char *)data, length);
|
2013-01-10 19:05:47 +01:00
|
|
|
if (gcc_likely(nbytes > 0))
|
|
|
|
return nbytes;
|
|
|
|
|
|
|
|
if (nbytes == 0) {
|
|
|
|
OnSocketClosed();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto code = GetSocketError();
|
|
|
|
if (IsSocketErrorAgain(code))
|
|
|
|
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
|
|
|
|
2013-10-15 10:28:52 +02:00
|
|
|
const auto nbytes = DirectRead(buffer.data, buffer.size);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-15 10:28:52 +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());
|
|
|
|
|
2020-10-14 14:24:16 +02:00
|
|
|
if (gcc_unlikely(flags & (SocketEvent::ERROR|SocketEvent::HANGUP))) {
|
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
|
|
|
}
|
|
|
|
}
|