2 Commits

Author SHA1 Message Date
f441b186b5 Enhancements 2025-08-14 17:32:16 -04:00
8b4769bf7e Add support for callbacks 2025-08-14 12:47:10 -04:00
13 changed files with 489 additions and 166 deletions

41
common/callback.hpp Executable file
View File

@@ -0,0 +1,41 @@
#ifndef _COMMON_CALLBACK_HPP_
#define _COMMON_CALLBACK_HPP_
#ifndef _COMMON_CALLBACKDATA_HPP_
#include <common/cbdata.hpp>
#endif
#ifndef _COMMON_PURECALLBACK_HPP_
#include <common/pcallbck.hpp>
#endif
#ifndef _COMMON_CALLBACKPOINTER_HPP_
#include <common/cbptr.hpp>
#endif
template <class T,class D>
class Callback : public PureCallback<D>
{
public:
typedef LONG (T::*PFNMETHOD)(CallbackData<D> &callbackData);
Callback(void);
Callback(const Callback<T,D> &someCallback);
Callback(T *lpObject,PFNMETHOD lpMethod);
virtual ~Callback();
void setObject(T *lpObject);
void setMethod(PFNMETHOD lpMethod);
void setCallback(T *lpObject,PFNMETHOD lpMethod);
WORD operator==(const Callback<T,D> &someCallback)const;
void operator=(const Callback<T,D> &someCallback);
LONG operator*(CallbackData<D> &someCallbackData);
private:
T *mlpObject;
LONG (T::*mlpMethod)(CallbackData<D> &someCallbackData);
};
template <class T,class D>
inline
LONG Callback<T,D>::operator*(CallbackData<D> &someCallbackData)
{
if((!mlpObject)||(!mlpMethod))return (LONG)0;
return (mlpObject->*mlpMethod)(someCallbackData);
}
#include <common/callback.tpp>
#endif

60
common/callback.tpp Executable file
View File

@@ -0,0 +1,60 @@
#ifndef _COMMON_CALLBACK_TPP_
#define _COMMON_CALLBACK_TPP_
template <class T,class D>
Callback<T,D>::Callback(void)
: mlpObject(0), mlpMethod(0)
{
}
template <class T,class D>
Callback<T,D>::Callback(const Callback<T,D> &someCallback)
: mlpObject(someCallback.mlpObject), mlpMethod(someCallback.mlpMethod)
{
}
template <class T,class D>
Callback<T,D>::Callback(T *lpObject,PFNMETHOD lpMethod)
: mlpObject(lpObject), mlpMethod(lpMethod)
{
}
template <class T,class D>
Callback<T,D>::~Callback()
{
}
template <class T,class D>
WORD Callback<T,D>::operator==(const Callback<T,D> &someCallback)const
{
return (mlpObject==someCallback.mlpObject && mlpMethod==someCallback.mlpMethod);
}
template <class T,class D>
void Callback<T,D>::operator=(const Callback<T,D> &someCallback)
{
mlpObject=someCallback.mlpObject;
mlpMethod=someCallback.mlpMethod;
}
template <class T,class D>
void Callback<T,D>::setObject(T *lpObject)
{
mlpObject=lpObject;
}
template <class T,class D>
void Callback<T,D>::setMethod(PFNMETHOD lpMethod)
{
mlpMethod=lpMethod;
}
template <class T,class D>
void Callback<T,D>::setCallback(T *lpObject,PFNMETHOD lpMethod)
{
mlpObject=lpObject;
mlpMethod=lpMethod;
}
#endif

70
common/cbdata.hpp Executable file
View File

