MPD  0.20.18
ThreadInputStream.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_THREAD_INPUT_STREAM_HXX
21 #define MPD_THREAD_INPUT_STREAM_HXX
22 
23 #include "check.h"
24 #include "InputStream.hxx"
25 #include "thread/Thread.hxx"
26 #include "thread/Cond.hxx"
27 
28 #include <exception>
29 
30 #include <stdint.h>
31 
32 template<typename T> class CircularBuffer;
33 
44  const char *const plugin;
45 
46  Thread thread;
47 
53  Cond wake_cond;
54 
55  std::exception_ptr postponed_exception;
56 
57  const size_t buffer_size;
58  CircularBuffer<uint8_t> *buffer = nullptr;
59 
63  bool close = false;
64 
68  bool eof = false;
69 
70 public:
71  ThreadInputStream(const char *_plugin,
72  const char *_uri, Mutex &_mutex, Cond &_cond,
73  size_t _buffer_size)
74  :InputStream(_uri, _mutex, _cond),
75  plugin(_plugin),
76  thread(BIND_THIS_METHOD(ThreadFunc)),
77  buffer_size(_buffer_size) {}
78 
79  virtual ~ThreadInputStream();
80 
84  void Start();
85 
86  /* virtual methods from InputStream */
87  void Check() override final;
88  bool IsEOF() noexcept final;
89  bool IsAvailable() noexcept final;
90  size_t Read(void *ptr, size_t size) override final;
91 
92 protected:
93  void SetMimeType(const char *_mime) {
94  assert(thread.IsInside());
95 
97  }
98 
99  /* to be implemented by the plugin */
100 
111  virtual void Open() {
112  }
113 
123  virtual size_t ThreadRead(void *ptr, size_t size) = 0;
124 
130  virtual void Close() {}
131 
138  virtual void Cancel() {}
139 
140 private:
141  void ThreadFunc();
142 };
143 
144 #endif
#define BIND_THIS_METHOD(method)
Shortcut wrapper for BIND_METHOD() which assumes "*this" is the instance to be bound.
Definition: BindMethod.hxx:207
virtual void Close()
Optional deinitialization before leaving the thread.
bool IsEOF() noexcept final
Returns true if the stream has reached end-of-file.
gcc_nonnull_all void SetMimeType(const char *_mime)
Definition: Cond.hxx:41
Definition: Mutex.hxx:43
void SetMimeType(const char *_mime)
offset_type size
the size of the resource, or UNKNOWN_SIZE if unknown
Definition: InputStream.hxx:84
bool IsAvailable() noexcept final
Returns true if the next read operation will not block: either data is available, or end-of-stream ha...
A circular buffer.
virtual ~ThreadInputStream()
ThreadInputStream(const char *_plugin, const char *_uri, Mutex &_mutex, Cond &_cond, size_t _buffer_size)
virtual void Open()
Optional initialization after entering the thread.
Helper class for moving InputStream implementations with blocking backend library implementation to a...
void Start()
Initialize the object and start the thread.
virtual size_t ThreadRead(void *ptr, size_t size)=0
Read from the stream.
gcc_pure bool IsInside() const noexcept
Check if this thread is the current thread.
Definition: Thread.hxx:82
virtual void Cancel()
Called from the client thread to cancel a Read() inside the thread.
void Check() override final
Check for errors that may have occurred in the I/O thread.
size_t Read(void *ptr, size_t size) override final
Reads data from the stream into the caller-supplied buffer.