Improve memory management
This commit is contained in:
@@ -28,7 +28,7 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
int returnCode(0);
|
int returnCode(0);
|
||||||
Profiler profiler;
|
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;
|
std::cout << "sstp version " << version.str() << std::endl;
|
||||||
try
|
try
|
||||||
@@ -78,6 +78,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// @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
|
||||||
|
/// 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)
|
||||||
{
|
{
|
||||||
@@ -91,7 +93,7 @@ void handleServer(Block<String> &commands)
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,17 +2,36 @@
|
|||||||
#include <common/utility.hpp>
|
#include <common/utility.hpp>
|
||||||
#include <common/stringbuffer.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
|
/// @brief After a connection has been accepted the SocketConnectionReceiver is initialized with the incoming socket
|
||||||
/// @param socket
|
/// @param socket
|
||||||
/// @param internalSocketAddress
|
/// @param internalSocketAddress
|
||||||
SocketConnectionReceiver::SocketConnectionReceiver(int socket, sockaddr_in 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
|
/// @brief The destructor will close the socket
|
||||||
SocketConnectionReceiver::~SocketConnectionReceiver()
|
SocketConnectionReceiver::~SocketConnectionReceiver()
|
||||||
{
|
{
|
||||||
|
isRunnable(false);
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +50,7 @@ void SocketConnectionReceiver::close(void)
|
|||||||
void SocketConnectionReceiver::threadFunction(int data)
|
void SocketConnectionReceiver::threadFunction(int data)
|
||||||
{
|
{
|
||||||
String line;
|
String line;
|
||||||
while(true)
|
while(mIsRunnable)
|
||||||
{
|
{
|
||||||
readLine(line);
|
readLine(line);
|
||||||
if(0==line.length())
|
if(0==line.length())
|
||||||
|
|||||||
@@ -16,11 +16,15 @@
|
|||||||
class SocketConnectionReceiver
|
class SocketConnectionReceiver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
SocketConnectionReceiver();
|
||||||
SocketConnectionReceiver(int socket, sockaddr_in inernalSocketAddress);
|
SocketConnectionReceiver(int socket, sockaddr_in inernalSocketAddress);
|
||||||
virtual ~SocketConnectionReceiver();
|
virtual ~SocketConnectionReceiver();
|
||||||
|
void initialize(int socket, sockaddr_in inernalSocketAddress);
|
||||||
void close(void);
|
void close(void);
|
||||||
private:
|
private:
|
||||||
static constexpr size_t BUFFER_LENGTH=65536; // this is the buffer length for the socket
|
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);
|
void threadFunction(int data);
|
||||||
bool handlePut(Block<String> &commands);
|
bool handlePut(Block<String> &commands);
|
||||||
void handleQuit(void);
|
void handleQuit(void);
|
||||||
@@ -32,6 +36,19 @@ class SocketConnectionReceiver
|
|||||||
friend class SocketServer;
|
friend class SocketServer;
|
||||||
char mBuffer[BUFFER_LENGTH] = {0};
|
char mBuffer[BUFFER_LENGTH] = {0};
|
||||||
int mSocket;
|
int mSocket;
|
||||||
|
bool mIsRunnable;
|
||||||
sockaddr_in mInternalSocketAddress;
|
sockaddr_in mInternalSocketAddress;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline
|
||||||
|
void SocketConnectionReceiver::isRunnable(bool isRunnable)
|
||||||
|
{
|
||||||
|
mIsRunnable=isRunnable;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline
|
||||||
|
bool SocketConnectionReceiver::isRunnable(void)
|
||||||
|
{
|
||||||
|
return mIsRunnable;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -40,9 +40,32 @@ SocketServer::SocketServer(int port)
|
|||||||
/// @brief Close down the listener
|
/// @brief Close down the listener
|
||||||
SocketServer::~SocketServer()
|
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
|
/// @brief returns true if we have a valid socket connection otherwise false
|
||||||
/// @return
|
/// @return
|
||||||
bool SocketServer::isOkay(void)
|
bool SocketServer::isOkay(void)
|
||||||
@@ -75,8 +98,14 @@ void SocketServer::listen(void)
|
|||||||
close();
|
close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SocketConnectionReceiver socketConnectionReceiver(socket, internalSocketAddress);
|
mSocketConnectionReceivers.insert(SmartPointer<SocketConnectionReceiver>());
|
||||||
mExecutionThreads.push_back(std::thread(&SocketConnectionReceiver::threadFunction, &socketConnectionReceiver, 0));
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,9 @@
|
|||||||
#include <common/profiler.hpp>
|
#include <common/profiler.hpp>
|
||||||
#include <common/block.hpp>
|
#include <common/block.hpp>
|
||||||
#include <common/fileio.hpp>
|
#include <common/fileio.hpp>
|
||||||
|
#include <common/pointer.hpp>
|
||||||
|
|
||||||
|
class SocketConnectionReceiver;
|
||||||
|
|
||||||
class SocketServer
|
class SocketServer
|
||||||
{
|
{
|
||||||
@@ -24,11 +27,15 @@ class SocketServer
|
|||||||
bool isOkay(void);
|
bool isOkay(void);
|
||||||
private:
|
private:
|
||||||
static constexpr int MAX_CONNECTIONS=10; // The maximum connections to be queued at a time
|
static constexpr int MAX_CONNECTIONS=10; // The maximum connections to be queued at a time
|
||||||
|
void shutdownConnectionReceivers();
|
||||||
|
void join(void);
|
||||||
|
|
||||||
bool mIsOkay;
|
bool mIsOkay;
|
||||||
int mListenPort;
|
int mListenPort;
|
||||||
int mSocketFileDescriptor;
|
int mSocketFileDescriptor;
|
||||||
struct sockaddr_in mInternalSocketAddress;
|
struct sockaddr_in mInternalSocketAddress;
|
||||||
socklen_t mAddressLength = sizeof(mInternalSocketAddress);
|
socklen_t mAddressLength = sizeof(mInternalSocketAddress);
|
||||||
std::vector<std::thread> mExecutionThreads;
|
std::vector<std::thread> mExecutionThreads;
|
||||||
|
Block<SmartPointer<SocketConnectionReceiver>> mSocketConnectionReceivers;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user