@@ -0,0 +1,70 @@
#ifndef _COMMON_CALLBACKDATA_HPP_
#define _COMMON_CALLBACKDATA_HPP_
#ifndef _COMMON_WINDOWS_HPP_
#include <common/windows.hpp>
#endif
template<class T>
class CallbackData
{
public:
typedef LONG ReturnType;
CallbackData(void);
CallbackData(T &data);
CallbackData(const CallbackData &someCallbackData);
virtual ~CallbackData();
CallbackData &operator=(const CallbackData &someCallbackData);
T &getData(void);
void setData(T &data);
private:
SmartPointer<T> mData;
};
template<class T>
inline
CallbackData<T>::CallbackData(void)
{
}
template<class T>
inline
CallbackData<T>::CallbackData(T &data)
{
mData=&data;
}
template <class T>
inline
CallbackData<T>::CallbackData(const CallbackData &someCallbackData)
{
*this=someCallbackData;
}
template <class T>
inline
CallbackData<T>::~CallbackData()
{
}
template <class T>
inline
CallbackData<T> &CallbackData<T>::operator=(const CallbackData<T> &someCallbackData)
{
mData = someCallbackData.mData;
return *this;
}
template <class T>
inline
T &CallbackData<T>::getData(void)
{
return *mData;
}
template <class T>
inline
void CallbackData<T>::setData(T &data)
{
mData=&data;
}
#endif

81
common/cbptr.hpp Executable file
View File

@@ -0,0 +1,81 @@
#ifndef _COMMON_CALLBACKPOINTER_HPP_
#define _COMMON_CALLBACKPOINTER_HPP_
#ifndef _COMMON_WINDOWS_HPP_
#include <common/windows.hpp>
#endif
#ifndef _COMMON_PURECALLBACK_HPP_
#include <common/pcallbck.hpp>
#endif
template <class T>
class CallbackPointer
{
public:
CallbackPointer(void);
CallbackPointer(PureCallback<T> *lpCallback);
CallbackPointer(const CallbackPointer<T> &someCallbackPointer);
virtual ~CallbackPointer();
void operator=(CallbackPointer &someCallbackPointer);
WORD operator==(const CallbackPointer &someCallbackPointer)const;
LONG callback(CallbackData<T> &someCallbackData);
bool isOkay(void)const;
private:
PureCallback<T> *mlpCallback;
};
template <class T>
inline
CallbackPointer<T>::CallbackPointer(void)
: mlpCallback(0)
{
}
template <class T>
inline
CallbackPointer<T>::CallbackPointer(PureCallback<T> *lpCallback)
: mlpCallback(lpCallback)
{
}
template <class T>
inline
CallbackPointer<T>::CallbackPointer(const CallbackPointer<T> &someCallbackPointer)
: mlpCallback(someCallbackPointer.mlpCallback)
{
}
template <class T>
inline
CallbackPointer<T>::~CallbackPointer()
{
}
template <class T>
inline
void CallbackPointer<T>::operator=(CallbackPointer<T> &someCallbackPointer)
{
mlpCallback=someCallbackPointer.mlpCallback;
}
template <class T>
inline
WORD CallbackPointer<T>::operator==(const CallbackPointer<T> &someCallbackPointer)const
{
return (mlpCallback==someCallbackPointer.mlpCallback);
}
template <class T>
inline
LONG CallbackPointer<T>::callback(CallbackData<T> &someCallbackData)
{
if(!mlpCallback)return (LONG)0;
return mlpCallback->operator*(someCallbackData);
}
template <class T>
inline
bool CallbackPointer<T>::isOkay(void)const
{
return mlpCallback?true:false;
}
#endif

35
common/pcallbck.hpp Executable file
View File

@@ -0,0 +1,35 @@
#ifndef _COMMON_PURECALLBACK_HPP_
#define _COMMON_PURECALLBACK_HPP_
#ifndef _COMMON_CALLBACKDATA_HPP_
#include <common/cbdata.hpp>
#endif
template <class T>
class PureCallback
{
public:
PureCallback(void);
virtual ~PureCallback();
virtual LONG operator*(CallbackData<T> &someCallbackData)=0;
private:
};
template <class T>
inline
PureCallback<T>::PureCallback(void)
{
}
template <class T>
inline
PureCallback<T>::~PureCallback()
{
}
template <class T>
inline
LONG PureCallback<T>::operator*(CallbackData<T> &/*someCallbackData*/)
{
return 0;
}
#endif

