mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-13 23:17:03 +03:00
new cspot/bell
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
#include "BellHTTPServer.h"
|
||||
#include <mutex>
|
||||
#include <regex>
|
||||
#include "CivetServer.h"
|
||||
#include "civetweb.h"
|
||||
|
||||
#include <string.h> // for memcpy
|
||||
#include <cassert> // for assert
|
||||
#include <exception> // for exception
|
||||
#include <mutex> // for scoped_lock
|
||||
#include <regex> // for sregex_token_iterator, regex
|
||||
|
||||
#include "BellLogger.h" // for AbstractLogger, BELL_LOG, bell
|
||||
#include "CivetServer.h" // for CivetServer, CivetWebSocketHandler
|
||||
#include "civetweb.h" // for mg_get_request_info, mg_printf, mg_set_user...
|
||||
|
||||
using namespace bell;
|
||||
|
||||
@@ -195,7 +201,8 @@ std::unique_ptr<BellHTTPServer::HTTPResponse> BellHTTPServer::makeJsonResponse(
|
||||
return response;
|
||||
}
|
||||
|
||||
std::unique_ptr<BellHTTPServer::HTTPResponse> BellHTTPServer::makeEmptyResponse() {
|
||||
std::unique_ptr<BellHTTPServer::HTTPResponse>
|
||||
BellHTTPServer::makeEmptyResponse() {
|
||||
auto response = std::make_unique<BellHTTPServer::HTTPResponse>();
|
||||
return response;
|
||||
}
|
||||
@@ -225,8 +232,10 @@ void BellHTTPServer::registerNotFound(HTTPHandler handler) {
|
||||
|
||||
std::unordered_map<std::string, std::string> BellHTTPServer::extractParams(
|
||||
struct mg_connection* conn) {
|
||||
void* data = mg_get_user_connection_data(conn);
|
||||
assert(data != nullptr);
|
||||
std::unordered_map<std::string, std::string>& params =
|
||||
*(std::unordered_map<std::string, std::string>*)
|
||||
mg_get_user_connection_data(conn);
|
||||
*(std::unordered_map<std::string, std::string>*)data;
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
#include "BellTar.h"
|
||||
#include <sys/stat.h>
|
||||
#include <fstream>
|
||||
|
||||
#include <sys/stat.h> // for mkdir
|
||||
|
||||
using namespace bell::BellTar;
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio> // for sprintf, snprintf and sscanf
|
||||
#include <cstdlib> // for rand
|
||||
#include <cstring> // for strlen and memset
|
||||
#include <ctime> // for time
|
||||
#include <algorithm> // for min
|
||||
#include <cassert> // for assert
|
||||
#include <cstdint> // for uint8_t
|
||||
#include <cstdio> // for sprintf, size_t, sscanf, EOF, NULL
|
||||
#include <cstdlib> // for rand
|
||||
#include <cstring> // for memset, strlen
|
||||
#include <ctime> // for time
|
||||
#include <fstream> // for ofstream
|
||||
#include <vector> // for vector
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#endif
|
||||
@@ -59,7 +63,7 @@ void header_set_metadata(tar_header* header) {
|
||||
std::memset(header, 0, sizeof(tar_header));
|
||||
|
||||
std::sprintf(header->magic, "ustar");
|
||||
std::sprintf(header->mtime, "%011lo", (unsigned long) std::time(NULL));
|
||||
std::sprintf(header->mtime, "%011lo", (unsigned long)std::time(NULL));
|
||||
std::sprintf(header->mode, "%07o", 0644);
|
||||
std::sprintf(header->uname, "unkown"); // ... a bit random
|
||||
std::sprintf(header->gname, "users");
|
||||
|
||||
@@ -1,72 +1,69 @@
|
||||
#include "BinaryReader.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdlib.h> // for size_t
|
||||
#include <cstdint> // for uint8_t
|
||||
#include <type_traits> // for remove_extent_t
|
||||
|
||||
#include "ByteStream.h" // for ByteStream
|
||||
|
||||
bell::BinaryReader::BinaryReader(std::shared_ptr<ByteStream> stream) {
|
||||
this->stream = stream;
|
||||
this->stream = stream;
|
||||
}
|
||||
|
||||
size_t bell::BinaryReader::position() {
|
||||
return stream->position();
|
||||
return stream->position();
|
||||
}
|
||||
|
||||
size_t bell::BinaryReader::size() {
|
||||
return stream->size();
|
||||
return stream->size();
|
||||
}
|
||||
|
||||
void bell::BinaryReader::close() {
|
||||
stream->close();
|
||||
stream->close();
|
||||
}
|
||||
|
||||
void bell::BinaryReader::skip(size_t pos) {
|
||||
std::vector<uint8_t> b(pos);
|
||||
stream->read(&b[0], pos);
|
||||
std::vector<uint8_t> b(pos);
|
||||
stream->read(&b[0], pos);
|
||||
}
|
||||
|
||||
int32_t bell::BinaryReader::readInt() {
|
||||
uint8_t b[4];
|
||||
if (stream->read((uint8_t *) b,4) != 4)
|
||||
return 0;
|
||||
|
||||
return static_cast<int32_t>(
|
||||
(b[3]) |
|
||||
(b[2] << 8) |
|
||||
(b[1] << 16)|
|
||||
(b[0] << 24) );
|
||||
uint8_t b[4];
|
||||
if (stream->read((uint8_t*)b, 4) != 4)
|
||||
return 0;
|
||||
|
||||
return static_cast<int32_t>((b[3]) | (b[2] << 8) | (b[1] << 16) |
|
||||
(b[0] << 24));
|
||||
}
|
||||
|
||||
int16_t bell::BinaryReader::readShort() {
|
||||
uint8_t b[2];
|
||||
if (stream->read((uint8_t *) b,2) != 2)
|
||||
return 0;
|
||||
|
||||
return static_cast<int16_t>(
|
||||
(b[1]) |
|
||||
(b[0] << 8));
|
||||
uint8_t b[2];
|
||||
if (stream->read((uint8_t*)b, 2) != 2)
|
||||
return 0;
|
||||
|
||||
return static_cast<int16_t>((b[1]) | (b[0] << 8));
|
||||
}
|
||||
|
||||
|
||||
uint32_t bell::BinaryReader::readUInt() {
|
||||
return readInt() & 0xffffffffL;
|
||||
return readInt() & 0xffffffffL;
|
||||
}
|
||||
|
||||
uint8_t bell::BinaryReader::readByte() {
|
||||
uint8_t b[1];
|
||||
if (stream->read((uint8_t *) b,1) != 1)
|
||||
return 0;
|
||||
return b[0];
|
||||
uint8_t b[1];
|
||||
if (stream->read((uint8_t*)b, 1) != 1)
|
||||
return 0;
|
||||
return b[0];
|
||||
}
|
||||
|
||||
std::vector<uint8_t> bell::BinaryReader::readBytes(size_t size) {
|
||||
std::vector<uint8_t> data(size);
|
||||
stream->read(&data[0], size);
|
||||
return data;
|
||||
std::vector<uint8_t> data(size);
|
||||
stream->read(&data[0], size);
|
||||
return data;
|
||||
}
|
||||
|
||||
long long bell::BinaryReader::readLong() {
|
||||
long high = readInt();
|
||||
long low = readInt();
|
||||
long high = readInt();
|
||||
long low = readInt();
|
||||
|
||||
return static_cast<long long>(
|
||||
((long long) high << 32) | low );
|
||||
return static_cast<long long>(((long long)high << 32) | low);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <BinaryStream.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept> // for runtime_error
|
||||
|
||||
using namespace bell;
|
||||
|
||||
|
||||
@@ -1,172 +1,182 @@
|
||||
#include "BufferedStream.h"
|
||||
#include <cstring>
|
||||
|
||||
BufferedStream::BufferedStream(
|
||||
const std::string &taskName,
|
||||
uint32_t bufferSize,
|
||||
uint32_t readThreshold,
|
||||
uint32_t readSize,
|
||||
uint32_t readyThreshold,
|
||||
uint32_t notReadyThreshold,
|
||||
bool waitForReady)
|
||||
: bell::Task(taskName, 4096, 5, 0) {
|
||||
this->bufferSize = bufferSize;
|
||||
this->readAt = bufferSize - readThreshold;
|
||||
this->readSize = readSize;
|
||||
this->readyThreshold = readyThreshold;
|
||||
this->notReadyThreshold = notReadyThreshold;
|
||||
this->waitForReady = waitForReady;
|
||||
this->buf = static_cast<uint8_t *>(malloc(bufferSize));
|
||||
this->bufEnd = buf + bufferSize;
|
||||
reset();
|
||||
#include <stdlib.h> // for free, malloc
|
||||
#include <algorithm> // for min
|
||||
#include <cstdint> // for uint32_t
|
||||
#include <cstring> // for memcpy
|
||||
#include <type_traits> // for remove_extent_t
|
||||
|
||||
BufferedStream::BufferedStream(const std::string& taskName, uint32_t bufferSize,
|
||||
uint32_t readThreshold, uint32_t readSize,
|
||||
uint32_t readyThreshold,
|
||||
uint32_t notReadyThreshold, bool waitForReady)
|
||||
: bell::Task(taskName, 4096, 5, 0) {
|
||||
this->bufferSize = bufferSize;
|
||||
this->readAt = bufferSize - readThreshold;
|
||||
this->readSize = readSize;
|
||||
this->readyThreshold = readyThreshold;
|
||||
this->notReadyThreshold = notReadyThreshold;
|
||||
this->waitForReady = waitForReady;
|
||||
this->buf = static_cast<uint8_t*>(malloc(bufferSize));
|
||||
this->bufEnd = buf + bufferSize;
|
||||
reset();
|
||||
}
|
||||
|
||||
BufferedStream::~BufferedStream() {
|
||||
this->close();
|
||||
free(buf);
|
||||
this->close();
|
||||
free(buf);
|
||||
}
|
||||
|
||||
void BufferedStream::close() {
|
||||
this->terminate = true;
|
||||
this->readSem.give(); // force a read operation
|
||||
const std::lock_guard lock(runningMutex);
|
||||
if (this->source)
|
||||
this->source->close();
|
||||
this->source = nullptr;
|
||||
this->terminate = true;
|
||||
this->readSem.give(); // force a read operation
|
||||
const std::lock_guard lock(runningMutex);
|
||||
if (this->source)
|
||||
this->source->close();
|
||||
this->source = nullptr;
|
||||
}
|
||||
|
||||
void BufferedStream::reset() {
|
||||
this->bufReadPtr = this->buf;
|
||||
this->bufWritePtr = this->buf;
|
||||
this->readTotal = 0;
|
||||
this->bufferTotal = 0;
|
||||
this->readAvailable = 0;
|
||||
this->terminate = false;
|
||||
this->bufReadPtr = this->buf;
|
||||
this->bufWritePtr = this->buf;
|
||||
this->readTotal = 0;
|
||||
this->bufferTotal = 0;
|
||||
this->readAvailable = 0;
|
||||
this->terminate = false;
|
||||
}
|
||||
|
||||
bool BufferedStream::open(const std::shared_ptr<bell::ByteStream> &stream) {
|
||||
if (this->running)
|
||||
this->close();
|
||||
reset();
|
||||
this->source = stream;
|
||||
startTask();
|
||||
return source.get();
|
||||
bool BufferedStream::open(const std::shared_ptr<bell::ByteStream>& stream) {
|
||||
if (this->running)
|
||||
this->close();
|
||||
reset();
|
||||
this->source = stream;
|
||||
startTask();
|
||||
return source.get();
|
||||
}
|
||||
|
||||
bool BufferedStream::open(const StreamReader &newReader, uint32_t initialOffset) {
|
||||
if (this->running)
|
||||
this->close();
|
||||
reset();
|
||||
this->reader = newReader;
|
||||
this->bufferTotal = initialOffset;
|
||||
startTask();
|
||||
return source.get();
|
||||
bool BufferedStream::open(const StreamReader& newReader,
|
||||
uint32_t initialOffset) {
|
||||
if (this->running)
|
||||
this->close();
|
||||
reset();
|
||||
this->reader = newReader;
|
||||
this->bufferTotal = initialOffset;
|
||||
startTask();
|
||||
return source.get();
|
||||
}
|
||||
|
||||
bool BufferedStream::isReady() const {
|
||||
return readAvailable >= readyThreshold;
|
||||
return readAvailable >= readyThreshold;
|
||||
}
|
||||
|
||||
bool BufferedStream::isNotReady() const {
|
||||
return readAvailable < notReadyThreshold;
|
||||
return readAvailable < notReadyThreshold;
|
||||
}
|
||||
|
||||
size_t BufferedStream::skip(size_t len) {
|
||||
return read(nullptr, len);
|
||||
return read(nullptr, len);
|
||||
}
|
||||
|
||||
size_t BufferedStream::position() {
|
||||
return readTotal;
|
||||
return readTotal;
|
||||
}
|
||||
|
||||
size_t BufferedStream::size() {
|
||||
return source->size();
|
||||
return source->size();
|
||||
}
|
||||
|
||||
uint32_t BufferedStream::lengthBetween(uint8_t *me, uint8_t *other) {
|
||||
const std::lock_guard lock(readMutex);
|
||||
if (other <= me) {
|
||||
// buf .... other ...... me ........ bufEnd
|
||||
// buf .... me/other ........ bufEnd
|
||||
return bufEnd - me;
|
||||
} else {
|
||||
// buf ........ me ........ other .... bufEnd
|
||||
return other - me;
|
||||
}
|
||||
uint32_t BufferedStream::lengthBetween(uint8_t* me, uint8_t* other) {
|
||||
const std::lock_guard lock(readMutex);
|
||||
if (other <= me) {
|
||||
// buf .... other ...... me ........ bufEnd
|
||||
// buf .... me/other ........ bufEnd
|
||||
return bufEnd - me;
|
||||
} else {
|
||||
// buf ........ me ........ other .... bufEnd
|
||||
return other - me;
|
||||
}
|
||||
}
|
||||
|
||||
size_t BufferedStream::read(uint8_t *dst, size_t len) {
|
||||
if (waitForReady && isNotReady()) {
|
||||
while ((source || reader) && !isReady()) {} // end waiting after termination
|
||||
}
|
||||
if (!running && !readAvailable) {
|
||||
reset();
|
||||
return 0;
|
||||
}
|
||||
uint32_t read = 0;
|
||||
uint32_t toReadTotal = std::min(readAvailable.load(), static_cast<uint32_t>(len));
|
||||
while (toReadTotal > 0) {
|
||||
uint32_t toRead = std::min(toReadTotal, lengthBetween(bufReadPtr, bufWritePtr));
|
||||
if (dst) {
|
||||
memcpy(dst, bufReadPtr, toRead);
|
||||
dst += toRead;
|
||||
}
|
||||
readAvailable -= toRead;
|
||||
bufReadPtr += toRead;
|
||||
if (bufReadPtr >= bufEnd)
|
||||
bufReadPtr = buf;
|
||||
toReadTotal -= toRead;
|
||||
read += toRead;
|
||||
readTotal += toRead;
|
||||
}
|
||||
this->readSem.give();
|
||||
return read;
|
||||
size_t BufferedStream::read(uint8_t* dst, size_t len) {
|
||||
if (waitForReady && isNotReady()) {
|
||||
while ((source || reader) && !isReady()) {
|
||||
} // end waiting after termination
|
||||
}
|
||||
if (!running && !readAvailable) {
|
||||
reset();
|
||||
return 0;
|
||||
}
|
||||
uint32_t read = 0;
|
||||
uint32_t toReadTotal =
|
||||
std::min(readAvailable.load(), static_cast<uint32_t>(len));
|
||||
while (toReadTotal > 0) {
|
||||
uint32_t toRead =
|
||||
std::min(toReadTotal, lengthBetween(bufReadPtr, bufWritePtr));
|
||||
if (dst) {
|
||||
memcpy(dst, bufReadPtr, toRead);
|
||||
dst += toRead;
|
||||
}
|
||||
readAvailable -= toRead;
|
||||
bufReadPtr += toRead;
|
||||
if (bufReadPtr >= bufEnd)
|
||||
bufReadPtr = buf;
|
||||
toReadTotal -= toRead;
|
||||
read += toRead;
|
||||
readTotal += toRead;
|
||||
}
|
||||
this->readSem.give();
|
||||
return read;
|
||||
}
|
||||
|
||||
void BufferedStream::runTask() {
|
||||
const std::lock_guard lock(runningMutex);
|
||||
running = true;
|
||||
if (!source && reader) {
|
||||
// get the initial request on the task's thread
|
||||
source = reader(this->bufferTotal);
|
||||
}
|
||||
while (!terminate) {
|
||||
if (!source)
|
||||
break;
|
||||
if (isReady()) {
|
||||
// buffer ready, wait for any read operations
|
||||
this->readSem.wait();
|
||||
}
|
||||
if (terminate)
|
||||
break;
|
||||
if (readAvailable > readAt)
|
||||
continue;
|
||||
// here, the buffer needs re-filling
|
||||
uint32_t len;
|
||||
bool wasReady = isReady();
|
||||
do {
|
||||
uint32_t toRead = std::min(readSize, lengthBetween(bufWritePtr, bufReadPtr));
|
||||
if (!source) {
|
||||
len = 0;
|
||||
break;
|
||||
}
|
||||
len = source->read(bufWritePtr, toRead);
|
||||
readAvailable += len;
|
||||
bufferTotal += len;
|
||||
bufWritePtr += len;
|
||||
if (bufWritePtr >= bufEnd) // TODO is == enough here?
|
||||
bufWritePtr = buf;
|
||||
} while (len && readSize < bufferSize - readAvailable); // loop until there's no more free space in the buffer
|
||||
if (!len && reader)
|
||||
source = reader(bufferTotal);
|
||||
else if (!len)
|
||||
terminate = true;
|
||||
// signal that buffer is ready for reading
|
||||
if (!wasReady && isReady()) {
|
||||
this->readySem.give();
|
||||
}
|
||||
}
|
||||
source = nullptr;
|
||||
reader = nullptr;
|
||||
running = false;
|
||||
const std::lock_guard lock(runningMutex);
|
||||
running = true;
|
||||
if (!source && reader) {
|
||||
// get the initial request on the task's thread
|
||||
source = reader(this->bufferTotal);
|
||||
}
|
||||
while (!terminate) {
|
||||
if (!source)
|
||||
break;
|
||||
if (isReady()) {
|
||||
// buffer ready, wait for any read operations
|
||||
this->readSem.wait();
|
||||
}
|
||||
if (terminate)
|
||||
break;
|
||||
if (readAvailable > readAt)
|
||||
continue;
|
||||
// here, the buffer needs re-filling
|
||||
uint32_t len;
|
||||
bool wasReady = isReady();
|
||||
do {
|
||||
uint32_t toRead =
|
||||
std::min(readSize, lengthBetween(bufWritePtr, bufReadPtr));
|
||||
if (!source) {
|
||||
len = 0;
|
||||
break;
|
||||
}
|
||||
len = source->read(bufWritePtr, toRead);
|
||||
readAvailable += len;
|
||||
bufferTotal += len;
|
||||
bufWritePtr += len;
|
||||
if (bufWritePtr >= bufEnd) // TODO is == enough here?
|
||||
bufWritePtr = buf;
|
||||
} while (
|
||||
len &&
|
||||
readSize <
|
||||
bufferSize -
|
||||
readAvailable); // loop until there's no more free space in the buffer
|
||||
if (!len && reader)
|
||||
source = reader(bufferTotal);
|
||||
else if (!len)
|
||||
terminate = true;
|
||||
// signal that buffer is ready for reading
|
||||
if (!wasReady && isReady()) {
|
||||
this->readySem.give();
|
||||
}
|
||||
}
|
||||
source = nullptr;
|
||||
reader = nullptr;
|
||||
running = false;
|
||||
}
|
||||
|
||||
@@ -1,89 +1,85 @@
|
||||
#include "CircularBuffer.h"
|
||||
|
||||
#include <algorithm> // for min
|
||||
|
||||
using namespace bell;
|
||||
|
||||
CircularBuffer::CircularBuffer(size_t dataCapacity)
|
||||
{
|
||||
this->dataCapacity = dataCapacity;
|
||||
buffer = std::vector<uint8_t>(dataCapacity);
|
||||
this->dataSemaphore = std::make_unique<bell::WrappedSemaphore>(5);
|
||||
CircularBuffer::CircularBuffer(size_t dataCapacity) {
|
||||
this->dataCapacity = dataCapacity;
|
||||
buffer = std::vector<uint8_t>(dataCapacity);
|
||||
this->dataSemaphore = std::make_unique<bell::WrappedSemaphore>(5);
|
||||
};
|
||||
|
||||
size_t CircularBuffer::write(const uint8_t *data, size_t bytes)
|
||||
{
|
||||
if (bytes == 0)
|
||||
return 0;
|
||||
size_t CircularBuffer::write(const uint8_t* data, size_t bytes) {
|
||||
if (bytes == 0)
|
||||
return 0;
|
||||
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
size_t bytesToWrite = std::min(bytes, dataCapacity - dataSize);
|
||||
// Write in a single step
|
||||
if (bytesToWrite <= dataCapacity - endIndex)
|
||||
{
|
||||
memcpy(buffer.data() + endIndex, data, bytesToWrite);
|
||||
endIndex += bytesToWrite;
|
||||
if (endIndex == dataCapacity)
|
||||
endIndex = 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
size_t bytesToWrite = std::min(bytes, dataCapacity - dataSize);
|
||||
// Write in a single step
|
||||
if (bytesToWrite <= dataCapacity - endIndex) {
|
||||
memcpy(buffer.data() + endIndex, data, bytesToWrite);
|
||||
endIndex += bytesToWrite;
|
||||
if (endIndex == dataCapacity)
|
||||
endIndex = 0;
|
||||
}
|
||||
|
||||
// Write in two steps
|
||||
else {
|
||||
size_t firstChunkSize = dataCapacity - endIndex;
|
||||
memcpy(buffer.data() + endIndex, data, firstChunkSize);
|
||||
size_t secondChunkSize = bytesToWrite - firstChunkSize;
|
||||
memcpy(buffer.data(), data + firstChunkSize, secondChunkSize);
|
||||
endIndex = secondChunkSize;
|
||||
}
|
||||
// Write in two steps
|
||||
else {
|
||||
size_t firstChunkSize = dataCapacity - endIndex;
|
||||
memcpy(buffer.data() + endIndex, data, firstChunkSize);
|
||||
size_t secondChunkSize = bytesToWrite - firstChunkSize;
|
||||
memcpy(buffer.data(), data + firstChunkSize, secondChunkSize);
|
||||
endIndex = secondChunkSize;
|
||||
}
|
||||
|
||||
dataSize += bytesToWrite;
|
||||
dataSize += bytesToWrite;
|
||||
|
||||
// this->dataSemaphore->give();
|
||||
return bytesToWrite;
|
||||
// this->dataSemaphore->give();
|
||||
return bytesToWrite;
|
||||
}
|
||||
|
||||
void CircularBuffer::emptyBuffer() {
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
begIndex = 0;
|
||||
dataSize = 0;
|
||||
endIndex = 0;
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
begIndex = 0;
|
||||
dataSize = 0;
|
||||
endIndex = 0;
|
||||
}
|
||||
|
||||
void CircularBuffer::emptyExcept(size_t sizeToSet) {
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
if (sizeToSet > dataSize)
|
||||
sizeToSet = dataSize;
|
||||
dataSize = sizeToSet;
|
||||
endIndex = begIndex + sizeToSet;
|
||||
if (endIndex > dataCapacity) {
|
||||
endIndex -= dataCapacity;
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
if (sizeToSet > dataSize)
|
||||
sizeToSet = dataSize;
|
||||
dataSize = sizeToSet;
|
||||
endIndex = begIndex + sizeToSet;
|
||||
if (endIndex > dataCapacity) {
|
||||
endIndex -= dataCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
size_t CircularBuffer::read(uint8_t *data, size_t bytes)
|
||||
{
|
||||
if (bytes == 0)
|
||||
return 0;
|
||||
size_t CircularBuffer::read(uint8_t* data, size_t bytes) {
|
||||
if (bytes == 0)
|
||||
return 0;
|
||||
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
size_t bytesToRead = std::min(bytes, dataSize);
|
||||
std::lock_guard<std::mutex> guard(bufferMutex);
|
||||
size_t bytesToRead = std::min(bytes, dataSize);
|
||||
|
||||
// Read in a single step
|
||||
if (bytesToRead <= dataCapacity - begIndex)
|
||||
{
|
||||
memcpy(data, buffer.data() + begIndex, bytesToRead);
|
||||
begIndex += bytesToRead;
|
||||
if (begIndex == dataCapacity)
|
||||
begIndex = 0;
|
||||
}
|
||||
// Read in two steps
|
||||
else
|
||||
{
|
||||
size_t firstChunkSize = dataCapacity - begIndex;
|
||||
memcpy(data, buffer.data() + begIndex, firstChunkSize);
|
||||
size_t secondChunkSize = bytesToRead - firstChunkSize;
|
||||
memcpy(data + firstChunkSize, buffer.data(), secondChunkSize);
|
||||
begIndex = secondChunkSize;
|
||||
}
|
||||
// Read in a single step
|
||||
if (bytesToRead <= dataCapacity - begIndex) {
|
||||
memcpy(data, buffer.data() + begIndex, bytesToRead);
|
||||
begIndex += bytesToRead;
|
||||
if (begIndex == dataCapacity)
|
||||
begIndex = 0;
|
||||
}
|
||||
// Read in two steps
|
||||
else {
|
||||
size_t firstChunkSize = dataCapacity - begIndex;
|
||||
memcpy(data, buffer.data() + begIndex, firstChunkSize);
|
||||
size_t secondChunkSize = bytesToRead - firstChunkSize;
|
||||
memcpy(data + firstChunkSize, buffer.data(), secondChunkSize);
|
||||
begIndex = secondChunkSize;
|
||||
}
|
||||
|
||||
dataSize -= bytesToRead;
|
||||
return bytesToRead;
|
||||
dataSize -= bytesToRead;
|
||||
return bytesToRead;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
#include "EncodedAudioStream.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h> // for memcpy, memmove
|
||||
#include <stdexcept> // for runtime_error
|
||||
#include <type_traits> // for remove_extent_t
|
||||
#include <utility> // for move
|
||||
|
||||
#include "BellLogger.h" // for AbstractLogger, BELL_LOG, bell
|
||||
#include "ByteStream.h" // for ByteStream
|
||||
#include "DecoderGlobals.h" // for DecodersInstance, decodersInstance, AAC_...
|
||||
|
||||
using namespace bell;
|
||||
|
||||
EncodedAudioStream::EncodedAudioStream() {
|
||||
@@ -171,5 +179,4 @@ void EncodedAudioStream::guessDataFormat() {
|
||||
}
|
||||
}
|
||||
|
||||
void EncodedAudioStream::readFully(uint8_t* dst, size_t nbytes) {
|
||||
}
|
||||
void EncodedAudioStream::readFully(uint8_t* dst, size_t nbytes) {}
|
||||
|
||||
@@ -1,70 +1,61 @@
|
||||
#include "FileStream.h"
|
||||
|
||||
#include <stdexcept> // for runtime_error
|
||||
|
||||
#include "BellLogger.h" // for bell
|
||||
|
||||
using namespace bell;
|
||||
|
||||
FileStream::FileStream(const std::string& path, std::string read)
|
||||
{
|
||||
file = fopen(path.c_str(), "rb");
|
||||
if (file == NULL)
|
||||
{
|
||||
throw std::runtime_error("Could not open file: " + path);
|
||||
}
|
||||
FileStream::FileStream(const std::string& path, std::string read) {
|
||||
file = fopen(path.c_str(), "rb");
|
||||
if (file == NULL) {
|
||||
throw std::runtime_error("Could not open file: " + path);
|
||||
}
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
close();
|
||||
FileStream::~FileStream() {
|
||||
close();
|
||||
}
|
||||
|
||||
size_t FileStream::read(uint8_t *buf, size_t nbytes)
|
||||
{
|
||||
if (file == NULL)
|
||||
{
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
size_t FileStream::read(uint8_t* buf, size_t nbytes) {
|
||||
if (file == NULL) {
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
|
||||
return fread(buf, 1, nbytes, file);
|
||||
return fread(buf, 1, nbytes, file);
|
||||
}
|
||||
|
||||
size_t FileStream::skip(size_t nbytes)
|
||||
{
|
||||
if (file == NULL)
|
||||
{
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
size_t FileStream::skip(size_t nbytes) {
|
||||
if (file == NULL) {
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
|
||||
return fseek(file, nbytes, SEEK_CUR);
|
||||
return fseek(file, nbytes, SEEK_CUR);
|
||||
}
|
||||
|
||||
size_t FileStream::position()
|
||||
{
|
||||
if (file == NULL)
|
||||
{
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
size_t FileStream::position() {
|
||||
if (file == NULL) {
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
|
||||
return ftell(file);
|
||||
return ftell(file);
|
||||
}
|
||||
|
||||
size_t FileStream::size()
|
||||
{
|
||||
if (file == NULL)
|
||||
{
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
size_t FileStream::size() {
|
||||
if (file == NULL) {
|
||||
throw std::runtime_error("Stream is closed");
|
||||
}
|
||||
|
||||
size_t pos = ftell(file);
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t size = ftell(file);
|
||||
fseek(file, pos, SEEK_SET);
|
||||
return size;
|
||||
size_t pos = ftell(file);
|
||||
fseek(file, 0, SEEK_END);
|
||||
size_t size = ftell(file);
|
||||
fseek(file, pos, SEEK_SET);
|
||||
return size;
|
||||
}
|
||||
|
||||
void FileStream::close()
|
||||
{
|
||||
if (file != NULL)
|
||||
{
|
||||
fclose(file);
|
||||
file = NULL;
|
||||
}
|
||||
void FileStream::close() {
|
||||
if (file != NULL) {
|
||||
fclose(file);
|
||||
file = NULL;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,14 @@
|
||||
#include "HTTPClient.h"
|
||||
|
||||
#include <string.h> // for memcpy
|
||||
#include <algorithm> // for transform
|
||||
#include <cassert> // for assert
|
||||
#include <cctype> // for tolower
|
||||
#include <ostream> // for operator<<, basic_ostream
|
||||
#include <stdexcept> // for runtime_error
|
||||
|
||||
#include "BellSocket.h" // for bell
|
||||
|
||||
using namespace bell;
|
||||
|
||||
void HTTPClient::Response::connect(const std::string& url) {
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
#include "SocketStream.h"
|
||||
|
||||
#include <stdint.h> // for uint8_t
|
||||
#include <cstdio> // for NULL, ssize_t
|
||||
|
||||
#include "TCPSocket.h" // for TCPSocket
|
||||
#include "TLSSocket.h" // for TLSSocket
|
||||
|
||||
using namespace bell;
|
||||
|
||||
int SocketBuffer::open(const std::string& hostname, int port, bool isSSL) {
|
||||
if (internalSocket != nullptr) { close(); }
|
||||
if (internalSocket != nullptr) {
|
||||
close();
|
||||
}
|
||||
if (isSSL) {
|
||||
internalSocket = std::make_unique<bell::TLSSocket>();
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
#include "TLSSocket.h"
|
||||
#include "X509Bundle.h"
|
||||
|
||||
#include <mbedtls/ctr_drbg.h> // for mbedtls_ctr_drbg_free, mbedtls_ctr_...
|
||||
#include <mbedtls/entropy.h> // for mbedtls_entropy_free, mbedtls_entro...
|
||||
#include <mbedtls/net_sockets.h> // for mbedtls_net_connect, mbedtls_net_free
|
||||
#include <mbedtls/ssl.h> // for mbedtls_ssl_conf_authmode, mbedtls_...
|
||||
#include <cstring> // for strlen, NULL
|
||||
#include <stdexcept> // for runtime_error
|
||||
|
||||
#include "BellLogger.h" // for AbstractLogger, BELL_LOG
|
||||
#include "X509Bundle.h" // for shouldVerify, attach
|
||||
|
||||
/**
|
||||
* Platform TLSSocket implementation for the mbedtls
|
||||
|
||||
@@ -4,40 +4,50 @@ namespace bell {
|
||||
|
||||
#ifdef BELL_DISABLE_REGEX
|
||||
void URLParser::parse(const char* url, std::vector<std::string>& match) {
|
||||
match[0] = url;
|
||||
char scratch[512];
|
||||
match[0] = url;
|
||||
char scratch[512];
|
||||
|
||||
/* Parsing the following (http|https://[host][/path][?query]#hash] as in regex
|
||||
/* Parsing the following (http|https://[host][/path][?query]#hash] as in regex
|
||||
* below. This needs to be changed if you update that regex */
|
||||
|
||||
// get the schema
|
||||
if (sscanf(url, "%[^:]:/", scratch) > 0) match[1] = scratch;
|
||||
|
||||
// get the host
|
||||
if (sscanf(url, "htt%*[^:]://%512[^/#?]", scratch) > 0) match[2] = scratch;
|
||||
|
||||
// get the path
|
||||
url = strstr(url, match[2].c_str()) + match[2].size();
|
||||
if (sscanf(url, "/%512[^?]", scratch) > 0) match[3] = scratch;
|
||||
else if (*url && *url != '?' && *url != '#') url++;
|
||||
|
||||
// get the query
|
||||
if (match[3].size()) url += match[3].size() + 1;
|
||||
if (sscanf(url, "?%512[^#]", scratch) > 0) match[4] = scratch;
|
||||
// get the schema
|
||||
if (sscanf(url, "%[^:]:/", scratch) > 0)
|
||||
match[1] = scratch;
|
||||
|
||||
// get the hash
|
||||
if (match[4].size()) url += match[4].size() + 1;
|
||||
if (sscanf(url, "#%512s", scratch) > 0) match[5] = scratch;
|
||||
// get the host
|
||||
if (sscanf(url, "htt%*[^:]://%512[^/#?]", scratch) > 0)
|
||||
match[2] = scratch;
|
||||
|
||||
// fix the acquired items
|
||||
match[3] = "/" + match[3];
|
||||
if (match[4].size()) match[4] = "?" + match[4];
|
||||
// get the path
|
||||
url = strstr(url, match[2].c_str()) + match[2].size();
|
||||
if (sscanf(url, "/%512[^?]", scratch) > 0)
|
||||
match[3] = scratch;
|
||||
else if (*url && *url != '?' && *url != '#')
|
||||
url++;
|
||||
|
||||
// need at least schema and host
|
||||
if (match[1].size() == 0 || match[2].size() == 0) match.clear();
|
||||
}
|
||||
#else
|
||||
// get the query
|
||||
if (match[3].size())
|
||||
url += match[3].size() + 1;
|
||||
if (sscanf(url, "?%512[^#]", scratch) > 0)
|
||||
match[4] = scratch;
|
||||
|
||||
// get the hash
|
||||
if (match[4].size())
|
||||
url += match[4].size() + 1;
|
||||
if (sscanf(url, "#%512s", scratch) > 0)
|
||||
match[5] = scratch;
|
||||
|
||||
// fix the acquired items
|
||||
match[3] = "/" + match[3];
|
||||
if (match[4].size())
|
||||
match[4] = "?" + match[4];
|
||||
|
||||
// need at least schema and host
|
||||
if (match[1].size() == 0 || match[2].size() == 0)
|
||||
match.clear();
|
||||
}
|
||||
#else
|
||||
const std::regex URLParser::urlParseRegex = std::regex(
|
||||
"^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\\?(?:[^#]*))?(#(?:.*))?");
|
||||
"^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\\?(?:[^#]*))?(#(?:.*))?");
|
||||
#endif
|
||||
}
|
||||
} // namespace bell
|
||||
|
||||
@@ -1,5 +1,15 @@
|
||||
#include "X509Bundle.h"
|
||||
|
||||
#include <mbedtls/md.h> // for mbedtls_md, mbedtls_md_get_size
|
||||
#include <mbedtls/pk.h> // for mbedtls_pk_can_do, mbedtls_pk_pa...
|
||||
#include <mbedtls/ssl.h> // for mbedtls_ssl_conf_ca_chain, mbedt...
|
||||
#include <mbedtls/x509.h> // for mbedtls_x509_buf, MBEDTLS_ERR_X5...
|
||||
#include <stdlib.h> // for free, calloc
|
||||
#include <string.h> // for memcmp, memcpy
|
||||
#include <stdexcept> // for runtime_error
|
||||
|
||||
#include "BellLogger.h" // for AbstractLogger, BELL_LOG
|
||||
|
||||
using namespace bell::X509Bundle;
|
||||
|
||||
static mbedtls_x509_crt s_dummy_crt;
|
||||
@@ -21,7 +31,8 @@ int bell::X509Bundle::crtCheckCertificate(mbedtls_x509_crt* child,
|
||||
|
||||
if ((ret = mbedtls_pk_parse_public_key(&parent.pk, pub_key_buf,
|
||||
pub_key_len)) != 0) {
|
||||
BELL_LOG(error, TAG, "PK parse failed with error %X", ret);
|
||||
BELL_LOG(error, TAG, "PK parse failed with error 0x%04x, key len = %d", ret,
|
||||
pub_key_len);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
@@ -110,6 +121,8 @@ int bell::X509Bundle::crtVerifyCallback(void* buf, mbedtls_x509_crt* crt,
|
||||
ret = crtCheckCertificate(
|
||||
child, s_crt_bundle.crts[middle] + CRT_HEADER_OFFSET + name_len,
|
||||
key_len);
|
||||
} else {
|
||||
BELL_LOG(error, TAG, "Certificate not found in bundle");
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
@@ -138,10 +151,13 @@ void bell::X509Bundle::init(const uint8_t* x509_bundle, size_t bundle_size) {
|
||||
throw std::runtime_error("Unable to allocate memory for bundle");
|
||||
}
|
||||
|
||||
bundleBytes.resize(bundle_size);
|
||||
memcpy(bundleBytes.data(), x509_bundle, bundle_size);
|
||||
|
||||
const uint8_t* cur_crt;
|
||||
/* This is the maximum region that is allowed to access */
|
||||
const uint8_t* bundle_end = x509_bundle + bundle_size;
|
||||
cur_crt = x509_bundle + BUNDLE_HEADER_OFFSET;
|
||||
const uint8_t* bundle_end = bundleBytes.data() + bundle_size;
|
||||
cur_crt = bundleBytes.data() + BUNDLE_HEADER_OFFSET;
|
||||
|
||||
for (int i = 0; i < num_certs; i++) {
|
||||
crts[i] = cur_crt;
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <BellLogger.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include "CivetServer.h"
|
||||
#include "civetweb.h"
|
||||
#include <BellLogger.h> // for bell
|
||||
#include <stdint.h> // for uint8_t
|
||||
#include <stdlib.h> // for free, size_t
|
||||
#include <functional> // for function
|
||||
#include <map> // for map
|
||||
#include <memory> // for unique_ptr
|
||||
#include <mutex> // for mutex
|
||||
#include <string> // for string, hash, operator==, operator<
|
||||
#include <unordered_map> // for unordered_map
|
||||
#include <utility> // for pair
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "CivetServer.h" // for CivetServer, CivetHandler
|
||||
|
||||
using namespace bell;
|
||||
namespace bell {
|
||||
@@ -46,7 +42,9 @@ class BellHTTPServer : public CivetHandler {
|
||||
}
|
||||
}
|
||||
};
|
||||
typedef std::function<std::unique_ptr<HTTPResponse>(struct mg_connection* conn)> HTTPHandler;
|
||||
typedef std::function<std::unique_ptr<HTTPResponse>(
|
||||
struct mg_connection* conn)>
|
||||
HTTPHandler;
|
||||
typedef std::function<void(struct mg_connection* conn, WSState)>
|
||||
WSStateHandler;
|
||||
typedef std::function<void(struct mg_connection* conn, char*, size_t)>
|
||||
@@ -79,7 +77,8 @@ class BellHTTPServer : public CivetHandler {
|
||||
std::vector<int> getListeningPorts() { return server->getListeningPorts(); };
|
||||
void close() { server->close(); }
|
||||
|
||||
std::unique_ptr<HTTPResponse> makeJsonResponse(const std::string& json, int status = 200);
|
||||
std::unique_ptr<HTTPResponse> makeJsonResponse(const std::string& json,
|
||||
int status = 200);
|
||||
std::unique_ptr<HTTPResponse> makeEmptyResponse();
|
||||
|
||||
void registerNotFound(HTTPHandler handler);
|
||||
@@ -88,7 +87,8 @@ class BellHTTPServer : public CivetHandler {
|
||||
void registerWS(const std::string&, WSDataHandler dataHandler,
|
||||
WSStateHandler stateHandler);
|
||||
|
||||
static std::unordered_map<std::string, std::string> extractParams(struct mg_connection* conn);
|
||||
static std::unordered_map<std::string, std::string> extractParams(
|
||||
struct mg_connection* conn);
|
||||
|
||||
private:
|
||||
std::unique_ptr<CivetServer> server;
|
||||
|
||||
@@ -14,6 +14,6 @@ class Socket {
|
||||
virtual size_t read(uint8_t* buf, size_t len) = 0;
|
||||
virtual bool isOpen() = 0;
|
||||
virtual void close() = 0;
|
||||
virtual int getFd() = 0;
|
||||
};
|
||||
} // namespace bell
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream> // for istream, ostream
|
||||
#include <string> // for string
|
||||
|
||||
namespace bell::BellTar {
|
||||
typedef long long unsigned file_size_t;
|
||||
|
||||
@@ -1,31 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include "ByteStream.h"
|
||||
#include <stdint.h> // for uint8_t, int16_t, int32_t, uint32_t
|
||||
#include <stdlib.h> // for size_t
|
||||
#include <memory> // for shared_ptr
|
||||
#include <vector> // for vector
|
||||
|
||||
namespace bell
|
||||
{
|
||||
class BinaryReader
|
||||
{
|
||||
std::shared_ptr<ByteStream> stream;
|
||||
size_t currentPos = 0;
|
||||
namespace bell {
|
||||
class ByteStream;
|
||||
|
||||
public:
|
||||
BinaryReader(std::shared_ptr<ByteStream> stream);
|
||||
int32_t readInt();
|
||||
int16_t readShort();
|
||||
uint32_t readUInt();
|
||||
long long readLong();
|
||||
void close();
|
||||
uint8_t readByte();
|
||||
size_t size();
|
||||
size_t position();
|
||||
std::vector<uint8_t> readBytes(size_t);
|
||||
void skip(size_t);
|
||||
};
|
||||
}
|
||||
class BinaryReader {
|
||||
std::shared_ptr<ByteStream> stream;
|
||||
size_t currentPos = 0;
|
||||
|
||||
public:
|
||||
BinaryReader(std::shared_ptr<ByteStream> stream);
|
||||
int32_t readInt();
|
||||
int16_t readShort();
|
||||
uint32_t readUInt();
|
||||
long long readLong();
|
||||
void close();
|
||||
uint8_t readByte();
|
||||
size_t size();
|
||||
size_t position();
|
||||
std::vector<uint8_t> readBytes(size_t);
|
||||
void skip(size_t);
|
||||
};
|
||||
} // namespace bell
|
||||
@@ -1,10 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef ESP_PLATFORM
|
||||
#include <bit>
|
||||
#include <bit> // for endian
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdint.h> // for int16_t, int32_t, int64_t, uint16_t, uint32_t
|
||||
#include <cstddef> // for byte
|
||||
#include <iostream> // for istream, ostream
|
||||
|
||||
namespace bell {
|
||||
class BinaryStream {
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "ByteStream.h"
|
||||
#include "BellTask.h"
|
||||
#include "WrappedSemaphore.h"
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <stddef.h> // for size_t
|
||||
#include <stdint.h> // for uint32_t, uint8_t
|
||||
#include <atomic> // for atomic
|
||||
#include <functional> // for function
|
||||
#include <memory> // for shared_ptr
|
||||
#include <mutex> // for mutex
|
||||
#include <string> // for string
|
||||
|
||||
#include "BellTask.h" // for Task
|
||||
#include "ByteStream.h" // for ByteStream
|
||||
#include "WrappedSemaphore.h" // for WrappedSemaphore
|
||||
|
||||
/**
|
||||
* This class implements a wrapper around an arbitrary bell::ByteStream,
|
||||
@@ -26,12 +30,12 @@
|
||||
* method correctly, such as that 0 is returned if, and only if the stream ends.
|
||||
*/
|
||||
class BufferedStream : public bell::ByteStream, bell::Task {
|
||||
public:
|
||||
typedef std::shared_ptr<bell::ByteStream> StreamPtr;
|
||||
typedef std::function<StreamPtr(uint32_t rangeStart)> StreamReader;
|
||||
public:
|
||||
typedef std::shared_ptr<bell::ByteStream> StreamPtr;
|
||||
typedef std::function<StreamPtr(uint32_t rangeStart)> StreamReader;
|
||||
|
||||
public:
|
||||
/**
|
||||
public:
|
||||
/**
|
||||
* @param taskName name to use for the reading task
|
||||
* @param bufferSize total size of the reading buffer
|
||||
* @param readThreshold how much can be read before refilling the buffer
|
||||
@@ -41,22 +45,18 @@ class BufferedStream : public bell::ByteStream, bell::Task {
|
||||
* @param waitForReady whether to wait for the buffer to be ready during reading
|
||||
* @param endWithSource whether to end the streaming as soon as source returns 0 from read()
|
||||
*/
|
||||
BufferedStream(
|
||||
const std::string &taskName,
|
||||
uint32_t bufferSize,
|
||||
uint32_t readThreshold,
|
||||
uint32_t readSize,
|
||||
uint32_t readyThreshold,
|
||||
uint32_t notReadyThreshold,
|
||||
bool waitForReady = false);
|
||||
~BufferedStream() override;
|
||||
bool open(const StreamPtr &stream);
|
||||
bool open(const StreamReader &newReader, uint32_t initialOffset = 0);
|
||||
void close() override;
|
||||
BufferedStream(const std::string& taskName, uint32_t bufferSize,
|
||||
uint32_t readThreshold, uint32_t readSize,
|
||||
uint32_t readyThreshold, uint32_t notReadyThreshold,
|
||||
bool waitForReady = false);
|
||||
~BufferedStream() override;
|
||||
bool open(const StreamPtr& stream);
|
||||
bool open(const StreamReader& newReader, uint32_t initialOffset = 0);
|
||||
void close() override;
|
||||
|
||||
// inherited methods
|
||||
public:
|
||||
/**
|
||||
// inherited methods
|
||||
public:
|
||||
/**
|
||||
* Read len bytes from the buffer to dst. If waitForReady is enabled
|
||||
* and readAvailable is lower than notReadyThreshold, the function
|
||||
* will block until readyThreshold bytes is available.
|
||||
@@ -65,61 +65,63 @@ class BufferedStream : public bell::ByteStream, bell::Task {
|
||||
* if the buffer does not contain len bytes available), or 0 if the source
|
||||
* stream is already closed and there is no reader attached.
|
||||
*/
|
||||
size_t read(uint8_t *dst, size_t len) override;
|
||||
size_t skip(size_t len) override;
|
||||
size_t position() override;
|
||||
size_t size() override;
|
||||
size_t read(uint8_t* dst, size_t len) override;
|
||||
size_t skip(size_t len) override;
|
||||
size_t position() override;
|
||||
size_t size() override;
|
||||
|
||||
// stream status
|
||||
public:
|
||||
/**
|
||||
// stream status
|
||||
public:
|
||||
/**
|
||||
* Total amount of bytes served to read().
|
||||
*/
|
||||
uint32_t readTotal;
|
||||
/**
|
||||
uint32_t readTotal;
|
||||
/**
|
||||
* Total amount of bytes read from source.
|
||||
*/
|
||||
uint32_t bufferTotal;
|
||||
/**
|
||||
uint32_t bufferTotal;
|
||||
/**
|
||||
* Amount of bytes available to read from the buffer.
|
||||
*/
|
||||
std::atomic<uint32_t> readAvailable;
|
||||
/**
|
||||
std::atomic<uint32_t> readAvailable;
|
||||
/**
|
||||
* Whether the caller should start reading the data. This indicates that a safe
|
||||
* amount (determined by readyThreshold) of data is available in the buffer.
|
||||
*/
|
||||
bool isReady() const;
|
||||
/**
|
||||
bool isReady() const;
|
||||
/**
|
||||
* Whether the caller should stop reading the data. This indicates that the amount of data
|
||||
* available for reading is decreasing to a non-safe value, as data is being read
|
||||
* faster than it can be buffered.
|
||||
*/
|
||||
bool isNotReady() const;
|
||||
/**
|
||||
bool isNotReady() const;
|
||||
/**
|
||||
* Semaphore that is given when the buffer becomes ready (isReady() == true). Caller can
|
||||
* wait for the semaphore instead of continuously querying isReady().
|
||||
*/
|
||||
bell::WrappedSemaphore readySem;
|
||||
bell::WrappedSemaphore readySem;
|
||||
|
||||
private:
|
||||
std::mutex runningMutex;
|
||||
bool running = false;
|
||||
bool terminate = false;
|
||||
bell::WrappedSemaphore readSem; // signal to start writing to buffer after reading from it
|
||||
std::mutex readMutex; // mutex for locking read operations during writing, and vice versa
|
||||
uint32_t bufferSize;
|
||||
uint32_t readAt;
|
||||
uint32_t readSize;
|
||||
uint32_t readyThreshold;
|
||||
uint32_t notReadyThreshold;
|
||||
bool waitForReady;
|
||||
uint8_t *buf;
|
||||
uint8_t *bufEnd;
|
||||
uint8_t *bufReadPtr;
|
||||
uint8_t *bufWritePtr;
|
||||
StreamPtr source;
|
||||
StreamReader reader;
|
||||
void runTask() override;
|
||||
void reset();
|
||||
uint32_t lengthBetween(uint8_t *me, uint8_t *other);
|
||||
private:
|
||||
std::mutex runningMutex;
|
||||
bool running = false;
|
||||
bool terminate = false;
|
||||
bell::WrappedSemaphore
|
||||
readSem; // signal to start writing to buffer after reading from it
|
||||
std::mutex
|
||||
readMutex; // mutex for locking read operations during writing, and vice versa
|
||||
uint32_t bufferSize;
|
||||
uint32_t readAt;
|
||||
uint32_t readSize;
|
||||
uint32_t readyThreshold;
|
||||
uint32_t notReadyThreshold;
|
||||
bool waitForReady;
|
||||
uint8_t* buf;
|
||||
uint8_t* bufEnd;
|
||||
uint8_t* bufReadPtr;
|
||||
uint8_t* bufWritePtr;
|
||||
StreamPtr source;
|
||||
StreamReader reader;
|
||||
void runTask() override;
|
||||
void reset();
|
||||
uint32_t lengthBetween(uint8_t* me, uint8_t* other);
|
||||
};
|
||||
|
||||
@@ -1,27 +1,25 @@
|
||||
#ifndef BELL_BYTE_READER_H
|
||||
#define BELL_BYTE_READER_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* A class for reading bytes from a stream. Further implemented in HTTPStream.h
|
||||
*/
|
||||
namespace bell
|
||||
{
|
||||
class ByteStream
|
||||
{
|
||||
public:
|
||||
ByteStream(){};
|
||||
virtual ~ByteStream() = default;
|
||||
namespace bell {
|
||||
class ByteStream {
|
||||
public:
|
||||
ByteStream(){};
|
||||
virtual ~ByteStream() = default;
|
||||
|
||||
virtual size_t read(uint8_t *buf, size_t nbytes) = 0;
|
||||
virtual size_t skip(size_t nbytes) = 0;
|
||||
virtual size_t read(uint8_t* buf, size_t nbytes) = 0;
|
||||
virtual size_t skip(size_t nbytes) = 0;
|
||||
|
||||
virtual size_t position() = 0;
|
||||
virtual size_t size() = 0;
|
||||
virtual void close() = 0;
|
||||
};
|
||||
}
|
||||
virtual size_t position() = 0;
|
||||
virtual size_t size() = 0;
|
||||
virtual void close() = 0;
|
||||
};
|
||||
} // namespace bell
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,39 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include "WrappedSemaphore.h"
|
||||
#include <cstdint> // for uint8_t
|
||||
#include <cstring> // for size_t
|
||||
#include <memory> // for unique_ptr
|
||||
#include <mutex> // for mutex
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "WrappedSemaphore.h" // for WrappedSemaphore
|
||||
|
||||
namespace bell {
|
||||
class CircularBuffer {
|
||||
public:
|
||||
CircularBuffer(size_t dataCapacity);
|
||||
public:
|
||||
CircularBuffer(size_t dataCapacity);
|
||||
|
||||
std::unique_ptr<bell::WrappedSemaphore> dataSemaphore;
|
||||
std::unique_ptr<bell::WrappedSemaphore> dataSemaphore;
|
||||
|
||||
size_t size() const {
|
||||
return dataSize;
|
||||
}
|
||||
size_t size() const { return dataSize; }
|
||||
|
||||
size_t capacity() const {
|
||||
return dataCapacity;
|
||||
}
|
||||
size_t capacity() const { return dataCapacity; }
|
||||
|
||||
size_t write(const uint8_t *data, size_t bytes);
|
||||
size_t read(uint8_t *data, size_t bytes);
|
||||
void emptyBuffer();
|
||||
void emptyExcept(size_t size);
|
||||
size_t write(const uint8_t* data, size_t bytes);
|
||||
size_t read(uint8_t* data, size_t bytes);
|
||||
void emptyBuffer();
|
||||
void emptyExcept(size_t size);
|
||||
|
||||
private:
|
||||
std::mutex bufferMutex;
|
||||
size_t begIndex = 0;
|
||||
size_t endIndex = 0;
|
||||
size_t dataSize = 0;
|
||||
size_t dataCapacity = 0;
|
||||
std::vector<uint8_t> buffer;
|
||||
private:
|
||||
std::mutex bufferMutex;
|
||||
size_t begIndex = 0;
|
||||
size_t endIndex = 0;
|
||||
size_t dataSize = 0;
|
||||
size_t dataCapacity = 0;
|
||||
std::vector<uint8_t> buffer;
|
||||
};
|
||||
} // namespace bell
|
||||
} // namespace bell
|
||||
@@ -1,15 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stddef.h> // for size_t
|
||||
#include <cstdint> // for uint8_t
|
||||
#include <memory> // for shared_ptr, unique_ptr
|
||||
#include <string> // for basic_string, string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "BellLogger.h"
|
||||
#include "ByteStream.h"
|
||||
#include "DecoderGlobals.h"
|
||||
#include "aacdec.h"
|
||||
#include "mp3dec.h"
|
||||
#include "aacdec.h" // for AACFrameInfo
|
||||
#include "mp3dec.h" // for MP3FrameInfo
|
||||
|
||||
namespace bell {
|
||||
class ByteStream;
|
||||
|
||||
class EncodedAudioStream {
|
||||
public:
|
||||
EncodedAudioStream();
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <BellLogger.h>
|
||||
#include <ByteStream.h>
|
||||
#include <stdio.h>
|
||||
#include <ByteStream.h> // for ByteStream
|
||||
#include <stdint.h> // for uint8_t
|
||||
#include <stdio.h> // for size_t, FILE
|
||||
#include <string> // for string
|
||||
|
||||
/*
|
||||
* FileStream
|
||||
@@ -12,17 +11,15 @@
|
||||
* A class for reading and writing to files implementing the ByteStream interface.
|
||||
*
|
||||
*/
|
||||
namespace bell
|
||||
{
|
||||
class FileStream : public ByteStream
|
||||
{
|
||||
public:
|
||||
FileStream(const std::string& path, std::string mode);
|
||||
~FileStream();
|
||||
namespace bell {
|
||||
class FileStream : public ByteStream {
|
||||
public:
|
||||
FileStream(const std::string& path, std::string mode);
|
||||
~FileStream();
|
||||
|
||||
FILE* file;
|
||||
FILE* file;
|
||||
|
||||
/*
|
||||
/*
|
||||
* Reads data from the stream.
|
||||
*
|
||||
* @param buf The buffer to read data into.
|
||||
@@ -30,18 +27,18 @@ namespace bell
|
||||
* @return The number of bytes read.
|
||||
* @throws std::runtime_error if the stream is closed.
|
||||
*/
|
||||
size_t read(uint8_t *buf, size_t nbytes);
|
||||
size_t read(uint8_t* buf, size_t nbytes);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Skips nbytes bytes in the stream.
|
||||
*/
|
||||
size_t skip(size_t nbytes);
|
||||
size_t skip(size_t nbytes);
|
||||
|
||||
size_t position();
|
||||
size_t position();
|
||||
|
||||
size_t size();
|
||||
size_t size();
|
||||
|
||||
// Closes the connection
|
||||
void close();
|
||||
};
|
||||
}
|
||||
// Closes the connection
|
||||
void close();
|
||||
};
|
||||
} // namespace bell
|
||||
|
||||
@@ -1,24 +1,20 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <stddef.h> // for size_t
|
||||
#include <cstdint> // for uint8_t, int32_t
|
||||
#include <memory> // for make_unique, unique_ptr
|
||||
#include <string> // for string
|
||||
#include <string_view> // for string_view
|
||||
#include <utility> // for pair
|
||||
#include <vector> // for vector
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
#include "BellSocket.h"
|
||||
#include "ByteStream.h"
|
||||
#include "SocketStream.h"
|
||||
#include "URLParser.h"
|
||||
#include "SocketStream.h" // for SocketStream
|
||||
#include "URLParser.h" // for URLParser
|
||||
#ifndef BELL_DISABLE_FMT
|
||||
#include "fmt/core.h"
|
||||
#include "fmt/core.h" // for format
|
||||
#endif
|
||||
#include "picohttpparser.h"
|
||||
#include "picohttpparser.h" // for phr_header
|
||||
|
||||
namespace bell {
|
||||
class HTTPClient {
|
||||
@@ -31,19 +27,20 @@ class HTTPClient {
|
||||
// Helper over ValueHeader, formatting a HTTP bytes range
|
||||
struct RangeHeader {
|
||||
static ValueHeader range(int32_t from, int32_t to) {
|
||||
#ifndef BELL_DISABLE_FMT
|
||||
#ifndef BELL_DISABLE_FMT
|
||||
return ValueHeader{"Range", fmt::format("bytes={}-{}", from, to)};
|
||||
#else
|
||||
return ValueHeader{"Range", "bytes=" + std::to_string(from) + "-" + std::to_string(to)};
|
||||
#endif
|
||||
#else
|
||||
return ValueHeader{
|
||||
"Range", "bytes=" + std::to_string(from) + "-" + std::to_string(to)};
|
||||
#endif
|
||||
}
|
||||
|
||||
static ValueHeader last(int32_t nbytes) {
|
||||
#ifndef BELL_DISABLE_FMT
|
||||
#ifndef BELL_DISABLE_FMT
|
||||
return ValueHeader{"Range", fmt::format("bytes=-{}", nbytes)};
|
||||
#else
|
||||
#else
|
||||
return ValueHeader{"Range", "bytes=-" + std::to_string(nbytes)};
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "TCPSocket.h"
|
||||
#include "TLSSocket.h"
|
||||
#include <iostream> // for streamsize, basic_streambuf<>::int_type, ios...
|
||||
#include <memory> // for unique_ptr, operator!=
|
||||
#include <string> // for char_traits, string
|
||||
|
||||
#include "BellSocket.h" // for Socket
|
||||
|
||||
namespace bell {
|
||||
class SocketBuffer : public std::streambuf {
|
||||
|
||||
@@ -39,6 +39,8 @@ class TCPSocket : public bell::Socket {
|
||||
TCPSocket(){};
|
||||
~TCPSocket() { close(); };
|
||||
|
||||
int getFd() { return sockFd; }
|
||||
|
||||
void open(const std::string& host, uint16_t port) {
|
||||
int err;
|
||||
int domain = AF_INET;
|
||||
@@ -101,7 +103,9 @@ class TCPSocket : public bell::Socket {
|
||||
#endif
|
||||
return value;
|
||||
}
|
||||
bool isOpen() { return !isClosed; }
|
||||
bool isOpen() {
|
||||
return !isClosed;
|
||||
}
|
||||
|
||||
void close() {
|
||||
if (!isClosed) {
|
||||
|
||||
@@ -1,34 +1,21 @@
|
||||
#ifndef BELL_TLS_SOCKET_H
|
||||
#define BELL_TLS_SOCKET_H
|
||||
|
||||
#include <ctype.h>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "BellLogger.h"
|
||||
#include "BellSocket.h"
|
||||
#include <stdint.h> // for uint8_t, uint16_t
|
||||
|
||||
#include "BellSocket.h" // for Socket
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdlib.h> // for size_t
|
||||
#include <string> // for string
|
||||
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/net_sockets.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/ctr_drbg.h" // for mbedtls_ctr_drbg_context
|
||||
#include "mbedtls/entropy.h" // for mbedtls_entropy_context
|
||||
#include "mbedtls/net_sockets.h" // for mbedtls_net_context
|
||||
#include "mbedtls/ssl.h" // for mbedtls_ssl_config, mbedtls_ssl_con...
|
||||
|
||||
namespace bell {
|
||||
class TLSSocket : public bell::Socket {
|
||||
@@ -53,6 +40,7 @@ class TLSSocket : public bell::Socket {
|
||||
bool isOpen();
|
||||
|
||||
void close();
|
||||
int getFd() { return server_fd.fd; }
|
||||
};
|
||||
|
||||
} // namespace bell
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstdlib> // for strtol, size_t
|
||||
#include <regex> // for match_results, match_results<>::value_type, sub...
|
||||
#include <stdexcept> // for invalid_argument
|
||||
#include <string> // for string, allocator, operator+, char_traits, oper...
|
||||
|
||||
namespace bell {
|
||||
class URLParser {
|
||||
@@ -60,7 +60,7 @@ class URLParser {
|
||||
std::string path;
|
||||
#ifdef BELL_DISABLE_REGEX
|
||||
void parse(const char* url, std::vector<std::string>& match);
|
||||
#else
|
||||
#else
|
||||
static const std::regex urlParseRegex;
|
||||
#endif
|
||||
|
||||
@@ -71,10 +71,10 @@ class URLParser {
|
||||
#ifdef BELL_DISABLE_REGEX
|
||||
std::vector<std::string> match(6);
|
||||
parser.parse(url.c_str(), match);
|
||||
#else
|
||||
#else
|
||||
std::cmatch match;
|
||||
std::regex_match(url.c_str(), match, parser.urlParseRegex);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (match.size() < 3) {
|
||||
throw std::invalid_argument("Invalid URL");
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include "BellLogger.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include <mbedtls/x509_crt.h> // for mbedtls_x509_crt
|
||||
#include <stddef.h> // for size_t
|
||||
#include <cstdint> // for uint8_t, uint16_t, uint32_t
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "mbedtls/ssl.h" // for mbedtls_ssl_config
|
||||
|
||||
namespace bell::X509Bundle {
|
||||
|
||||
@@ -13,6 +16,7 @@ typedef struct crt_bundle_t {
|
||||
} crt_bundle_t;
|
||||
|
||||
static crt_bundle_t s_crt_bundle;
|
||||
static std::vector<uint8_t> bundleBytes;
|
||||
|
||||
static constexpr auto TAG = "X509Bundle";
|
||||
static constexpr auto CRT_HEADER_OFFSET = 4;
|
||||
|
||||
@@ -40,30 +40,35 @@ extern "C" {
|
||||
/* contains name and value of a header (name == NULL if is a continuing line
|
||||
* of a multiline header */
|
||||
struct phr_header {
|
||||
const char *name;
|
||||
size_t name_len;
|
||||
const char *value;
|
||||
size_t value_len;
|
||||
const char* name;
|
||||
size_t name_len;
|
||||
const char* value;
|
||||
size_t value_len;
|
||||
};
|
||||
|
||||
/* returns number of bytes consumed if successful, -2 if request is partial,
|
||||
* -1 if failed */
|
||||
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
|
||||
int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
||||
int phr_parse_request(const char* buf, size_t len, const char** method,
|
||||
size_t* method_len, const char** path, size_t* path_len,
|
||||
int* minor_version, struct phr_header* headers,
|
||||
size_t* num_headers, size_t last_len);
|
||||
|
||||
/* ditto */
|
||||
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
|
||||
struct phr_header *headers, size_t *num_headers, size_t last_len);
|
||||
int phr_parse_response(const char* _buf, size_t len, int* minor_version,
|
||||
int* status, const char** msg, size_t* msg_len,
|
||||
struct phr_header* headers, size_t* num_headers,
|
||||
size_t last_len);
|
||||
|
||||
/* ditto */
|
||||
int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
||||
int phr_parse_headers(const char* buf, size_t len, struct phr_header* headers,
|
||||
size_t* num_headers, size_t last_len);
|
||||
|
||||
/* should be zero-filled before start */
|
||||
struct phr_chunked_decoder {
|
||||
size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
|
||||
char consume_trailer; /* if trailing headers should be consumed */
|
||||
char _hex_count;
|
||||
char _state;
|
||||
size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
|
||||
char consume_trailer; /* if trailing headers should be consumed */
|
||||
char _hex_count;
|
||||
char _state;
|
||||
};
|
||||
|
||||
/* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
|
||||
@@ -75,10 +80,11 @@ struct phr_chunked_decoder {
|
||||
* octets left undecoded, that starts from the offset returned by `*bufsz`.
|
||||
* Returns -1 on error.
|
||||
*/
|
||||
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *bufsz);
|
||||
ssize_t phr_decode_chunked(struct phr_chunked_decoder* decoder, char* buf,
|
||||
size_t* bufsz);
|
||||
|
||||
/* returns if the chunked decoder is in middle of chunked data */
|
||||
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
|
||||
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder* decoder);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user