Switch to unified view

a b/src/mediaserver/cdplugins/netfetch.cpp
1
/* Copyright (C) 2017-2018 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#include "netfetch.h"
18
19
#include "log.h"
20
21
using namespace std;
22
23
#ifndef MAX
24
#define MAX(A,B) ((A) < (B) ? (B) : (A))
25
#endif
26
27
size_t NetFetch::databufToQ(const void *contents, size_t bcnt)
28
{
29
    LOGDEB1("NetFetch::dataBufToQ. bcnt " << bcnt << endl);
30
    
31
    ABuffer *buf = nullptr;
32
    // Try to recover an empty buffer from the queue, else allocate one.
33
    if (outqueue && outqueue->take_recycled(&buf)) {
34
        if (buf->allocbytes < bcnt) {
35
            delete buf;
36
            buf = nullptr;
37
        }
38
    }
39
    if (buf == nullptr) {
40
        buf = new ABuffer(MAX(4096, bcnt));
41
    }
42
    if (buf == nullptr) {
43
        LOGERR("CurlFetch::dataBufToQ: can't get buffer for " << bcnt <<
44
               " bytes\n");
45
        return 0;
46
    }
47
    memcpy(buf->buf, contents, bcnt);
48
    buf->bytes = bcnt;
49
    buf->curoffs = 0;
50
51
    LOGDEB1("NetFetch::calling put on " <<
52
            (outqueue ? outqueue->getname() : "null") << endl);
53
    
54
    if (!outqueue->put(buf)) {
55
        LOGDEB1("CurlFetch::dataBufToQ. queue put failed\n");
56
        delete buf;
57
        return -1;
58
    }
59
60
    fetch_data_count += bcnt;
61
    if (fbcb) {
62
        fbcb(fetch_data_count);
63
    }
64
    LOGDEB1("CurlFetch::dataBufToQ. returning " << bcnt << endl);
65
    return bcnt;
66
}