View File

@@ -14,7 +14,8 @@ template <class T>
class SmartPointer class SmartPointer
{ {
public: public:
SmartPointer(void); // SmartPointer(void);
SmartPointer(bool createNew=false);
SmartPointer(T FAR *lpSmartPointer,PointerDisposition::Disposition disposition=PointerDisposition::Assume); SmartPointer(T FAR *lpSmartPointer,PointerDisposition::Disposition disposition=PointerDisposition::Assume);
SmartPointer(const SmartPointer<T> &someSmartPointer); SmartPointer(const SmartPointer<T> &someSmartPointer);
virtual ~SmartPointer(); virtual ~SmartPointer();
@@ -36,11 +37,23 @@ private:
PointerDisposition::Disposition mDisposition; PointerDisposition::Disposition mDisposition;
}; };
// template <class T>
// inline
// SmartPointer<T>::SmartPointer(void)
// : mlpSmartPointer(0), mDisposition(PointerDisposition::Assume)
// {
// }
template <class T> template <class T>
inline inline
SmartPointer<T>::SmartPointer(void) SmartPointer<T>::SmartPointer(bool createNew)
: mlpSmartPointer(0), mDisposition(PointerDisposition::Assume) : mlpSmartPointer(0), mDisposition(PointerDisposition::Assume)
{ {
if(createNew)
{
mlpSmartPointer = ::new T();
mDisposition = PointerDisposition::Delete;
}
} }
template <class T> template <class T>

View File

@@ -15,7 +15,7 @@ class Utility
static String formatCurrency(double number,int places=2); static String formatCurrency(double number,int places=2);
static bool fmod(double dividend,double divisor); static bool fmod(double dividend,double divisor);
static bool fmod(double dividend, double divisor,double epsilon); static bool fmod(double dividend, double divisor,double epsilon);
static String bytesTransferredToString(double bytesTransferred); static String byteCountToString(double bytesTransferred, bool perSecond = true);
private: private:
static String pad(String theString,char padChar,int toLength); static String pad(String theString,char padChar,int toLength);
static String addCommas(String& theString); static String addCommas(String& theString);
@@ -34,30 +34,35 @@ Utility::~Utility()
} }
inline inline
String Utility::bytesTransferredToString(double bytesTransferred) String Utility::byteCountToString(double bytesTransferred, bool perSecond)
{ {
double roundedValue = std::round(bytesTransferred * 100.00)/100.00; double roundedValue = std::round(bytesTransferred * 100.00)/100.00;
String strBytesTransferrred;
if(roundedValue >= 1E+15) if(roundedValue >= 1E+15)
{ {
return Utility::formatNumber(roundedValue/1E+15,2) + "Pb/s"; strBytesTransferrred = Utility::formatNumber(roundedValue/1E+15,2) + "Pb";
} }
else if(roundedValue >= 1000000000000) else if(roundedValue >= 1000000000000)
{ {
return Utility::formatNumber(roundedValue/1000000000000,2) + "Tb/s"; strBytesTransferrred = Utility::formatNumber(roundedValue/1000000000000,2) + "Tb";
} }
else if(roundedValue >= 1073741824) else if(roundedValue >= 1073741824)
{ {
return Utility::formatNumber(roundedValue/1073741824,2) + "Gbb/s"; strBytesTransferrred = Utility::formatNumber(roundedValue/1073741824,2) + "Gb";
} }
else if(roundedValue >= 1000000) else if(roundedValue >= 1000000)
{ {
return Utility::formatNumber(roundedValue/1000000,2) + "Mb/s"; strBytesTransferrred = Utility::formatNumber(roundedValue/1000000,2) + "Mb";
} }
else if(roundedValue >= 1000) else if(roundedValue >= 1000)
{ {
return Utility::formatNumber(roundedValue/1000,2) + "Kb/s"; strBytesTransferrred = Utility::formatNumber(roundedValue/1000,2) + "Kb";
} }
return Utility::formatNumber(roundedValue,2) + "B/s"; else strBytesTransferrred = Utility::formatNumber(roundedValue,2) + "B";
if(perSecond)strBytesTransferrred+="/s";
return strBytesTransferrred;
} }
inline inline

