Improve memory management

This commit is contained in:
2025-08-16 09:13:07 -04:00
parent 1da824d8cb
commit aef2d050f5
5 changed files with 82 additions and 8 deletions

View File

@@ -28,7 +28,7 @@ int main(int argc, char **argv)
{
int returnCode(0);
Profiler profiler;
String version = "1.00";
String version = "0.1.0.1"; // major version, minor version, patch, build
std::cout << "sstp version " << version.str() << std::endl;
try
@@ -78,6 +78,8 @@ int main(int argc, char **argv)
}
/// @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
void handleServer(Block<String> &commands)
{

View File

@@ -2,17 +2,36 @@
#include <common/utility.hpp>
#include <common/stringbuffer.hpp>
SocketConnectionReceiver::SocketConnectionReceiver()
: mSocket(-1), mIsRunnable(true)
{
}
/// @brief After a connection has been accepted the SocketConnectionReceiver is initialized with the incoming socket
/// @param socket
/// @param internalSocketAddress
SocketConnectionReceiver::SocketConnectionReceiver(int socket, sockaddr_in internalSocketAddress)
: mSocket(socket), mInternalSocketAddress(internalSocketAddress)
: mSocket(socket), mInternalSocketAddress(internalSocketAddress), mIsRunnable(true)
{
}
/// @brief Initialize a SocketConnectionReceiver.
/// @param socket
/// @param internalSocketAddress
void SocketConnectionReceiver::initialize(int socket, sockaddr_in internalSocketAddress)
{
close();
mSocket=socket;
mInternalSocketAddress = internalSocketAddress;
}
/// @brief The destructor will close the socket
SocketConnectionReceiver::~SocketConnectionReceiver()
{
isRunnable(false);
close();
}
@@ -31,7 +50,7 @@ void SocketConnectionReceiver::close(void)
void SocketConnectionReceiver::threadFunction(int data)
{
String line;
while(true)
while(mIsRunnable)
{
readLine(line);
if(0==line.length())

View File

@@ -16,11 +16,15 @@
class SocketConnectionReceiver
{
public:
SocketConnectionReceiver();
SocketConnectionReceiver(int socket, sockaddr_in inernalSocketAddress);
virtual ~SocketConnectionReceiver();
void initialize(int socket, sockaddr_in inernalSocketAddress);
void close(void);
private:
static constexpr size_t BUFFER_LENGTH=65536; // this is the buffer length for the socket
void isRunnable(bool isRunnable);
bool isRunnable(void);
void threadFunction(int data);
bool handlePut(Block<String> &commands);
void handleQuit(void);
@@ -32,6 +36,19 @@ class SocketConnectionReceiver
friend class SocketServer;
char mBuffer[BUFFER_LENGTH] = {0};
int mSocket;
bool mIsRunnable;
sockaddr_in mInternalSocketAddress;
};
inline
void SocketConnectionReceiver::isRunnable(bool isRunnable)
{
mIsRunnable=isRunnable;
}
inline
bool SocketConnectionReceiver::isRunnable(void)
{
return mIsRunnable;
}
#endif

View File

@@ -40,9 +40,32 @@ SocketServer::SocketServer(int port)
/// @brief Close down the listener
SocketServer::~SocketServer()
{
close();
shutdownConnectionReceivers();
join(); // Then join all socket threads
close(); // close the listener socket
}
/// @brief Join all threads
/// @param
void SocketServer::join(void)
{
for (std::thread& executionThread : mExecutionThreads)
{
if (executionThread.joinable())
{
executionThread.join();
}
}
}
/// @brief Remove object references to the connection receivers. This will force socket threads to exit and close connections
void SocketServer::shutdownConnectionReceivers()
{
mSocketConnectionReceivers.remove();
}
/// @brief returns true if we have a valid socket connection otherwise false
/// @return
bool SocketServer::isOkay(void)
@@ -75,8 +98,14 @@ void SocketServer::listen(void)
close();
return;
}
SocketConnectionReceiver socketConnectionReceiver(socket, internalSocketAddress);
mExecutionThreads.push_back(std::thread(&SocketConnectionReceiver::threadFunction, &socketConnectionReceiver, 0));
mSocketConnectionReceivers.insert(SmartPointer<SocketConnectionReceiver>());
SmartPointer<SocketConnectionReceiver> &pSocketConnectionReceiver = mSocketConnectionReceivers[mSocketConnectionReceivers.size()-1];
pSocketConnectionReceiver = ::new SocketConnectionReceiver(socket, internalSocketAddress);
pSocketConnectionReceiver.disposition(PointerDisposition::Delete);
mExecutionThreads.push_back(std::thread(&SocketConnectionReceiver::threadFunction, *pSocketConnectionReceiver, 0));
// SocketConnectionReceiver socketConnectionReceiver(socket, internalSocketAddress);
// mExecutionThreads.push_back(std::thread(&SocketConnectionReceiver::threadFunction, &socketConnectionReceiver, 0));
}
}

View File

@@ -13,6 +13,9 @@
#include <common/profiler.hpp>
#include <common/block.hpp>
#include <common/fileio.hpp>
#include <common/pointer.hpp>
class SocketConnectionReceiver;
class SocketServer
{
@@ -24,11 +27,15 @@ class SocketServer
bool isOkay(void);
private:
static constexpr int MAX_CONNECTIONS=10; // The maximum connections to be queued at a time
void shutdownConnectionReceivers();
void join(void);
bool mIsOkay;
int mListenPort;
int mSocketFileDescriptor;
struct sockaddr_in mInternalSocketAddress;
socklen_t mAddressLength = sizeof(mInternalSocketAddress);
std::vector<std::thread> mExecutionThreads;
Block<SmartPointer<SocketConnectionReceiver>> mSocketConnectionReceivers;
};
#endif