Moved StreamBuf into HapOut as private nested class

This commit is contained in:
Gregg 2023-12-30 17:07:12 -06:00
parent 117c348708
commit 18c74e6f17
3 changed files with 28 additions and 42 deletions

View File

@ -1152,7 +1152,6 @@ int HAPClient::getStatusURL(){
LOG2("\n>>>>>>>>>> %s >>>>>>>>>>\n",client.remoteIP().toString().c_str());
// hapStream.setHapClient(this).setLogLevel(2);
hapOut.setHapClient(this).setLogLevel(2);
hapOut << "HTTP/1.1 200 OK\r\nContent-type: text/html; charset=utf-8\r\n\r\n";
@ -1637,7 +1636,7 @@ void Nonce::inc(){
//////////////////////////////////////
//////////////////////////////////////
StreamBuffer::StreamBuffer(){
HapOut::HapStreamBuffer::HapStreamBuffer(){
buffer=(char *)HS_MALLOC(bufSize+1); // add 1 for optional null terminator when printing text
setp(buffer, buffer+bufSize-1);
@ -1645,7 +1644,7 @@ StreamBuffer::StreamBuffer(){
//////////////////////////////////////
StreamBuffer::~StreamBuffer(){
HapOut::HapStreamBuffer::~HapStreamBuffer(){
sync();
free(buffer);
@ -1653,7 +1652,7 @@ StreamBuffer::~StreamBuffer(){
//////////////////////////////////////
int StreamBuffer::flushBuffer(){
int HapOut::HapStreamBuffer::flushBuffer(){
int num=pptr()-pbase();
if(logLevel<=homeSpan.getLogLevel()){
@ -1673,7 +1672,7 @@ int StreamBuffer::flushBuffer(){
//////////////////////////////////////
StreamBuffer::int_type StreamBuffer::overflow(StreamBuffer::int_type c){
std::streambuf::int_type HapOut::HapStreamBuffer::overflow(std::streambuf::int_type c){
if(c!=EOF){
*pptr() = c;
@ -1687,7 +1686,7 @@ StreamBuffer::int_type StreamBuffer::overflow(StreamBuffer::int_type c){
//////////////////////////////////////
int StreamBuffer::sync(){
int HapOut::HapStreamBuffer::sync(){
int_type c=flushBuffer();

View File

@ -180,16 +180,14 @@ struct HAPClient {
};
/////////////////////////////////////////////////
// StreamBuffer and HapOut Structures
// HapOut Structure
class HapOut;
class StreamBuffer : public std::streambuf {
friend HapOut;
class HapOut : public std::ostream {
private:
struct HapStreamBuffer : public std::streambuf {
const size_t bufSize=1024; // max allowed for HAP encrypted records
char *buffer;
HAPClient *hapClient=NULL;
@ -200,32 +198,23 @@ class StreamBuffer : public std::streambuf {
int_type overflow(int_type c) override;
int sync() override;
public:
HapStreamBuffer();
~HapStreamBuffer();
};
StreamBuffer();
~StreamBuffer();
};
class HapOut : public std::ostream {
private:
StreamBuffer *sBuf;
HapStreamBuffer hapBuffer;
public:
HapOut(StreamBuffer *sBuf) : std::ostream(sBuf) {this->sBuf=sBuf;}
HapOut() : std::ostream(&hapBuffer){}
HapOut& setHapClient(HAPClient *hapClient){sBuf->hapClient=hapClient;return(*this);}
HapOut& setLogLevel(int logLevel){sBuf->logLevel=logLevel;return(*this);}
HapOut& prettyPrint(){sBuf->enablePrettyPrint=true;return(*this);}
HapOut& setHapClient(HAPClient *hapClient){hapBuffer.hapClient=hapClient;return(*this);}
HapOut& setLogLevel(int logLevel){hapBuffer.logLevel=logLevel;return(*this);}
HapOut& prettyPrint(){hapBuffer.enablePrettyPrint=true;return(*this);}
};
/////////////////////////////////////////////////
// Extern Variables
extern HAPClient **hap;
//extern std::ostream hapOut;
extern HapOut hapOut;
extern StreamBuffer hapStream;

View File

@ -45,9 +45,7 @@ const __attribute__((section(".rodata_custom_desc"))) SpanPartition spanPartitio
using namespace Utils;
StreamBuffer hapStream;
HapOut hapOut(&hapStream);
HapOut hapOut; // Specialized output stream that can both print to serial monitor and encrypt/transmit to HAP Clients with minimal memory usage (global-scope)
HAPClient **hap; // HAP Client structure containing HTTP client connections, parsing routines, and state variables (global-scoped variable)
Span homeSpan; // HAP Attributes database and all related control functions for this Accessory (global-scoped variable)
HapCharacteristics hapChars; // Instantiation of all HAP Characteristics (used to create SpanCharacteristics)