BufferedSocket.cxx 2.7 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "BufferedSocket.hxx"
22
#include "net/SocketError.hxx"
23 24 25
#include "Compiler.h"

#include <algorithm>
26

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

bool
BufferedSocket::ReadToBuffer()
{
	assert(IsDefined());

55 56
	const auto buffer = input.Write();
	assert(!buffer.IsEmpty());
57

58
	const auto nbytes = DirectRead(buffer.data, buffer.size);
59
	if (nbytes > 0)
60
		input.Append(nbytes);
61 62 63 64 65 66 67 68 69 70

	return nbytes >= 0;
}

bool
BufferedSocket::ResumeInput()
{
	assert(IsDefined());

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

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

			ScheduleRead();
			return true;

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

		case InputResult::AGAIN:
			continue;

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

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

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

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

114
		if (!ReadToBuffer())
115
			return false;
116

117 118 119 120 121 122
		if (!ResumeInput())
			/* we must return "true" here or
			   SocketMonitor::Dispatch() will call
			   Cancel() on a freed object */
			return true;

123
		if (!input.IsFull())
124 125
			ScheduleRead();
	}
126 127

	return true;
128
}