|
a/src/mediaserver/cdplugins/netfetch.h |
|
b/src/mediaserver/cdplugins/netfetch.h |
|
... |
|
... |
16 |
*/
|
16 |
*/
|
17 |
|
17 |
|
18 |
#ifndef _MEDIAFETCH_H_INCLUDED_
|
18 |
#ifndef _MEDIAFETCH_H_INCLUDED_
|
19 |
#define _MEDIAFETCH_H_INCLUDED_
|
19 |
#define _MEDIAFETCH_H_INCLUDED_
|
20 |
|
20 |
|
|
|
21 |
#include <functional>
|
|
|
22 |
|
21 |
#include "bufxchange.h"
|
23 |
#include "bufxchange.h"
|
22 |
#include "abuffer.h"
|
24 |
#include "abuffer.h"
|
23 |
|
25 |
|
24 |
//
|
26 |
//
|
25 |
// Wrapper for a network fetch
|
27 |
// Wrapper for a network fetch
|
|
... |
|
... |
30 |
// The end of transfer is marked by pushing an empty buffer on the queue
|
32 |
// The end of transfer is marked by pushing an empty buffer on the queue
|
31 |
//
|
33 |
//
|
32 |
// All methods are supposedly thread-safe
|
34 |
// All methods are supposedly thread-safe
|
33 |
class NetFetch {
|
35 |
class NetFetch {
|
34 |
public:
|
36 |
public:
|
35 |
NetFetch() {}
|
37 |
NetFetch(const std::string& u)
|
|
|
38 |
: _url(u) {
|
|
|
39 |
}
|
36 |
virtual ~NetFetch() {}
|
40 |
virtual ~NetFetch() {}
|
37 |
|
41 |
|
|
|
42 |
virtual const std::string& url() {
|
|
|
43 |
return _url;
|
|
|
44 |
}
|
|
|
45 |
|
38 |
virtual void setTimeout(int secs) = 0;
|
46 |
virtual void setTimeout(int secs) {
|
|
|
47 |
timeoutsecs = secs;
|
|
|
48 |
}
|
39 |
|
49 |
|
40 |
/// Start the transfer to the output queue.
|
50 |
/// Start the transfer to the output queue.
|
41 |
virtual bool start(BufXChange<ABuffer*> *queue, uint64_t offset = 0) = 0;
|
51 |
virtual bool start(BufXChange<ABuffer*> *queue, uint64_t offset = 0) = 0;
|
42 |
|
52 |
|
43 |
// Wait for headers. This allows, e.g. doing stuff depending on
|
53 |
// Wait for headers. This allows, e.g. doing stuff depending on
|
|
... |
|
... |
54 |
// The pointers can be set to zero if no value should be retrieved
|
64 |
// The pointers can be set to zero if no value should be retrieved
|
55 |
enum FetchStatus {FETCH_OK=0, FETCH_RETRYABLE, FETCH_FATAL};
|
65 |
enum FetchStatus {FETCH_OK=0, FETCH_RETRYABLE, FETCH_FATAL};
|
56 |
virtual bool fetchDone(FetchStatus *code, int *http_code) = 0;
|
66 |
virtual bool fetchDone(FetchStatus *code, int *http_code) = 0;
|
57 |
|
67 |
|
58 |
/// Reset after transfer done, for retrying for exemple.
|
68 |
/// Reset after transfer done, for retrying for exemple.
|
59 |
virtual void reset() = 0;
|
69 |
virtual bool reset() = 0;
|
|
|
70 |
|
|
|
71 |
u_int64_t datacount() {
|
|
|
72 |
return fetch_data_count;
|
|
|
73 |
}
|
60 |
|
74 |
|
61 |
// Callbacks
|
75 |
// Callbacks
|
62 |
|
76 |
|
63 |
// A function to create the first buffer (typically for prepending
|
77 |
// A function to create the first buffer (typically for prepending
|
64 |
// a wav header to a raw pcm stream. If set this is called from
|
78 |
// a wav header to a raw pcm stream. If set this is called from
|
65 |
// the first curl write callback, before processing the curl data,
|
79 |
// the first curl write callback, before processing the curl data,
|
66 |
// so this happens at a point where the client may have had a look
|
80 |
// so this happens at a point where the client may have had a look
|
67 |
// at the headers).
|
81 |
// at the headers).
|
68 |
virtual void setBuf1GenCB(std::function<bool(std::string& buf,void*,int)>) {
|
82 |
virtual void setBuf1GenCB(std::function<bool(
|
|
|
83 |
std::string& buf, void*, int)> f) {
|
|
|
84 |
buf1cb = f;
|
69 |
}
|
85 |
}
|
70 |
// Called when the network transfer is done
|
86 |
// Called when the network transfer is done
|
71 |
void setEOFetchCB(std::function<void(bool ok, u_int64_t count)>) {
|
87 |
void setEOFetchCB(std::function<void(bool ok, u_int64_t count)> f) {
|
|
|
88 |
eofcb = f;
|
72 |
}
|
89 |
}
|
73 |
// Called every time we get new data from the remote
|
90 |
// Called every time we get new data from the remote
|
74 |
void setFetchBytesCB(std::function<void(u_int64_t count)>) {
|
91 |
void setFetchBytesCB(std::function<void(u_int64_t count)> f) {
|
|
|
92 |
fbcb = f;
|
75 |
}
|
93 |
}
|
|
|
94 |
|
|
|
95 |
protected:
|
|
|
96 |
size_t databufToQ(const void *contents, size_t bcnt);
|
|
|
97 |
|
|
|
98 |
std::string _url;
|
|
|
99 |
uint64_t startoffset;
|
|
|
100 |
int timeoutsecs{0};
|
|
|
101 |
u_int64_t fetch_data_count{0};
|
|
|
102 |
BufXChange<ABuffer*> *outqueue{nullptr};
|
|
|
103 |
std::function<bool(std::string&, void *, int)> buf1cb;
|
|
|
104 |
std::function<void(u_int64_t)> fbcb;
|
|
|
105 |
std::function<void(bool, u_int64_t)> eofcb;
|
76 |
};
|
106 |
};
|
77 |
|
107 |
|
78 |
#endif /* _MEDIAFETCH_H_INCLUDED_ */
|
108 |
#endif /* _MEDIAFETCH_H_INCLUDED_ */
|