2020-11-19 10:29:53 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2023-11-09 12:23:00 -06:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2020-11-19 10:29:53 -06:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-11-26 13:12:54 -06:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ostream>
|
2020-11-19 10:29:53 -06:00
|
|
|
#include <vector>
|
|
|
|
|
2020-11-26 13:12:54 -06:00
|
|
|
#include <Util.hpp>
|
|
|
|
|
2020-11-19 10:29:53 -06:00
|
|
|
// Blocks -> we can share from client -> server ... etc.
|
|
|
|
// headers / and a 'writeV' etc. =)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulate data we need to write.
|
|
|
|
*/
|
2020-11-26 13:12:54 -06:00
|
|
|
class Buffer
|
|
|
|
{
|
2021-11-29 13:51:39 -06:00
|
|
|
std::size_t _offset; /// offset into _buffer of data
|
2020-11-19 10:29:53 -06:00
|
|
|
std::vector<char> _buffer;
|
2020-11-26 13:12:54 -06:00
|
|
|
|
2020-11-19 10:29:53 -06:00
|
|
|
public:
|
2021-11-29 13:57:34 -06:00
|
|
|
Buffer() : _offset(0)
|
2020-11-19 10:29:53 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-25 11:05:54 -05:00
|
|
|
typedef std::vector<char>::iterator iterator;
|
|
|
|
typedef std::vector<char>::const_iterator const_iterator;
|
|
|
|
|
2021-11-29 13:57:34 -06:00
|
|
|
std::size_t size() const { return _buffer.size() - _offset; }
|
|
|
|
bool empty() const { return _offset == _buffer.size(); }
|
2020-11-26 13:12:54 -06:00
|
|
|
|
|
|
|
const char *getBlock() const
|
2020-11-19 10:29:53 -06:00
|
|
|
{
|
2021-11-29 13:57:34 -06:00
|
|
|
if (!empty())
|
2020-11-27 08:47:11 -06:00
|
|
|
return &_buffer[_offset];
|
|
|
|
return nullptr;
|
2020-11-19 10:29:53 -06:00
|
|
|
}
|
2020-11-26 13:12:54 -06:00
|
|
|
|
|
|
|
std::size_t getBlockSize() const
|
2020-11-19 10:29:53 -06:00
|
|
|
{
|
2021-11-29 13:57:34 -06:00
|
|
|
return size();
|
2020-11-19 10:29:53 -06:00
|
|
|
}
|
2020-11-26 13:12:54 -06:00
|
|
|
|
|
|
|
void eraseFirst(std::size_t len)
|
2020-11-19 10:29:53 -06:00
|
|
|
{
|
2020-11-27 08:47:11 -06:00
|
|
|
if (len <= 0)
|
|
|
|
return;
|
|
|
|
|
2020-11-19 13:55:30 -06:00
|
|
|
assert(_offset + len <= _buffer.size());
|
2021-11-29 13:57:34 -06:00
|
|
|
assert(_offset + size() == _buffer.size());
|
2020-11-26 13:12:54 -06:00
|
|
|
|
2021-11-29 13:57:34 -06:00
|
|
|
len = std::min(len, size()); // Avoid accidental damage.
|
2020-11-19 13:55:30 -06:00
|
|
|
|
|
|
|
// avoid regular shuffling down larger chunks of data
|
|
|
|
if (_buffer.size() > 16384 && // lots of queued data
|
2021-11-29 13:57:34 -06:00
|
|
|
len < size() && // not a complete erase
|
2020-11-19 13:55:30 -06:00
|
|
|
_offset < 16384 * 64 && // do cleanup a Mb at a time or so:
|
2021-11-29 13:57:34 -06:00
|
|
|
size() > 512) // early cleanup if what remains is small.
|
2020-11-19 13:55:30 -06:00
|
|
|
{
|
|
|
|
_offset += len;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffer.erase(_buffer.begin(), _buffer.begin() + _offset + len);
|
|
|
|
_offset = 0;
|
2020-11-19 10:29:53 -06:00
|
|
|
}
|
2020-11-26 13:12:54 -06:00
|
|
|
|
2020-11-19 10:29:53 -06:00
|
|
|
void append(const char *data, const int len)
|
|
|
|
{
|
|
|
|
_buffer.insert(_buffer.end(), data, data + len);
|
|
|
|
}
|
2020-11-26 13:12:54 -06:00
|
|
|
|
2021-03-20 09:14:39 -05:00
|
|
|
void append(const std::string& s) { append(s.c_str(), s.size()); }
|
|
|
|
|
2021-04-22 22:10:37 -05:00
|
|
|
/// Append a literal string, with compile-time size capturing.
|
|
|
|
template <std::size_t N> void append(const char (&s)[N])
|
|
|
|
{
|
|
|
|
static_assert(N > 1, "Cannot append empty strings.");
|
|
|
|
append(s, N - 1); // Minus null termination.
|
|
|
|
}
|
2021-03-20 09:14:39 -05:00
|
|
|
|
2020-11-26 13:12:54 -06:00
|
|
|
void dumpHex(std::ostream &os, const char *legend, const char *prefix) const
|
2020-11-19 10:29:53 -06:00
|
|
|
{
|
2021-11-29 13:57:34 -06:00
|
|
|
if (size() > 0 || _offset > 0)
|
|
|
|
os << prefix << "Buffer size: " << size() << " offset: " << _offset << '\n';
|
2020-11-19 13:55:30 -06:00
|
|
|
if (_buffer.size() > 0)
|
2021-03-11 12:12:20 -06:00
|
|
|
Util::dumpHex(os, _buffer, legend, prefix);
|
2020-11-19 10:29:53 -06:00
|
|
|
}
|
2021-10-25 11:05:54 -05:00
|
|
|
|
|
|
|
// various std::vector API compatibility functions
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
_buffer.clear();
|
|
|
|
_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator begin() { return _buffer.begin() + _offset; }
|
|
|
|
|
|
|
|
const_iterator begin() const { return _buffer.begin() + _offset; }
|
|
|
|
|
|
|
|
iterator end() { return _buffer.end(); }
|
|
|
|
|
|
|
|
const_iterator end() const { return _buffer.end(); }
|
|
|
|
|
|
|
|
char operator[](int index) const { return _buffer[_offset + index]; }
|
|
|
|
|
|
|
|
char& operator[](int index) { return _buffer[_offset + index]; }
|
|
|
|
|
|
|
|
const char* data() const { return _buffer.data() + _offset; }
|
|
|
|
|
|
|
|
char* data() { return _buffer.data() + _offset; }
|
|
|
|
|
|
|
|
iterator erase(iterator first, iterator last)
|
|
|
|
{
|
|
|
|
if (first == begin())
|
|
|
|
{
|
|
|
|
eraseFirst(last - begin());
|
|
|
|
return begin();
|
|
|
|
}
|
|
|
|
iterator ret = _buffer.erase(first, last);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-19 10:29:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|