MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

BufferedSocket.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_BUFFERED_SOCKET_HXX
21 #define MPD_BUFFERED_SOCKET_HXX
22
23 #include "check.h"
24 #include "SocketMonitor.hxx"
25 #include "util/StaticFifoBuffer.hxx"
26
27 #include <exception>
28
29 #include <assert.h>
30 #include <stdint.h>
31
32 class EventLoop;
33
37 class BufferedSocket : protected SocketMonitor {
38 StaticFifoBuffer<uint8_t, 8192> input;
39
40 public:
41 BufferedSocket(int _fd, EventLoop &_loop)
42 :SocketMonitor(_fd, _loop) {
43 ScheduleRead();
44 }
45
46 using SocketMonitor::IsDefined;
47 using SocketMonitor::Close;
48 using SocketMonitor::Write;
49
50 private:
51 ssize_t DirectRead(void *data, size_t length);
52
58 bool ReadToBuffer();
59
60 protected:
64 bool ResumeInput();
65
72 void ConsumeInput(size_t nbytes) {
73 assert(IsDefined());
74
75 input.Consume(nbytes);
76 }
77
78 enum class InputResult {
83 MORE,
84
90 PAUSE,
91
96 AGAIN,
97
101 CLOSED,
102 };
103
111 virtual InputResult OnSocketInput(void *data, size_t length) = 0;
112
113 virtual void OnSocketError(std::exception_ptr ep) = 0;
114 virtual void OnSocketClosed() = 0;
115
116 virtual bool OnSocketReady(unsigned flags) override;
117 };
118
119 #endif
BufferedSocket::OnSocketError
virtual void OnSocketError(std::exception_ptr ep)=0
check.h
BufferedSocket::InputResult::PAUSE
The method does not want to get more data for now.
StaticFifoBuffer< uint8_t, 8192 >
BufferedSocket::BufferedSocket
BufferedSocket(int _fd, EventLoop &_loop)
Definition: BufferedSocket.hxx:41
EventLoop
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
SocketMonitor::ssize_t
std::make_signed< size_t >::type ssize_t
Definition: SocketMonitor.hxx:69
BufferedSocket::InputResult
InputResult
Definition: BufferedSocket.hxx:78
BufferedSocket::OnSocketInput
virtual InputResult OnSocketInput(void *data, size_t length)=0
Data has been received on the socket.
SocketMonitor::Write
ssize_t Write(const void *data, size_t length)
SocketMonitor.hxx
BufferedSocket::ResumeInput
bool ResumeInput()
SocketMonitor::IsDefined
bool IsDefined() const
Definition: SocketMonitor.hxx:83
BufferedSocket::InputResult::MORE
The method was successful, and it is ready to read more data.
StaticFifoBuffer::Consume
void Consume(size_type n)
Marks a chunk as consumed.
Definition: StaticFifoBuffer.hxx:124
SocketMonitor::ScheduleRead
void ScheduleRead()
Definition: SocketMonitor.hxx:120
StaticFifoBuffer.hxx
SocketMonitor
Monitor events on a socket.
Definition: SocketMonitor.hxx:54
BufferedSocket::InputResult::AGAIN
The method wants to be called again immediately, if there's more data in the buffer.
BufferedSocket::OnSocketClosed
virtual void OnSocketClosed()=0
BufferedSocket
A SocketMonitor specialization that adds an input buffer.
Definition: BufferedSocket.hxx:37
BufferedSocket::InputResult::CLOSED
The method has closed the socket.
BufferedSocket::ConsumeInput
void ConsumeInput(size_t nbytes)
Mark a portion of the input buffer "consumed".
Definition: BufferedSocket.hxx:72
SocketMonitor::Close
void Close()
BufferedSocket::OnSocketReady
virtual bool OnSocketReady(unsigned flags) override