From 7282c2a8c9f386d91776a41253c6ea42a71ffad6 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 30 Dec 2023 07:48:11 -0600 Subject: [PATCH] Moved Streamer from Utils.h into separate Streamer.cpp But declared Streamer inside HomeSpan.h instead of separate Streamer.h since it should only be used when HomeSpan.h is used. Also, changed class structure so that it is "final" by removing virtual tags and making all protected variables private. --- src/HomeSpan.h | 22 +++++++++++++ src/Streamer.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Utils.h | 63 ------------------------------------- 3 files changed, 103 insertions(+), 63 deletions(-) create mode 100644 src/Streamer.cpp diff --git a/src/HomeSpan.h b/src/HomeSpan.h index e3be0a6..14d5fe5 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -67,6 +68,27 @@ enum { GET_VALUE=128 }; +/////////////////////////////// + +class StreamBuffer : public std::streambuf { + + private: + + char *buffer; + WiFiClient *client; + + int flushBuffer() ; + int_type overflow(int_type c) override; + int sync() override; + + public: + + StreamBuffer(WiFiClient *client, size_t bufSize=1024); + ~StreamBuffer(); + +}; + + /////////////////////////////// template diff --git a/src/Streamer.cpp b/src/Streamer.cpp new file mode 100644 index 0000000..3e1646f --- /dev/null +++ b/src/Streamer.cpp @@ -0,0 +1,81 @@ +/********************************************************************************* + * MIT License + * + * Copyright (c) 2020-2023 Gregg E. Berman + * + * https://github.com/HomeSpan/HomeSpan + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + ********************************************************************************/ + +#include "HomeSpan.h" + +StreamBuffer::StreamBuffer(WiFiClient *client, size_t bufSize){ + + this->client=client; + buffer=(char *)HS_MALLOC(bufSize); + setp (buffer, buffer+bufSize-1); +} + +////////////////////////////////////// + +StreamBuffer::~StreamBuffer(){ + + free(buffer); + sync(); +} + +////////////////////////////////////// + +int StreamBuffer::flushBuffer(){ + int num=pptr()-pbase(); + + write(1,buffer,num); + client->write(buffer,num); + delay(1); + + pbump(-num); + return num; +} + +////////////////////////////////////// + +StreamBuffer::int_type StreamBuffer::overflow(StreamBuffer::int_type c){ + + if(c!=EOF){ + *pptr() = c; + pbump(1); + } + + if(flushBuffer()==EOF) + return EOF; + return c; +} + +////////////////////////////////////// + +int StreamBuffer::sync(){ + + if(flushBuffer()==EOF) + return -1; + return 0; +} + +////////////////////////////////////// diff --git a/src/Utils.h b/src/Utils.h index 7b0149d..756511d 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -27,11 +27,7 @@ #pragma once -#include -#include - #include -#include #if defined(BOARD_HAS_PSRAM) #define HS_MALLOC ps_malloc @@ -52,65 +48,6 @@ String mask(char *c, int n); // simply utility that creates a String fr } -///////////////////////////////////////////////// - -class StreamBuffer : public std::streambuf { - - protected: - - char *buffer; - WiFiClient *client; - - public: - - StreamBuffer(WiFiClient *client, size_t bufSize=1024){ - this->client=client; - buffer=(char *)HS_MALLOC(bufSize); - setp (buffer, buffer+bufSize-1); - } - - virtual ~StreamBuffer() { - free(buffer); - sync(); - } - - protected: - - int flushBuffer(){ - int num=pptr()-pbase(); - -// if(write(1,buffer,num)!=num) -// return EOF; - - write(1,buffer,num); - client->write(buffer,num); - delay(1); - - pbump(-num); - return num; - } - - virtual int_type overflow(int_type c){ // buffer is full - if(c!=EOF){ - *pptr() = c; - pbump(1); - } - - if(flushBuffer()==EOF) - return EOF; - - return c; - } - - virtual int sync(){ - if(flushBuffer()==EOF) - return -1; - - return 0; - } - -}; - ///////////////////////////////////////////////// // Creates a temporary buffer that is freed after // going out of scope