Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e111d4ab5c | |||
| aef2d050f5 |
@@ -12,8 +12,13 @@
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <complex>
|
||||
#include <csignal>
|
||||
#include <common/callback.hpp>
|
||||
|
||||
|
||||
bool registerSignalHandler(void); // registers a Control-c handler
|
||||
void signalHandler(int signal); // The Control-C handler
|
||||
|
||||
class Info
|
||||
{
|
||||
public:
|
||||
@@ -104,6 +109,11 @@ void Monitor::doCallback(CallbackData<Info> &callbackData)
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
registerSignalHandler();
|
||||
while(true)
|
||||
{
|
||||
// do stuff
|
||||
}
|
||||
// std::vector<int> myBigVec(10000000, 2011);
|
||||
// std::vector<std::string> s1;
|
||||
// std::vector<std::string> s2;
|
||||
@@ -114,21 +124,42 @@ int main(int argc, char ** argv)
|
||||
// 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);
|
||||
// Monitor monitor;
|
||||
// SmartPointer<B> b(true);
|
||||
// Callback<B,Info> callback(&(*b),&B::callback);
|
||||
// monitor.setCallback(callback);
|
||||
|
||||
Info info;
|
||||
info.setMessage("Hello");
|
||||
// Info info;
|
||||
// info.setMessage("Hello");
|
||||
|
||||
CallbackData<Info> callbackData(info);
|
||||
monitor.doCallback(callbackData);
|
||||
// CallbackData<Info> callbackData(info);
|
||||
// monitor.doCallback(callbackData);
|
||||
|
||||
}
|
||||
|
||||
/// @brief The signal handler
|
||||
void signalHandler(int signal)
|
||||
{
|
||||
std::cout << "Received signal " << signal << std::endl;
|
||||
exit(signal);
|
||||
}
|
||||
|
||||
/// @brief Method that registers the signal handler
|
||||
/// @param
|
||||
bool registerSignalHandler(void)
|
||||
{
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = signalHandler;
|
||||
sigemptyset(&sa.sa_mask); // Clear the mask of blocked signals
|
||||
sa.sa_flags = 0; // No special flags
|
||||
|
||||
if (-1==sigaction(SIGINT, &sa, nullptr))
|
||||
{
|
||||
std::cerr << "Error registering SIGINT handler." << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <csignal>
|
||||
#include <arpa/inet.h>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
@@ -15,22 +16,27 @@
|
||||
#include <sstp/socketserver.hpp>
|
||||
#include <sstp/clientsocketsender.hpp>
|
||||
|
||||
// https://www.man7.org/linux/man-pages/man2/accept.2.html
|
||||
|
||||
void handleServer(Block<String> &commands);
|
||||
void handleClient(Block<String> &commands);
|
||||
void handleServer(Block<String> &commands); // handler for server mode
|
||||
void handleClient(Block<String> &commands); // handler for client mode
|
||||
bool registerSignalHandler(void); // registers a Control-c handler
|
||||
void signalHandler(int signal); // The Control-C handler
|
||||
|
||||
/// @brief
|
||||
/// @param argc
|
||||
/// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName}
|
||||
/// @return
|
||||
/// @return -1:Error 1:Success
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int returnCode(0);
|
||||
Profiler profiler;
|
||||
String version = "1.00";
|
||||
String version = "0.1.0.3"; // major version, minor version, patch, build
|
||||
|
||||
std::cout << "sstp version " << version.str() << std::endl;
|
||||
if(!registerSignalHandler())
|
||||
{
|
||||
std::cout << "Unable to register CTRL-C handler" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
try
|
||||
{
|
||||
if(argc<2)
|
||||
@@ -77,7 +83,36 @@ int main(int argc, char **argv)
|
||||
return returnCode;
|
||||
}
|
||||
|
||||
/// @brief The signal handler
|
||||
/// TODO: If we are sending then we need to destroy the sender, if we are listening then we need to destroy the listener
|
||||
/// Currently we exit the app without cleaning anything up.
|
||||
void signalHandler(int signal)
|
||||
{
|
||||
std::cout << "SSTP Received signal " << signal << std::endl;
|
||||
exit(signal);
|
||||
}
|
||||
|
||||
/// @brief Method that registers the signal handler
|
||||
/// @param
|
||||
bool registerSignalHandler(void)
|
||||
{
|
||||
struct sigaction sa;
|
||||
sa.sa_handler = signalHandler;
|
||||
sigemptyset(&sa.sa_mask); // Clear the mask of blocked signals
|
||||
sa.sa_flags = 0; // No special flags
|
||||
|
||||
if (-1==sigaction(SIGINT, &sa, nullptr))
|
||||
{
|
||||
std::cerr << "Error registering SIGINT handler." << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// @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)
|
||||
{
|
||||
@@ -91,7 +126,7 @@ void handleServer(Block<String> &commands)
|
||||
int port = commands[2].toInt();
|
||||
std::cout << commands[1] << ":" << commands[2] << std::endl;
|
||||
SocketServer socketServer(commands[2].toInt());
|
||||
socketServer.listen();
|
||||
socketServer.listen();
|
||||
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user