mpd/src/event/BufferedSocket.cxx

120 lines
2.6 KiB
C++
Raw Normal View History

/*
2021-01-01 19:54:25 +01:00
* Copyright 2003-2021 The Music Player Daemon Project
* 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"
#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>
BufferedSocket::ssize_t
2017-12-20 10:42:17 +01:00
BufferedSocket::DirectRead(void *data, size_t length) noexcept
{
const auto nbytes = GetSocket().Read((char *)data, length);
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
OnSocketError(std::make_exception_ptr(MakeSocketError(code, "Failed to receive from socket")));
return -1;
}
bool
2017-12-20 10:42:17 +01:00
BufferedSocket::ReadToBuffer() noexcept
{
assert(IsDefined());
const auto buffer = input.Write();
assert(!buffer.empty());
const auto nbytes = DirectRead(buffer.data, buffer.size);
if (nbytes > 0)
input.Append(nbytes);
return nbytes >= 0;
}
bool
2017-12-20 10:42:17 +01:00
BufferedSocket::ResumeInput() noexcept
{
assert(IsDefined());
while (true) {
const auto buffer = input.Read();
if (buffer.empty()) {
event.ScheduleRead();
return true;
}
const auto result = OnSocketInput(buffer.data, buffer.size);
switch (result) {
case InputResult::MORE:
if (input.IsFull()) {
OnSocketError(std::make_exception_ptr(std::runtime_error("Input buffer is full")));
return false;
}
event.ScheduleRead();
return true;
case InputResult::PAUSE:
event.CancelRead();
return true;
case InputResult::AGAIN:
continue;
case InputResult::CLOSED:
return false;
}
}
}
void
2017-11-10 20:20:07 +01:00
BufferedSocket::OnSocketReady(unsigned flags) noexcept
{
assert(IsDefined());
if (gcc_unlikely(flags & (SocketEvent::ERROR|SocketEvent::HANGUP))) {
OnSocketClosed();
return;
}
if (flags & SocketEvent::READ) {
assert(!input.IsFull());
if (!ReadToBuffer() || !ResumeInput())
return;
if (!input.IsFull())
event.ScheduleRead();
}
}