View File

@@ -48,6 +48,11 @@
"streambuf": "cpp", "streambuf": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"fstream": "cpp", "fstream": "cpp",
"iostream": "cpp" "iostream": "cpp",
"complex": "cpp",
"cstring": "cpp",
"semaphore": "cpp",
"stop_token": "cpp",
"thread": "cpp"
} }
} }

View File

@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.10.0) cmake_minimum_required(VERSION 3.10.0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/home/pi/Boneyard") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/home/pi/CPP")
project(tester VERSION 0.1.0 LANGUAGES C CXX) project(tester VERSION 0.1.0 LANGUAGES C CXX)
add_executable(tester main.cpp) add_executable(tester main.cpp)
add_subdirectory(/home/pi/Boneyard/common /home/pi/Boneyard/common/build) add_subdirectory(/home/pi/CPP/common /home/pi/CPP/common/build)
target_link_libraries(tester PRIVATE common) target_link_libraries(tester PRIVATE common)

View File

@@ -1 +0,0 @@

View File

@@ -10,144 +10,141 @@
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <tuple> #include <tuple>
#include <utility>
#include <complex> #include <complex>
#include <common/callback.hpp>
class Info
// #include <iostream> // For standard input/output operations (like std::cout)
// #include <fstream> // For file stream operations (like std::ifstream)
// #include <string> // For using std::string
// int main()
// {
// Profiler profiler = Profiler();
// std::ifstream inputFile("/mnt/mariadb/backupdb.sql");
// if (!inputFile.is_open())
// {
// std::cerr << "Error: Could not open the file." << std::endl;
// return 1; // Indicate an error
// }
// DWORD lines = 0;
// std::string line;
// while (std::getline(inputFile, line))
// {
// lines++;
// if(!(lines%1000000))
// {
// std::cout << "reading " << lines << " " << line << std::endl;
// }
// }
// inputFile.close();
// std::cout << "Done, total took " << profiler.end() << " (ms)" << std::endl;
// return 0; // Indicate successful execution
// }
// int main(int argc, char ** argv)
// {
// Profiler profiler = Profiler();
// FileIO inputFile = FileIO("/mnt/mariadb/backupdb.sql");
// inputFile.setBufferLength(500000000);
// // FileIO inputFile = FileIO("/home/pi/Boneyard/ctest/main.cpp");
// String strLine = String();
// DWORD length = 0;
// DWORD lines = 0;
// printf("opening...\n");
// while(inputFile.readLine(strLine))
// {
// lines++;
// // printf("reading %d %s\n",lines,(char*)strLine);
// if(!(lines%1000000))
// {
// printf("reading %d %s\n",lines,(char*)strLine);
// }
// }
// printf("Done, read %d lines\n",lines);
// inputFile.close();
// printf("done, took %d(ms)...\n",profiler.end());
// return 0;
// }
std::tuple<int,int,int> foo()
{ {
return std::make_tuple(2,3,4); public:
Info();
~Info();
String getMessage(void);
void setMessage(const String &message);
private:
String mMessage;
};
Info::Info()
{
std::cout << "Info" << std::endl;
}
Info::~Info()
{
std::cout << "~Info" << std::endl;
}
String Info::getMessage(void)
{
return mMessage;
}
void Info::setMessage(const String &message)
{
mMessage = message;
}
class B
{
public:
B();
LONG callback(CallbackData<Info> &callbackData);
~B();
private:
String mFoo;
};
B::B()
{
}
B::~B()
{
}
LONG B::callback(CallbackData<Info> &callbackData)
{
std::cout << "callback with data " << callbackData.getData().getMessage().str() << std::endl;
return 0;
}
class Monitor
{
public:
Monitor();
~Monitor();
void setCallback(Callback<B,Info> &callback);
void doCallback(CallbackData<Info> &callbackData);
private:
Callback<B,Info> mCallback;
};
Monitor::Monitor()
{
std::cout << "Monitor" << std::endl;
}
Monitor::~Monitor()
{
std::cout << "~Monitor" << std::endl;
}
void Monitor::setCallback(Callback<B,Info> &callback)
{
mCallback=callback;
}
void Monitor::doCallback(CallbackData<Info> &callbackData)
{
CallbackPointer cbptr(&mCallback);
cbptr.callback(callbackData);
} }
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {
// std::complex<double> c{4.0,3.0}; // std::vector<int> myBigVec(10000000, 2011);
// std::vector<std::string> s1;
// std::vector<std::string> s2;
// s1.push_back("hello");
// s1.push_back("world");
// s2 = std::move(s1);
Monitor monitor;
// SmartPointer<B> b(::new B(),PointerDisposition::Delete);
SmartPointer<B> b(true);
Callback<B,Info> callback(&(*b),&B::callback);
monitor.setCallback(callback);
// FileIO fileIO = FileIO("/mnt/mariadb/backupdb.sql"); Info info;
// std::cout << "Reading ... " << std::endl; info.setMessage("Hello");
// int lines =0;
// String strLine;
// while(fileIO.readLine(strLine))
// {
// lines++;
// if(!(lines%1000000))
// {
// printf("reading %d %s\n",lines,(char*)strLine);
// }
// }
// fileIO.close();
}
void stdLibStuff()
{
// std::vector<int> numbers;
// numbers.push_back(100);
// numbers.push_back(50);
// numbers.push_back(100);
// int *p=&numbers[0];
// int *p1=&numbers[1];
// int *p2=&numbers[2];
// int max = *std::max_element(numbers.begin(), numbers.end());
// std::cout << typeid(max_it).name() << std::endl;
// if(max_it == p)std::cout << "p" << std::endl;
// if(max_it == p1) std::cout << "p1" << std::endl;
// if(max_it == p2)std::cout << "p2" << std::endl;
// std::cout << *max_it << std::endl;
// int max_value = *max_it;
// std::max(*numbers.begin());
// std::cout << std::max(numbers) << std::endl;
// Block<int> numbers;
// numbers.insert(newInt(100);
// std::cout<< std::min(100,50) << std::endl;
// std::string s = "hello";
// std::cout << s << std::endl;
// std::vector<std::string> strings;
// strings.push_back("hello");
// strings.push_back("world");
// for (const std::string& s : strings)
// {
// std::cout << s << " ";
// }
// char buffer[] = "Another direct copy";
// int a = sizeof(buffer);
CallbackData<Info> callbackData(info);
monitor.doCallback(callbackData);
} }
// Callback callback(&(*b),C
// SmartPointer<B> b1 = b;
// b1->foo();
// b->foo();
// String str="Hello";
// b->setFoo(str);
// str="Goodbye";
// String str2=b->getFoo();
// str2="world";
void readFile() void readFile()
{ {
// MemoryMappedFile memoryMappedFile("/home/pi/Boneyard/ctest/main.cpp",MemoryMappedFile::CreationFlags::ReadOnly); // MemoryMappedFile memoryMappedFile("/home/pi/Boneyard/ctest/main.cpp",MemoryMappedFile::CreationFlags::ReadOnly);

View File

@@ -26,38 +26,55 @@ void handleClient(Block<String> &commands);
/// @return /// @return
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int returnCode(0);
Profiler profiler; Profiler profiler;
String version = "1.00";
if(argc<2) std::cout << "sstp version " << version.str() << std::endl;
try
{ {
std::cout << "sstp SERVERMODE {port} | CLIENTMODE {serveripaddress} {serverport} {pathfileName}." << std::endl; if(argc<2)
std::cout << "SERVERMODE will listen on the specified port for a connection and receive the specified file." << std::endl; {
std::cout << "CLIENTMODE willl connect to the specified address and port and send the specfied file." << std::endl; std::cout << "sstp SERVERMODE {port} | CLIENTMODE {serveripaddress} {serverport} {pathfileName}." << std::endl;
return 1; std::cout << "SERVERMODE will listen on the specified port for a connection and receive the specified file." << std::endl;
} std::cout << "CLIENTMODE willl connect to the specified address and port and send the specfied file." << std::endl;
Block<String> arguments; return 1;
for(int index=0;index<argc;index++) }
{ Block<String> arguments;
String item(argv[index]); for(int index=0;index<argc;index++)
arguments.insert(item); {
} String item(argv[index]);
arguments.insert(item);
}
std::cout << argv[1] << std::endl; std::cout << argv[1] << std::endl;
if(arguments[1]=="SERVERMODE") if(arguments[1]=="SERVERMODE")
{ {
handleServer(arguments); handleServer(arguments);
}
else if(arguments[1]=="CLIENTMODE")
{
handleClient(arguments);
}
else
{
std::cout << "Unknown command " << arguments[1] << std::endl;
returnCode=-1;
}
} }
else if(arguments[1]=="CLIENTMODE") catch(Exception& exception)
{ {
handleClient(arguments); std::cout << exception.toString() << std::endl;
} returnCode=-1;
else }
{ catch(...)
std::cout << "Unknown command " << arguments[1] << std::endl; {
std::cout << "An unhandled exception was encountered" << std::endl;
returnCode=-1;
} }
return 0;
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl; std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
return returnCode;
} }
/// @brief [0]=program [1]=SERVERMODE [2]={port} /// @brief [0]=program [1]=SERVERMODE [2]={port}

