Switch to unified view

a b/src/mediaserver/cdplugins/curlfetch.h
1
#ifndef _CURLFETCH_H_INCLUDED_
2
#define _CURLFETCH_H_INCLUDED_
3
4
#include <stddef.h>
5
#include <stdint.h>
6
7
#include <string>
8
#include <functional>
9
#include <memory>
10
11
#include "bufxchange.h"
12
#include "abuffer.h"
13
14
//
15
// Wrapper for a libcurl transfer. This uses the curl_easy interface
16
// in a separate thread, so any number of transfers may be performed
17
// in parallel.
18
//
19
// The transfer is aborted when the object is deleted, with proper
20
// cleanup (in practise, if the curl thread is currently working
21
// inside curl_easy_transfer, the curl fd is closed to abort any
22
// current waiting).
23
//
24
// The end of transfer is signalled by pushing an empty buffer on the queue
25
//
26
// All methods are supposedly thread-safe
27
class CurlFetch {
28
public:
29
    CurlFetch(const std::string& url);
30
    ~CurlFetch();
31
32
    void setTimeout(int secs);
33
    
34
    /// Start the transfer to the output queue.
35
    bool start(BufXChange<ABuffer*> *queue, uint64_t offset = 0);
36
37
    // Wait for HTTP headers. This allows, e.g. doing stuff depending
38
    // on content-type before proceeding with the actual data transfer
39
    bool waitForHeaders(int maxSecs = 0);
40
    // Retrieve header value (after a successful waitForHeaders).
41
    bool headerValue(const std::string& nm, std::string& val);
42
43
    // Check if the curl thread is done and retrieve the results if it
44
    // is. This does not wait, it returns false if the transfer is
45
    // still running.
46
    bool curlDone(int *curlcode, long *http_code);
47
48
    // Callbacks
49
50
    // A function to create the first buffer (typically for prepending
51
    // a wav header to a raw pcm stream. If set this is called from
52
    // the first curl write callback, before processing the curl data,
53
    // so this happens at a point where the client may have had a look
54
    // at the headers).
55
    void setBuf1GenCB(std::function<bool(std::string& buf,void*,int)>);
56
    // Called after curl_easy_perform returns
57
    void setEOFetchCB(std::function<void(bool ok, u_int64_t count)> eofcb);
58
    // Called every time we get new data from curl
59
    void setFetchBytesCB(std::function<void(u_int64_t count)> fbcb);
60
    
61
    class Internal;
62
private:
63
    std::unique_ptr<Internal> m;
64
};
65
66
#endif /* _CURLFETCH_H_INCLUDED_ */