Refactoring code to use SmartPointers

This commit is contained in:
2025-08-16 18:40:58 -04:00
parent e111d4ab5c
commit ddb25c98b5
6 changed files with 135 additions and 39 deletions

View File

@@ -52,6 +52,7 @@
"semaphore": "cpp", "semaphore": "cpp",
"stop_token": "cpp", "stop_token": "cpp",
"thread": "cpp", "thread": "cpp",
"*.tpp": "cpp" "*.tpp": "cpp",
"csignal": "cpp"
} }
} }

View File

@@ -1,5 +1,11 @@
#include <sstp/clientsocketsender.hpp> #include <sstp/clientsocketsender.hpp>
/// @Brief This is to provide a definition for SmartPointer
ClientSocketSender::ClientSocketSender()
: mIPAddress(""), mPort(0)
{
}
/// @brief Establish a connection to the socket specified by ipAddress and port /// @brief Establish a connection to the socket specified by ipAddress and port
/// @param ipAddress The ipAddress /// @param ipAddress The ipAddress
/// @param port The port /// @param port The port

View File

@@ -16,6 +16,7 @@
class ClientSocketSender class ClientSocketSender
{ {
public: public:
ClientSocketSender();
ClientSocketSender(String ipAddress,int port); ClientSocketSender(String ipAddress,int port);
virtual ~ClientSocketSender(); virtual ~ClientSocketSender();
bool sendFile(String &pathFileName); bool sendFile(String &pathFileName);

View File

@@ -21,6 +21,83 @@ void handleClient(Block<String> &commands); // handler for client mode
bool registerSignalHandler(void); // registers a Control-c handler bool registerSignalHandler(void); // registers a Control-c handler
void signalHandler(int signal); // The Control-C handler void signalHandler(int signal); // The Control-C handler
class SSTP;
SmartPointer<SSTP> sstp;
class SSTP
{
public:
SSTP();
~SSTP();
void handleServer(Block<String>& arguments);
void handleClient(Block<String>& arguments);
private:
SmartPointer<SocketServer> mSocketServer;
SmartPointer<ClientSocketSender> mClientSocketSender;
};
inline
SSTP::SSTP()
{
}
inline
SSTP::~SSTP()
{
mSocketServer.destroy();
mClientSocketSender.destroy();
}
/// @brief [0]=program [1]=SERVERMODE [2]={port}
/// @Note Currently this method will never return. The application is terminate with CTRL-C. Ideally, we would exit is some cleaner fashion
/// perhaps by implementing a CTRL-C hook and then destroying the SocketServer properly by calling it's destructor. See SocketServer::~SocketServer()
/// @param commands
inline
void SSTP::handleServer(Block<String>& commands)
{
Profiler profiler;
if(commands.size()!=3)
{
std::cout << "Missing required parameters" << std::endl;
return;
}
int port = commands[2].toInt();
std::cout << commands[1] << ":" << commands[2] << std::endl;
SocketServer socketServer(commands[2].toInt());
socketServer.listen();
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
}
/// @brief [0]=program, [1]=CLIENTMODE, [2]=serveripaddress, [3]=serverport, [4]=pathfilename
/// @param commands
inline
void SSTP::handleClient(Block<String>& arguments)
{
Profiler profiler;
if(arguments.size()!=5)
{
std::cout << "Missing required parameters" << std::endl;
return;
}
mClientSocketSender.destroy();
mClientSocketSender = new ClientSocketSender(arguments[2],arguments[3].toInt());
bool returnCode = mClientSocketSender->sendFile(arguments[4]);
if(!returnCode)
{
std::cout << "The transfer failed." << std::endl;
}
else
{
std::cout << "Transfer complete" << std::endl;
}
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
}
// ****************************************************************************************************************************************************************
/// @brief /// @brief
/// @param argc /// @param argc
/// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName} /// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName}
@@ -52,15 +129,18 @@ int main(int argc, char **argv)
String item(argv[index]); String item(argv[index]);
arguments.insert(item); arguments.insert(item);
} }
sstp=::new SSTP();
sstp.disposition(PointerDisposition::Delete);
std::cout << argv[1] << std::endl; std::cout << argv[1] << std::endl;
if(arguments[1]=="SERVERMODE") if(arguments[1]=="SERVERMODE")
{ {
handleServer(arguments); sstp->handleServer(arguments);
// handleServer(arguments);
} }
else if(arguments[1]=="CLIENTMODE") else if(arguments[1]=="CLIENTMODE")
{ {
handleClient(arguments); sstp->handleClient(arguments);
// handleClient(arguments);
} }
else else
{ {
@@ -89,6 +169,7 @@ int main(int argc, char **argv)
void signalHandler(int signal) void signalHandler(int signal)
{ {
std::cout << "SSTP Received signal " << signal << std::endl; std::cout << "SSTP Received signal " << signal << std::endl;
if(sstp.isOkay())sstp.destroy();
exit(signal); exit(signal);
} }
@@ -109,47 +190,46 @@ bool registerSignalHandler(void)
return true; return true;
} }
/// @brief [0]=program [1]=SERVERMODE [2]={port} /// @brief [0]=program [1]=SERVERMODE [2]={port}
/// @Note Currently this method will never return. The application is terminate with CTRL-C. Ideally, we would exit is some cleaner fashion /// @Note Currently this method will never return. The application is terminate with CTRL-C. Ideally, we would exit is some cleaner fashion
/// perhaps by implementing a CTRL-C hook and then destroying the SocketServer properly by calling it's destructor. See SocketServer::~SocketServer() /// perhaps by implementing a CTRL-C hook and then destroying the SocketServer properly by calling it's destructor. See SocketServer::~SocketServer()
/// @param commands /// @param commands
void handleServer(Block<String> &commands) // void handleServer(Block<String> &commands)
{ // {
Profiler profiler; // Profiler profiler;
if(commands.size()!=3) // if(commands.size()!=3)
{ // {
std::cout << "Missing required parameters" << std::endl; // std::cout << "Missing required parameters" << std::endl;
return; // return;
} // }
int port = commands[2].toInt(); // int port = commands[2].toInt();
std::cout << commands[1] << ":" << commands[2] << std::endl; // std::cout << commands[1] << ":" << commands[2] << std::endl;
SocketServer socketServer(commands[2].toInt()); // SocketServer socketServer(commands[2].toInt());
socketServer.listen(); // socketServer.listen();
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl; // std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
} // }
/// @brief [0]=program, [1]=CLIENTMODE, [2]=serveripaddress, [3]=serverport, [4]=pathfilename /// @brief [0]=program, [1]=CLIENTMODE, [2]=serveripaddress, [3]=serverport, [4]=pathfilename
/// @param commands /// @param commands
void handleClient(Block<String> &commands) // void handleClient(Block<String> &commands)
{ // {
Profiler profiler; // Profiler profiler;
if(commands.size()!=5) // if(commands.size()!=5)
{ // {
std::cout << "Missing required parameters" << std::endl; // std::cout << "Missing required parameters" << std::endl;
return; // return;
} // }
ClientSocketSender clientSocketSender(commands[2],commands[3].toInt()); // ClientSocketSender clientSocketSender(commands[2],commands[3].toInt());
bool returnCode = clientSocketSender.sendFile(commands[4]); // bool returnCode = clientSocketSender.sendFile(commands[4]);
if(!returnCode) // if(!returnCode)
{ // {
std::cout << "The transfer failed." << std::endl; // std::cout << "The transfer failed." << std::endl;
} // }
else // else
{ // {
std::cout << "Transfer complete" << std::endl; // std::cout << "Transfer complete" << std::endl;
} // }
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl; // std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
} // }

View File

@@ -1,6 +1,13 @@
#include <sstp/socketserver.hpp> #include <sstp/socketserver.hpp>
#include <sstp/socketconnectionreceiver.hpp> #include <sstp/socketconnectionreceiver.hpp>
/// @brief Defauly constructor. This is to provide a definition for SmartPointer
/// @param port
SocketServer::SocketServer()
: mListenPort(-1), mIsOkay(false), mSocketFileDescriptor(-1)
{
}
/// @brief This will construct the socket listener. You must then call listen() to listen for connections /// @brief This will construct the socket listener. You must then call listen() to listen for connections
/// @param port /// @param port
SocketServer::SocketServer(int port) SocketServer::SocketServer(int port)

View File

@@ -20,6 +20,7 @@ class SocketConnectionReceiver;
class SocketServer class SocketServer
{ {
public: public:
SocketServer();
SocketServer(int port); SocketServer(int port);
virtual ~SocketServer(); virtual ~SocketServer();
void listen(void); void listen(void);