FullyBufferedSocket.cxx 2.55 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "FullyBufferedSocket.hxx"
21
#include "net/SocketError.hxx"
22
#include "util/Compiler.h"
23 24 25 26 27

#include <assert.h>
#include <string.h>

FullyBufferedSocket::ssize_t
28
FullyBufferedSocket::DirectWrite(const void *data, size_t length) noexcept
29
{
30
	const auto nbytes = GetSocket().Write((const char *)data, length);
31 32 33 34 35
	if (gcc_unlikely(nbytes < 0)) {
		const auto code = GetSocketError();
		if (IsSocketErrorAgain(code))
			return 0;

36 37
		IdleMonitor::Cancel();
		BufferedSocket::Cancel();
38 39 40 41

		if (IsSocketErrorClosed(code))
			OnSocketClosed();
		else
42
			OnSocketError(std::make_exception_ptr(MakeSocketError(code, "Failed to send to socket")));
43 44 45 46 47 48
	}

	return nbytes;
}

bool
49
FullyBufferedSocket::Flush() noexcept
50 51 52
{
	assert(IsDefined());

53
	const auto data = output.Read();
54
	if (data.empty()) {
55
		IdleMonitor::Cancel();
56 57 58 59
		CancelWrite();
		return true;
	}

60
	auto nbytes = DirectWrite(data.data, data.size);
61 62 63 64 65
	if (gcc_unlikely(nbytes <= 0))
		return nbytes == 0;

	output.Consume(nbytes);

66
	if (output.empty()) {
67
		IdleMonitor::Cancel();
68
		CancelWrite();
69
	}
70 71 72 73 74

	return true;
}

bool
75
FullyBufferedSocket::Write(const void *data, size_t length) noexcept
76 77 78
{
	assert(IsDefined());

79 80 81
	if (length == 0)
		return true;

82
	const bool was_empty = output.empty();
83

84
	if (!output.Append(data, length)) {
85
		OnSocketError(std::make_exception_ptr(std::runtime_error("Output buffer is full")));
86 87 88
		return false;
	}

89 90
	if (was_empty)
		IdleMonitor::Schedule();
91 92 93 94
	return true;
}

bool
95
FullyBufferedSocket::OnSocketReady(unsigned flags) noexcept
96 97
{
	if (flags & WRITE) {
98
		assert(!output.empty());
99
		assert(!IdleMonitor::IsActive());
100

101
		if (!Flush())
102 103 104
			return false;
	}

105 106 107
	if (!BufferedSocket::OnSocketReady(flags))
		return false;

108 109
	return true;
}
110 111

void
112
FullyBufferedSocket::OnIdle() noexcept
113
{
114
	if (Flush() && !output.empty())
115 116
		ScheduleWrite();
}