MPD  0.20.15
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"
26 
27 #include <exception>
28 
29 #include <assert.h>
30 #include <stdint.h>
31 
32 class EventLoop;
33 
37 class BufferedSocket : protected SocketMonitor {
39 
40 public:
41  BufferedSocket(int _fd, EventLoop &_loop)
42  :SocketMonitor(_fd, _loop) {
43  ScheduleRead();
44  }
45 
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
virtual void OnSocketError(std::exception_ptr ep)=0
The method does not want to get more data for now.
BufferedSocket(int _fd, EventLoop &_loop)
An event loop that polls for events on file/socket descriptors.
Definition: Loop.hxx:51
std::make_signed< size_t >::type ssize_t
virtual InputResult OnSocketInput(void *data, size_t length)=0
Data has been received on the socket.
ssize_t Write(const void *data, size_t length)
bool ResumeInput()
The method was successful, and it is ready to read more data.
bool IsDefined() const
void Consume(size_type n)
Marks a chunk as consumed.
Monitor events on a socket.
The method wants to be called again immediately, if there&#39;s more data in the buffer.
virtual void OnSocketClosed()=0
A SocketMonitor specialization that adds an input buffer.
The method has closed the socket.
void ConsumeInput(size_t nbytes)
Mark a portion of the input buffer "consumed".
virtual bool OnSocketReady(unsigned flags) override