2013-01-30 10:36:47 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2013-01-30 10:36: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 "config.h"
|
|
|
|
#include "FullyBufferedSocket.hxx"
|
2015-02-10 21:46:23 +01:00
|
|
|
#include "net/SocketError.hxx"
|
2013-11-28 11:50:54 +01:00
|
|
|
#include "Compiler.h"
|
2013-01-30 10:36:47 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
FullyBufferedSocket::ssize_t
|
|
|
|
FullyBufferedSocket::DirectWrite(const void *data, size_t length)
|
|
|
|
{
|
|
|
|
const auto nbytes = SocketMonitor::Write((const char *)data, length);
|
|
|
|
if (gcc_unlikely(nbytes < 0)) {
|
|
|
|
const auto code = GetSocketError();
|
|
|
|
if (IsSocketErrorAgain(code))
|
|
|
|
return 0;
|
|
|
|
|
2013-11-06 20:36:37 +01:00
|
|
|
IdleMonitor::Cancel();
|
|
|
|
BufferedSocket::Cancel();
|
2013-01-30 10:36:47 +01:00
|
|
|
|
|
|
|
if (IsSocketErrorClosed(code))
|
|
|
|
OnSocketClosed();
|
|
|
|
else
|
2016-11-02 10:38:05 +01:00
|
|
|
OnSocketError(std::make_exception_ptr(MakeSocketError(code, "Failed to send to socket")));
|
2013-01-30 10:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-11-06 21:52:09 +01:00
|
|
|
FullyBufferedSocket::Flush()
|
2013-01-30 10:36:47 +01:00
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-12-15 22:39:30 +01:00
|
|
|
const auto data = output.Read();
|
2017-11-10 19:24:33 +01:00
|
|
|
if (data.empty()) {
|
2013-11-06 20:36:37 +01:00
|
|
|
IdleMonitor::Cancel();
|
2013-01-30 10:36:47 +01:00
|
|
|
CancelWrite();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-15 22:39:30 +01:00
|
|
|
auto nbytes = DirectWrite(data.data, data.size);
|
2013-01-30 10:36:47 +01:00
|
|
|
if (gcc_unlikely(nbytes <= 0))
|
|
|
|
return nbytes == 0;
|
|
|
|
|
|
|
|
output.Consume(nbytes);
|
|
|
|
|
2017-11-10 19:24:33 +01:00
|
|
|
if (output.empty()) {
|
2013-11-06 20:36:37 +01:00
|
|
|
IdleMonitor::Cancel();
|
2013-01-30 10:36:47 +01:00
|
|
|
CancelWrite();
|
2013-11-06 20:36:37 +01:00
|
|
|
}
|
2013-01-30 10:36:47 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FullyBufferedSocket::Write(const void *data, size_t length)
|
|
|
|
{
|
|
|
|
assert(IsDefined());
|
|
|
|
|
2013-11-06 20:36:37 +01:00
|
|
|
if (length == 0)
|
|
|
|
return true;
|
|
|
|
|
2017-11-10 19:24:33 +01:00
|
|
|
const bool was_empty = output.empty();
|
2013-11-06 20:36:37 +01:00
|
|
|
|
2013-01-30 10:36:47 +01:00
|
|
|
if (!output.Append(data, length)) {
|
2016-11-02 10:38:05 +01:00
|
|
|
OnSocketError(std::make_exception_ptr(std::runtime_error("Output buffer is full")));
|
2013-01-30 10:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:36:37 +01:00
|
|
|
if (was_empty)
|
|
|
|
IdleMonitor::Schedule();
|
2013-01-30 10:36:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FullyBufferedSocket::OnSocketReady(unsigned flags)
|
|
|
|
{
|
|
|
|
if (flags & WRITE) {
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(!output.empty());
|
2013-11-06 20:36:37 +01:00
|
|
|
assert(!IdleMonitor::IsActive());
|
2013-01-30 10:36:47 +01:00
|
|
|
|
2013-11-06 21:52:09 +01:00
|
|
|
if (!Flush())
|
2013-01-30 10:36:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:36:37 +01:00
|
|
|
if (!BufferedSocket::OnSocketReady(flags))
|
|
|
|
return false;
|
|
|
|
|
2013-01-30 10:36:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-06 20:36:37 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
FullyBufferedSocket::OnIdle()
|
|
|
|
{
|
2017-11-10 19:24:33 +01:00
|
|
|
if (Flush() && !output.empty())
|
2013-11-06 20:36:37 +01:00
|
|
|
ScheduleWrite();
|
|
|
|
}
|