2013-01-30 10:36:47 +01:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPD_FULLY_BUFFERED_SOCKET_HXX
|
|
|
|
#define MPD_FULLY_BUFFERED_SOCKET_HXX
|
|
|
|
|
|
|
|
#include "check.h"
|
|
|
|
#include "BufferedSocket.hxx"
|
2013-11-06 20:36:37 +01:00
|
|
|
#include "IdleMonitor.hxx"
|
2013-01-30 10:36:47 +01:00
|
|
|
#include "util/PeakBuffer.hxx"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A #BufferedSocket specialization that adds an output buffer.
|
|
|
|
*/
|
2013-11-06 20:36:37 +01:00
|
|
|
class FullyBufferedSocket : protected BufferedSocket, private IdleMonitor {
|
2013-01-30 10:36:47 +01:00
|
|
|
PeakBuffer output;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FullyBufferedSocket(int _fd, EventLoop &_loop,
|
|
|
|
size_t normal_size, size_t peak_size=0)
|
2013-11-06 20:36:37 +01:00
|
|
|
:BufferedSocket(_fd, _loop), IdleMonitor(_loop),
|
2013-01-30 10:36:47 +01:00
|
|
|
output(normal_size, peak_size) {
|
|
|
|
}
|
|
|
|
|
|
|
|
using BufferedSocket::IsDefined;
|
2013-11-06 20:36:37 +01:00
|
|
|
|
|
|
|
void Close() {
|
|
|
|
IdleMonitor::Cancel();
|
|
|
|
BufferedSocket::Close();
|
|
|
|
}
|
2013-01-30 10:36:47 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
ssize_t DirectWrite(const void *data, size_t length);
|
|
|
|
|
2013-11-06 21:52:09 +01:00
|
|
|
protected:
|
2013-01-30 10:36:47 +01:00
|
|
|
/**
|
|
|
|
* Send data from the output buffer to the socket.
|
|
|
|
*
|
|
|
|
* @return false if the socket has been closed
|
|
|
|
*/
|
2013-11-06 21:52:09 +01:00
|
|
|
bool Flush();
|
2013-01-30 10:36:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return false if the socket has been closed
|
|
|
|
*/
|
|
|
|
bool Write(const void *data, size_t length);
|
|
|
|
|
|
|
|
virtual bool OnSocketReady(unsigned flags) override;
|
2013-11-06 20:36:37 +01:00
|
|
|
virtual void OnIdle() override;
|
2013-01-30 10:36:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|