| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2019 |
| 4 |
* Author: Georgios Bitzes <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "ResponseBuffer.hpp" |
| 23 |
#include <string.h> |
| 24 |
#include <iostream> |
| 25 |
|
| 26 |
#define DBG(message) std::cerr << __FILE__ << ":" << __LINE__ << " -- " << #message << " = " << message << std::endl; |
| 27 |
|
| 28 |
namespace Davix { |
| 29 |
|
| 30 |
//------------------------------------------------------------------------------ |
| 31 |
// Constructor |
| 32 |
//------------------------------------------------------------------------------ |
| 33 |
ResponseBuffer::ResponseBuffer(size_t bsize) : bufferSize(bsize), posWrite(0), |
| 34 |
posRead(0) {} |
| 35 |
|
| 36 |
//------------------------------------------------------------------------------ |
| 37 |
// Destructor |
| 38 |
//------------------------------------------------------------------------------ |
| 39 |
ResponseBuffer::~ResponseBuffer() {} |
| 40 |
|
| 41 |
//------------------------------------------------------------------------------ |
| 42 |
// Feed len bytes into the buffer |
| 43 |
//------------------------------------------------------------------------------ |
| 44 |
void ResponseBuffer::feed(const char *buff, size_t len) { |
| 45 |
size_t buffPos = 0; |
| 46 |
|
| 47 |
while(len > 0) { |
| 48 |
if(buffers.size() == 0 || posWrite == bufferSize) { |
| 49 |
buffers.emplace_back(); |
| 50 |
buffers.back().resize(bufferSize); |
| 51 |
posWrite = 0; |
| 52 |
} |
| 53 |
|
| 54 |
size_t bytesToWrite = std::min(len, bufferSize - posWrite); |
| 55 |
|
| 56 |
::memcpy(buffers.back().data()+posWrite, buff+buffPos, bytesToWrite); |
| 57 |
buffPos += bytesToWrite; |
| 58 |
len -= bytesToWrite; |
| 59 |
posWrite += bytesToWrite; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
//------------------------------------------------------------------------------ |
| 64 |
// Consume a maximum of maxlen bytes out of the buffer |
| 65 |
//------------------------------------------------------------------------------ |
| 66 |
size_t ResponseBuffer::consume(char *target, size_t maxlen) { |
| 67 |
size_t bytesDelivered = 0; |
| 68 |
|
| 69 |
while(maxlen > 0) { |
| 70 |
if(buffers.size() == 0) { |
| 71 |
break; |
| 72 |
} |
| 73 |
|
| 74 |
if(buffers.size() == 1 && posRead >= posWrite) { |
| 75 |
break; |
| 76 |
} |
| 77 |
|
| 78 |
if(posRead == bufferSize) { |
| 79 |
buffers.pop_front(); |
| 80 |
posRead = 0; |
| 81 |
} |
| 82 |
|
| 83 |
size_t bytesToCopy = 0; |
| 84 |
if(buffers.size() == 1) { |
| 85 |
bytesToCopy = posWrite - posRead; |
| 86 |
} |
| 87 |
else { |
| 88 |
bytesToCopy = bufferSize - posRead; |
| 89 |
} |
| 90 |
|
| 91 |
bytesToCopy = std::min(maxlen, bytesToCopy); |
| 92 |
::memcpy(target+bytesDelivered, buffers.front().data()+posRead, bytesToCopy); |
| 93 |
|
| 94 |
posRead += bytesToCopy; |
| 95 |
maxlen -= bytesToCopy; |
| 96 |
bytesDelivered += bytesToCopy; |
| 97 |
} |
| 98 |
|
| 99 |
return bytesDelivered; |
| 100 |
} |
| 101 |
|
| 102 |
//------------------------------------------------------------------------------ |
| 103 |
// Check total stored bytes |
| 104 |
//------------------------------------------------------------------------------ |
| 105 |
size_t ResponseBuffer::size() const { |
| 106 |
size_t total = 0; |
| 107 |
|
| 108 |
if(buffers.size() == 0) { |
| 109 |
return total; |
| 110 |
} |
| 111 |
|
| 112 |
total += bufferSize * buffers.size(); |
| 113 |
|
| 114 |
total -= posRead; |
| 115 |
total -= (bufferSize - posWrite); |
| 116 |
return total; |
| 117 |
} |
| 118 |
|
| 119 |
} |