2013-01-30 10:36:47 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 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-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.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
|
|
|
|
OnSocketError(NewSocketError(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2013-12-19 10:30:26 +01:00
|
|
|
if (data.IsEmpty()) {
|
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);
|
|
|
|
|
2013-11-06 20:36:37 +01:00
|
|
|
if (output.IsEmpty()) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
const bool was_empty = output.IsEmpty();
|
|
|
|
|
2013-01-30 10:36:47 +01:00
|
|
|
if (!output.Append(data, length)) {
|
|
|
|
// TODO
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain buffered_socket_domain("buffered_socket");
|
|
|
|
Error error;
|
|
|
|
error.Set(buffered_socket_domain, "Output buffer is full");
|
|
|
|
OnSocketError(std::move(error));
|
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) {
|
|
|
|
assert(!output.IsEmpty());
|
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()
|
|
|
|
{
|
|
|
|
if (Flush() && !output.IsEmpty())
|
|
|
|
ScheduleWrite();
|
|
|
|
}
|