MPD  0.20.6
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  buffer_size(_buffer_size) {}
77 
78  virtual ~ThreadInputStream();
79 
83  void Start();
84 
85  /* virtual methods from InputStream */
86  void Check() override final;
87  bool IsEOF() override final;
88  bool IsAvailable() override final;
89  size_t Read(void *ptr, size_t size) override final;
90 
91 protected:
92  void SetMimeType(const char *_mime) {
93  assert(thread.IsInside());
94 
96  }
97 
98  /* to be implemented by the plugin */
99 
110  virtual void Open() {
111  }
112 
122  virtual size_t ThreadRead(void *ptr, size_t size) = 0;
123 
129  virtual void Close() {}
130 
137  virtual void Cancel() {}
138 
139 private:
140  void ThreadFunc();
141  static void ThreadFunc(void *ctx);
142 };
143 
144 #endif
virtual void Close()
Optional deinitialization before leaving the thread.
gcc_nonnull_all void SetMimeType(const char *_mime)
size_t Read(void *ptr, size_t size) overridefinal
Reads data from the stream into the caller-supplied buffer.
void Check() overridefinal
Check for errors that may have occurred in the I/O thread.
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
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
Check if this thread is the current thread.
Definition: Thread.hxx:80
virtual void Cancel()
Called from the client thread to cancel a Read() inside the thread.
bool IsEOF() overridefinal
Returns true if the stream has reached end-of-file.
bool IsAvailable() overridefinal
Returns true if the next read operation will not block: either data is available, or end-of-stream ha...