BufferedSocket.cxx 2.59 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 "BufferedSocket.hxx"
21
#include "net/SocketError.hxx"
22
#include "util/Compiler.h"
23 24

#include <algorithm>
25

26
BufferedSocket::ssize_t
27
BufferedSocket::DirectRead(void *data, size_t length) noexcept
28
{
29
	const auto nbytes = GetSocket().Read((char *)data, length);
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	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
45
		OnSocketError(std::make_exception_ptr(MakeSocketError(code, "Failed to receive from socket")));
46 47 48 49
	return -1;
}

bool
50
BufferedSocket::ReadToBuffer() noexcept
51 52 53
{
	assert(IsDefined());

54
	const auto buffer = input.Write();
55
	assert(!buffer.empty());
56

57
	const auto nbytes = DirectRead(buffer.data, buffer.size);
58
	if (nbytes > 0)
59
		input.Append(nbytes);
60 61 62 63 64

	return nbytes >= 0;
}

bool
65
BufferedSocket::ResumeInput() noexcept
66 67 68 69
{
	assert(IsDefined());

	while (true) {
70
		const auto buffer = input.Read();
71
		if (buffer.empty()) {
72 73 74 75
			ScheduleRead();
			return true;
		}

76
		const auto result = OnSocketInput(buffer.data, buffer.size);
77 78
		switch (result) {
		case InputResult::MORE:
79
			if (input.IsFull()) {
80
				OnSocketError(std::make_exception_ptr(std::runtime_error("Input buffer is full")));
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
				return false;
			}

			ScheduleRead();
			return true;

		case InputResult::PAUSE:
			CancelRead();
			return true;

		case InputResult::AGAIN:
			continue;

		case InputResult::CLOSED:
			return false;
		}
	}
}

100
bool
101
BufferedSocket::OnSocketReady(unsigned flags) noexcept
102 103 104 105 106
{
	assert(IsDefined());

	if (gcc_unlikely(flags & (ERROR|HANGUP))) {
		OnSocketClosed();
107
		return false;
108 109 110
	}

	if (flags & READ) {
111
		assert(!input.IsFull());
112

113
		if (!ReadToBuffer() || !ResumeInput())
114
			return false;
115

116
		if (!input.IsFull())
117 118
			ScheduleRead();
	}
119 120

	return true;
121
}