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.
This commit is contained in:
Gregg 2023-12-30 07:48:11 -06:00
parent f3e98601b0
commit 7282c2a8c9
3 changed files with 103 additions and 63 deletions

View File

@ -38,6 +38,7 @@
#include <unordered_map>
#include <vector>
#include <list>
#include <sstream>
#include <nvs.h>
#include <ArduinoOTA.h>
#include <esp_now.h>
@ -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 <class T>

81
src/Streamer.cpp Normal file
View File

@ -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;
}
//////////////////////////////////////

View File

@ -27,11 +27,7 @@
#pragma once
#include <sstream>
#include <WiFi.h>
#include <Arduino.h>
#include <driver/timer.h>
#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