View File

@@ -105,13 +105,13 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
{ {
double elapsedTimeSeconds = profiler.elapsed()/1000.00; double elapsedTimeSeconds = profiler.elapsed()/1000.00;
double bytesPerSecond = totalBytesRead / elapsedTimeSeconds; double bytesPerSecond = totalBytesRead / elapsedTimeSeconds;
String strBytesPerSecond = Utility::bytesTransferredToString(bytesPerSecond); String strBytesPerSecond = Utility::byteCountToString(bytesPerSecond);
std::cout << "Transferred " << Utility::formatNumber(totalBytesRead) << " of " << Utility::formatNumber(fileLength) << " " << percent << " percent " << strBytesPerSecond << std::endl; std::cout << "Transferred " << Utility::byteCountToString(totalBytesRead,false) << " of " << Utility::byteCountToString(fileLength,false) << " " << percent << " percent " << strBytesPerSecond << std::endl;
} }
} }
std::cout << "Transfer complete" << std::endl; std::cout << "Transfer complete" << std::endl;
std::cout << "Received " << Utility::formatNumber(totalBytesRead) << " in " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl; std::cout << "Received " << Utility::byteCountToString(totalBytesRead,false) << " in " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
writeFile.close(); writeFile.close();
return totalBytesRead==fileLength; return totalBytesRead==fileLength;
} }
@@ -128,7 +128,7 @@ size_t SocketConnectionReceiver::expectPacket(void)
sb.append("Received an invalid PCKT command"); sb.append("Received an invalid PCKT command");
String str=sb.toString(); String str=sb.toString();
std::cout << str << std::endl; std::cout << str << std::endl;
throw new InvalidOperationException(str); throw InvalidOperationException(str);
} }
if(!(subCommands[0]=="PCKT")) if(!(subCommands[0]=="PCKT"))
{ {
@@ -138,7 +138,7 @@ size_t SocketConnectionReceiver::expectPacket(void)
sb.append("Expected PCKT but received ").append(subCommands[0]); sb.append("Expected PCKT but received ").append(subCommands[0]);
String str = sb.toString(); String str = sb.toString();
std::cout << str << std::endl; std::cout << str << std::endl;
throw new InvalidOperationException(str); throw InvalidOperationException(str);
} }
size_t bufferLength = subCommands[1].toULong(); size_t bufferLength = subCommands[1].toULong();
return bufferLength; return bufferLength;