MythTV  master
netstream.h
Go to the documentation of this file.
1 /* Network stream
2  * Copyright 2011 Lawrence Rust <lvr at softsystem dot co dot uk>
3  */
4 #ifndef NETSTREAM_H
5 #define NETSTREAM_H
6 
7 #include <QList>
8 #include <QString>
9 #include <QByteArray>
10 #include <QObject>
11 #include <QMutex>
12 #include <QSemaphore>
13 #include <QThread>
14 #include <QNetworkRequest>
15 #include <QNetworkReply>
16 #include <QSslError>
17 #include <QWaitCondition>
18 #include <QQueue>
19 #include <QDateTime>
20 
21 class QUrl;
22 class QNetworkAccessManager;
23 class NetStreamRequest;
24 class NetStreamAbort;
25 
26 
30 class NetStream : public QObject
31 {
32  Q_OBJECT
33 
34 public:
36  explicit NetStream(const QUrl &url, EMode mode = kPreferCache,
37  QByteArray cert = QByteArray());
38  ~NetStream() override;
39 
40 public:
41  // RingBuffer interface
42  static bool IsSupported(const QUrl &url);
43  bool IsOpen() const;
44  void Abort();
45  int safe_read(void *data, unsigned sz, unsigned millisecs = 0);
46  qlonglong Seek(qlonglong pos);
47  qlonglong GetReadPosition() const;
48  qlonglong GetSize() const;
49 
50  // Properties
51  const QUrl &Url() const { return m_url; }
52 
53  // Synchronous interface
54  bool WaitTillReady(unsigned long milliseconds);
55  bool WaitTillFinished(unsigned long milliseconds);
56  QNetworkReply::NetworkError GetError() const;
57  QString GetErrorString() const;
58  qlonglong BytesAvailable() const;
59  QByteArray ReadAll();
60 
61  // Async interface
62  bool isStarted() const;
63  bool isReady() const;
64  bool isFinished() const;
65 
66 signals:
67  void ReadyRead(QObject*);
68  void Finished(QObject*);
69 
70 public:
71  // Time when a URI was last written to cache or invalid if not cached.
72  static QDateTime GetLastModified(const QUrl &url);
73  // Is the network accessible
74  static bool isAvailable();
75 
76  // Implementation
77 private slots:
78  // NAMThread signals
79  void slotRequestStarted(int id, QNetworkReply *reply);
80  // QNetworkReply signals
81  void slotFinished();
82 #ifndef QT_NO_OPENSSL
83  void slotSslErrors(const QList<QSslError> & errors);
84 #endif
85  // QIODevice signals
86  void slotReadyRead();
87 
88 private:
89  Q_DISABLE_COPY(NetStream)
90 
91  bool Request(const QUrl &url);
92 
93  const int m_id; // Unique request ID
94  const QUrl m_url;
95 
96  mutable QMutex m_mutex; // Protects r/w access to the following data
97  QNetworkRequest m_request;
98  enum { kClosed, kPending, kStarted, kReady, kFinished } m_state {kClosed};
100  QNetworkReply* m_reply {nullptr};
102  qlonglong m_size {-1};
103  qlonglong m_pos {0};
104  QByteArray m_cert;
105  QWaitCondition m_ready;
106  QWaitCondition m_finished;
107 };
108 
109 
113 class NAMThread : public QThread
114 {
115  Q_OBJECT
116 
117  // Use manager() to create
118  NAMThread();
119 
120 public:
121  static NAMThread & manager(); // Singleton
122  ~NAMThread() override;
123 
124  static inline void PostEvent(QEvent *e) { manager().Post(e); }
125  void Post(QEvent *event);
126 
127  static inline QMutex* GetMutex() { return &manager().m_mutexNAM; }
128 
129  static bool isAvailable(); // is network usable
130  static QDateTime GetLastModified(const QUrl &url);
131 
132 signals:
133  void requestStarted(int, QNetworkReply *);
134 
135  // Implementation
136 protected:
137  void run() override; // QThread
138  bool NewRequest(QEvent *event);
140  static bool AbortRequest(NetStreamAbort *p);
141 
142 private slots:
143  void quit();
144 
145 private:
146  Q_DISABLE_COPY(NAMThread)
147 
148  volatile bool m_bQuit {false};
149  QSemaphore m_running;
150  mutable QMutex m_mutexNAM {QMutex::Recursive}; // Provides recursive access to m_nam
151  QNetworkAccessManager *m_nam {nullptr};
152  mutable QMutex m_mutex; // Protects r/w access to the following data
153  QQueue< QEvent * > m_workQ;
154  QWaitCondition m_work;
155 };
156 
157 #endif /* ndef NETSTREAM_H */
NetStream::ReadAll
QByteArray ReadAll()
Definition: netstream.cpp:684
NetStream::slotSslErrors
void slotSslErrors(const QList< QSslError > &errors)
Definition: netstream.cpp:482
e
QDomElement e
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1420
NAMThread::m_bQuit
volatile bool m_bQuit
Definition: netstream.h:148
NetStream::m_ready
QWaitCondition m_ready
Definition: netstream.h:105
NAMThread::m_mutexNAM
QMutex m_mutexNAM
Definition: netstream.h:150
NAMThread::m_nam
QNetworkAccessManager * m_nam
Definition: netstream.h:151
NAMThread::StartRequest
bool StartRequest(NetStreamRequest *p)
Definition: netstream.cpp:875
NetStream::Request
bool Request(const QUrl &url)
Definition: netstream.cpp:163
NetStream::isStarted
bool isStarted() const
Asynchronous interface.
Definition: netstream.cpp:699
NetStream::GetLastModified
static QDateTime GetLastModified(const QUrl &url)
Definition: netstream.cpp:728
NetStream::isFinished
bool isFinished() const
Definition: netstream.cpp:711
NAMThread::m_running
QSemaphore m_running
Definition: netstream.h:149
NAMThread::NewRequest
bool NewRequest(QEvent *event)
Definition: netstream.cpp:858
NetStream::m_pending
NetStreamRequest * m_pending
Definition: netstream.h:99
NetStream::m_finished
QWaitCondition m_finished
Definition: netstream.h:106
NetStream::EMode
EMode
Definition: netstream.h:35
NAMThread::quit
void quit()
Definition: netstream.cpp:846
NAMThread::Post
void Post(QEvent *event)
Definition: netstream.cpp:852
NetStream::WaitTillFinished
bool WaitTillFinished(unsigned long milliseconds)
Definition: netstream.cpp:649
NetStream::Url
const QUrl & Url() const
Definition: netstream.h:51
NetStream::m_request
QNetworkRequest m_request
Definition: netstream.h:97
NetStream::m_reply
QNetworkReply * m_reply
Definition: netstream.h:100
NetStream::m_nRedirections
int m_nRedirections
Definition: netstream.h:101
NAMThread::m_workQ
QQueue< QEvent * > m_workQ
Definition: netstream.h:153
NetStream::m_id
const int m_id
Definition: netstream.h:93
NetStream::kClosed
enum NetStream::@18 kClosed
NAMThread::PostEvent
static void PostEvent(QEvent *e)
Definition: netstream.h:124
NetStream::slotFinished
void slotFinished()
Definition: netstream.cpp:419
NAMThread::GetLastModified
static QDateTime GetLastModified(const QUrl &url)
Definition: netstream.cpp:937
NAMThread::~NAMThread
~NAMThread() override
Definition: netstream.cpp:760
NetStreamAbort
Definition: netstream.cpp:84
NAMThread::isAvailable
static bool isAvailable()
Definition: netstream.cpp:912
NAMThread::NAMThread
NAMThread()
Definition: netstream.cpp:748
NetStream::IsSupported
static bool IsSupported(const QUrl &url)
RingBuffer interface.
Definition: netstream.cpp:522
hardwareprofile.config.p
p
Definition: config.py:33
NetStream::WaitTillReady
bool WaitTillReady(unsigned long milliseconds)
Synchronous interface.
Definition: netstream.cpp:632
NetStream::BytesAvailable
qlonglong BytesAvailable() const
Definition: netstream.cpp:678
NAMThread::run
void run() override
Definition: netstream.cpp:767
NetStream::GetSize
qlonglong GetSize() const
Definition: netstream.cpp:621
NetStream::NetStream
NetStream(const QUrl &url, EMode mode=kPreferCache, QByteArray cert=QByteArray())
Network streaming request.
Definition: netstream.cpp:103
NetStream::GetReadPosition
qlonglong GetReadPosition() const
Definition: netstream.cpp:614
NetStream::Finished
void Finished(QObject *)
NAMThread::requestStarted
void requestStarted(int, QNetworkReply *)
NetStream::safe_read
int safe_read(void *data, unsigned sz, unsigned millisecs=0)
Definition: netstream.cpp:561
NetStream
Stream content from a URI.
Definition: netstream.h:30
NetStream::slotReadyRead
void slotReadyRead()
Definition: netstream.cpp:369
NetStream::kPending
@ kPending
Definition: netstream.h:98
NetStream::GetError
QNetworkReply::NetworkError GetError() const
Definition: netstream.cpp:666
NetStream::isAvailable
static bool isAvailable()
Public helpers.
Definition: netstream.cpp:721
NAMThread::m_mutex
QMutex m_mutex
Definition: netstream.h:152
NAMThread
Thread to process NetStream requests.
Definition: netstream.h:113
NAMThread::GetMutex
static QMutex * GetMutex()
Definition: netstream.h:127
NetStream::IsOpen
bool IsOpen() const
Definition: netstream.cpp:530
NetStream::Seek
qlonglong Seek(qlonglong pos)
Definition: netstream.cpp:593
NAMThread::AbortRequest
static bool AbortRequest(NetStreamAbort *p)
Definition: netstream.cpp:895
NAMThread::m_work
QWaitCondition m_work
Definition: netstream.h:154
NetStream::ReadyRead
void ReadyRead(QObject *)
NetStreamRequest
Definition: netstream.cpp:67
NetStream::kPreferCache
@ kPreferCache
Definition: netstream.h:35
NetStream::kFinished
@ kFinished
Definition: netstream.h:98
NetStream::isReady
bool isReady() const
Definition: netstream.cpp:705
NetStream::kStarted
@ kStarted
Definition: netstream.h:98
NetStream::m_cert
QByteArray m_cert
Definition: netstream.h:104
NAMThread::manager
static NAMThread & manager()
NetworkAccessManager event loop thread.
Definition: netstream.cpp:738
NetStream::m_pos
qlonglong m_pos
Definition: netstream.h:103
NetStream::~NetStream
~NetStream() override
Definition: netstream.cpp:128
NetStream::slotRequestStarted
void slotRequestStarted(int id, QNetworkReply *reply)
Definition: netstream.cpp:283
NetStream::kNeverCache
@ kNeverCache
Definition: netstream.h:35
NetStream::m_mutex
QMutex m_mutex
Definition: netstream.h:96
NetStream::m_url
const QUrl m_url
Definition: netstream.h:94
NetStream::GetErrorString
QString GetErrorString() const
Definition: netstream.cpp:672
NetStream::Abort
void Abort()
Definition: netstream.cpp:536
NetStream::kAlwaysCache
@ kAlwaysCache
Definition: netstream.h:35
NetStream::kReady
@ kReady
Definition: netstream.h:98
NetStream::m_size
qlonglong m_size
Definition: netstream.h:102