Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9abd4b195b | |||
| f7facd3319 | |||
| 2e4a713b1c | |||
| 6c70a3c141 | |||
| ddc9b473cd | |||
| ddb25c98b5 | |||
| e111d4ab5c | |||
| aef2d050f5 | |||
| 1da824d8cb | |||
| f441b186b5 |
@@ -3,6 +3,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <sys/statvfs.h>
|
||||||
#include <common/string.hpp>
|
#include <common/string.hpp>
|
||||||
|
|
||||||
class Utility
|
class Utility
|
||||||
@@ -15,7 +17,8 @@ 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);
|
||||||
|
static size_t getAvailableDiskSpace(const String &path);
|
||||||
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 +37,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
|
||||||
@@ -167,4 +175,23 @@ bool Utility::fmod(double dividend, double divisor)
|
|||||||
// Check if the absolute remainder is less than epsilon
|
// Check if the absolute remainder is less than epsilon
|
||||||
return std::fabs(remainder) < epsilon;
|
return std::fabs(remainder) < epsilon;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// inline
|
||||||
|
// size_t Utility::getAvailableDiskSpace(const String& path)
|
||||||
|
// {
|
||||||
|
// struct statvfs stat;
|
||||||
|
// if (::statvfs(path, &stat) != 0)
|
||||||
|
// {
|
||||||
|
// throw new Exception("Error calling statvfs to get available disk space.");
|
||||||
|
// }
|
||||||
|
// // Calculate available space in bytes. Multiply Number of free inodes * Fragment size
|
||||||
|
// return (size_t)stat.f_bavail * stat.f_frsize;
|
||||||
|
// }
|
||||||
|
|
||||||
|
inline
|
||||||
|
size_t Utility::getAvailableDiskSpace(const String& path)
|
||||||
|
{
|
||||||
|
std::filesystem::space_info si = std::filesystem::space(path.str());
|
||||||
|
return si.available;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -12,8 +12,13 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <complex>
|
#include <complex>
|
||||||
|
#include <csignal>
|
||||||
#include <common/callback.hpp>
|
#include <common/callback.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
bool registerSignalHandler(void); // registers a Control-c handler
|
||||||
|
void signalHandler(int signal); // The Control-C handler
|
||||||
|
|
||||||
class Info
|
class Info
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -104,6 +109,11 @@ void Monitor::doCallback(CallbackData<Info> &callbackData)
|
|||||||
|
|
||||||
int main(int argc, char ** argv)
|
int main(int argc, char ** argv)
|
||||||
{
|
{
|
||||||
|
registerSignalHandler();
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
// do stuff
|
||||||
|
}
|
||||||
// std::vector<int> myBigVec(10000000, 2011);
|
// std::vector<int> myBigVec(10000000, 2011);
|
||||||
// std::vector<std::string> s1;
|
// std::vector<std::string> s1;
|
||||||
// std::vector<std::string> s2;
|
// std::vector<std::string> s2;
|
||||||
@@ -114,21 +124,42 @@ int main(int argc, char ** argv)
|
|||||||
// s2 = std::move(s1);
|
// s2 = std::move(s1);
|
||||||
|
|
||||||
|
|
||||||
Monitor monitor;
|
// Monitor monitor;
|
||||||
// SmartPointer<B> b(::new B(),PointerDisposition::Delete);
|
// SmartPointer<B> b(true);
|
||||||
SmartPointer<B> b(true);
|
// Callback<B,Info> callback(&(*b),&B::callback);
|
||||||
Callback<B,Info> callback(&(*b),&B::callback);
|
// monitor.setCallback(callback);
|
||||||
monitor.setCallback(callback);
|
|
||||||
|
|
||||||
Info info;
|
// Info info;
|
||||||
info.setMessage("Hello");
|
// info.setMessage("Hello");
|
||||||
|
|
||||||
CallbackData<Info> callbackData(info);
|
// CallbackData<Info> callbackData(info);
|
||||||
monitor.doCallback(callbackData);
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
5
sstp/.vscode/settings.json
vendored
5
sstp/.vscode/settings.json
vendored
@@ -52,6 +52,9 @@
|
|||||||
"semaphore": "cpp",
|
"semaphore": "cpp",
|
||||||
"stop_token": "cpp",
|
"stop_token": "cpp",
|
||||||
"thread": "cpp",
|
"thread": "cpp",
|
||||||
"*.tpp": "cpp"
|
"*.tpp": "cpp",
|
||||||
|
"csignal": "cpp",
|
||||||
|
"codecvt": "cpp",
|
||||||
|
"iomanip": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.10.0)
|
|||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/home/pi/CPP")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/home/pi/CPP")
|
||||||
project(sstp VERSION 0.1.0 LANGUAGES C CXX)
|
project(sstp VERSION 0.1.0 LANGUAGES C CXX)
|
||||||
|
|
||||||
add_executable(sstp main.cpp clientsocketsender.cpp socketconnectionreceiver.cpp socketserver.cpp)
|
add_executable(sstp main.cpp clientsocketsender.cpp socketconnectionreceiver.cpp socketserver.cpp sstp.cpp)
|
||||||
|
|
||||||
add_subdirectory(/home/pi/CPP/common /home/pi/CPP/common/build)
|
add_subdirectory(/home/pi/CPP/common /home/pi/CPP/common/build)
|
||||||
target_link_libraries(sstp PRIVATE common)
|
target_link_libraries(sstp PRIVATE common)
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
#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
|
||||||
|
/// @param ipAddress The ipAddress
|
||||||
|
/// @param port The port
|
||||||
ClientSocketSender::ClientSocketSender(String ipAddress, int port)
|
ClientSocketSender::ClientSocketSender(String ipAddress, int port)
|
||||||
: mIPAddress(ipAddress), mPort(port)
|
: mIPAddress(ipAddress), mPort(port)
|
||||||
{
|
{
|
||||||
@@ -26,20 +35,26 @@ ClientSocketSender::ClientSocketSender(String ipAddress, int port)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Close the connection on destruction
|
||||||
ClientSocketSender::~ClientSocketSender()
|
ClientSocketSender::~ClientSocketSender()
|
||||||
{
|
{
|
||||||
|
std::cout << "~ClientSocketSender" << std::endl;
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Closes the connection
|
||||||
|
/// @param
|
||||||
void ClientSocketSender::close(void)
|
void ClientSocketSender::close(void)
|
||||||
{
|
{
|
||||||
if(-1!=mSocket)
|
if(-1==mSocket)return;
|
||||||
{
|
std::cout << "ClientSocketSender::close, closing socket" << std::endl;
|
||||||
::close(mSocket);
|
::close(mSocket);
|
||||||
mSocket=-1;
|
mSocket=-1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Send the specified file through the connection
|
||||||
|
/// @param pathFileName The file to send
|
||||||
|
/// @return Returns true if successful
|
||||||
bool ClientSocketSender::sendFile(String &pathFileName)
|
bool ClientSocketSender::sendFile(String &pathFileName)
|
||||||
{
|
{
|
||||||
Profiler profiler;
|
Profiler profiler;
|
||||||
@@ -73,6 +88,9 @@ bool ClientSocketSender::sendFile(String &pathFileName)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Send PCKT with length of packet
|
||||||
|
/// @param bytesToSend
|
||||||
|
/// @return
|
||||||
bool ClientSocketSender::sendPacketIndicator(DWORD bytesToSend)
|
bool ClientSocketSender::sendPacketIndicator(DWORD bytesToSend)
|
||||||
{
|
{
|
||||||
if(!isOkay())return false;
|
if(!isOkay())return false;
|
||||||
@@ -83,6 +101,10 @@ bool ClientSocketSender::sendPacketIndicator(DWORD bytesToSend)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Send PUT with filename and file length
|
||||||
|
/// @param fileName
|
||||||
|
/// @param fileLength
|
||||||
|
/// @return
|
||||||
bool ClientSocketSender::sendPutIndicator(String fileName,DWORD fileLength)
|
bool ClientSocketSender::sendPutIndicator(String fileName,DWORD fileLength)
|
||||||
{
|
{
|
||||||
if(!isOkay())return false;
|
if(!isOkay())return false;
|
||||||
@@ -97,6 +119,9 @@ bool ClientSocketSender::sendPutIndicator(String fileName,DWORD fileLength)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Send QUIT
|
||||||
|
/// @param
|
||||||
|
/// @return
|
||||||
bool ClientSocketSender::sendQuit(void)
|
bool ClientSocketSender::sendQuit(void)
|
||||||
{
|
{
|
||||||
if(!isOkay())return false;
|
if(!isOkay())return false;
|
||||||
@@ -105,6 +130,10 @@ bool ClientSocketSender::sendQuit(void)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief items in buffer with length bytesToSend
|
||||||
|
/// @param buffer The buffer to send
|
||||||
|
/// @param bytesToSend The number of bytes to send
|
||||||
|
/// @return
|
||||||
bool ClientSocketSender::sendPacket(Array<char> &buffer,DWORD bytesToSend)
|
bool ClientSocketSender::sendPacket(Array<char> &buffer,DWORD bytesToSend)
|
||||||
{
|
{
|
||||||
if(!isOkay())return false;
|
if(!isOkay())return false;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef _LISTENER_CLIENTSOCKETSENDER_HPP_
|
#ifndef _SSTP_CLIENTSOCKETSENDER_HPP_
|
||||||
#define _LISTENER_CLIENTSOCKETSENDER_HPP_
|
#define _SSTP_CLIENTSOCKETSENDER_HPP_
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -16,13 +16,13 @@
|
|||||||
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);
|
||||||
bool isOkay(void);
|
bool isOkay(void);
|
||||||
private:
|
private:
|
||||||
// static constexpr int BUFFER_LENGTH=65536;
|
static constexpr int BUFFER_LENGTH=1048576; // This buffer length is used for both reading the file and also the packet transmission length
|
||||||
static constexpr int BUFFER_LENGTH=1048576;
|
|
||||||
void close(void);
|
void close(void);
|
||||||
bool sendPacketIndicator(DWORD bytesToSend);
|
bool sendPacketIndicator(DWORD bytesToSend);
|
||||||
bool sendPacket(Array<char> &buffer,DWORD bytesToSend);
|
bool sendPacket(Array<char> &buffer,DWORD bytesToSend);
|
||||||
|
|||||||
143
sstp/main.cpp
143
sstp/main.cpp
@@ -1,103 +1,112 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sys/types.h>
|
#include <csignal>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <thread>
|
|
||||||
#include <vector>
|
|
||||||
#include <common/string.hpp>
|
#include <common/string.hpp>
|
||||||
#include <common/profiler.hpp>
|
#include <common/profiler.hpp>
|
||||||
#include <common/block.hpp>
|
#include <common/block.hpp>
|
||||||
#include <common/fileio.hpp>
|
|
||||||
#include <common/utility.hpp>
|
#include <common/utility.hpp>
|
||||||
#include <sstp/socketserver.hpp>
|
#include <sstp/socketserver.hpp>
|
||||||
#include <sstp/clientsocketsender.hpp>
|
#include <sstp/clientsocketsender.hpp>
|
||||||
|
#include <sstp/sstp.hpp>
|
||||||
|
|
||||||
// https://www.man7.org/linux/man-pages/man2/accept.2.html
|
// To Clean: From the top menu choose "View", "Command Palette", Then choose CMake: Clean OR /usr/bin/cmake --build /home/pi/CPP/sstp/build --config Debug --target clean --
|
||||||
|
// To Build: From the top menu choose "View", "Command Palette", Then choose CMake: Build OR /usr/bin/cmake --build /home/pi/CPP/sstp/build --config Debug --target all --
|
||||||
|
// Output will be here. /home/pi/CPP/sstp/build
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
SmartPointer<SSTP> sstp;
|
||||||
|
|
||||||
/// @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}
|
||||||
/// @return
|
/// @return -1:Error 1:Success
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
int returnCode(0);
|
||||||
Profiler profiler;
|
Profiler profiler;
|
||||||
|
String version = "0.1.0.5"; // major version, minor version, patch, build
|
||||||
|
|
||||||
if(argc<2)
|
std::cout << "sstp version " << version.str() << std::endl;
|
||||||
|
if(!registerSignalHandler())
|
||||||
{
|
{
|
||||||
std::cout << "sstp SERVERMODE {port} | CLIENTMODE {serveripaddress} {serverport} {pathfileName}." << std::endl;
|
std::cout << "Unable to register CTRL-C handler" << std::endl;
|
||||||
std::cout << "SERVERMODE will listen on the specified port for a connection and receive the specified file." << std::endl;
|
return -1;
|
||||||
std::cout << "CLIENTMODE willl connect to the specified address and port and send the specfied file." << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
Block<String> arguments;
|
try
|
||||||
for(int index=0;index<argc;index++)
|
|
||||||
{
|
{
|
||||||
String item(argv[index]);
|
if(argc<2)
|
||||||
arguments.insert(item);
|
{
|
||||||
|
std::cout << "sstp SERVERMODE {port} | CLIENTMODE {serveripaddress} {serverport} {pathfileName}." << std::endl;
|
||||||
|
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;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
Block<String> arguments;
|
||||||
|
for(int index=0;index<argc;index++)
|
||||||
|
{
|
||||||
|
String item(argv[index]);
|
||||||
|
arguments.insert(item);
|
||||||
|
}
|
||||||
|
sstp=::new SSTP();
|
||||||
|
sstp.disposition(PointerDisposition::Delete);
|
||||||
|
std::cout << argv[1] << std::endl;
|
||||||
|
if(arguments[1]=="SERVERMODE")
|
||||||
|
{
|
||||||
|
sstp->handleServer(arguments);
|
||||||
|
}
|
||||||
|
else if(arguments[1]=="CLIENTMODE")
|
||||||
|
{
|
||||||
|
sstp->handleClient(arguments);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Unknown command " << arguments[1] << std::endl;
|
||||||
|
returnCode=-1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch(Exception& exception)
|
||||||
std::cout << argv[1] << std::endl;
|
|
||||||
if(arguments[1]=="SERVERMODE")
|
|
||||||
{
|
{
|
||||||
handleServer(arguments);
|
std::cout << exception.toString() << std::endl;
|
||||||
|
returnCode=-1;
|
||||||
}
|
}
|
||||||
else if(arguments[1]=="CLIENTMODE")
|
catch(...)
|
||||||
{
|
{
|
||||||
handleClient(arguments);
|
std::cout << "An unhandled exception was encountered" << std::endl;
|
||||||
}
|
returnCode=-1;
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "Unknown command " << arguments[1] << std::endl;
|
|
||||||
}
|
}
|
||||||
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 The signal handler
|
||||||
/// @param commands
|
/// TODO: If we are sending then we need to destroy the sender, if we are listening then we need to destroy the listener
|
||||||
void handleServer(Block<String> &commands)
|
/// Currently we exit the app without cleaning anything up.
|
||||||
|
void signalHandler(int signal)
|
||||||
{
|
{
|
||||||
Profiler profiler;
|
std::cout << "SSTP Received signal " << signal << std::endl;
|
||||||
|
if(sstp.isOkay())sstp.destroy();
|
||||||
if(commands.size()!=3)
|
exit(signal);
|
||||||
{
|
|
||||||
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
|
/// @brief Method that registers the signal handler
|
||||||
/// @param commands
|
/// @param
|
||||||
void handleClient(Block<String> &commands)
|
bool registerSignalHandler(void)
|
||||||
{
|
{
|
||||||
Profiler profiler;
|
struct sigaction sa;
|
||||||
if(commands.size()!=5)
|
sa.sa_handler = signalHandler;
|
||||||
{
|
sigemptyset(&sa.sa_mask); // Clear the mask of blocked signals
|
||||||
std::cout << "Missing required parameters" << std::endl;
|
sa.sa_flags = 0; // No special flags
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClientSocketSender clientSocketSender(commands[2],commands[3].toInt());
|
if (-1==sigaction(SIGINT, &sa, nullptr))
|
||||||
bool returnCode = clientSocketSender.sendFile(commands[4]);
|
|
||||||
if(!returnCode)
|
|
||||||
{
|
{
|
||||||
std::cout << "The transfer failed." << std::endl;
|
std::cerr << "Error registering SIGINT handler." << std::endl;
|
||||||
}
|
return false;
|
||||||
else
|
}
|
||||||
{
|
return true;
|
||||||
std::cout << "Transfer complete" << std::endl;
|
|
||||||
}
|
|
||||||
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,15 +2,41 @@
|
|||||||
#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
|
||||||
|
/// @param socket
|
||||||
|
/// @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
|
||||||
SocketConnectionReceiver::~SocketConnectionReceiver()
|
SocketConnectionReceiver::~SocketConnectionReceiver()
|
||||||
{
|
{
|
||||||
|
isRunnable(false);
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Close the socket and set the mSocket variable to -1
|
||||||
|
/// @param
|
||||||
void SocketConnectionReceiver::close(void)
|
void SocketConnectionReceiver::close(void)
|
||||||
{
|
{
|
||||||
if(-1==mSocket)return;
|
if(-1==mSocket)return;
|
||||||
@@ -19,10 +45,12 @@ void SocketConnectionReceiver::close(void)
|
|||||||
mSocket=-1;
|
mSocket=-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief The threadFunction will handle the lifetime of the socket connection. When a QUIT is received the socket will be closed and the thread method will exit
|
||||||
|
/// @param data
|
||||||
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())
|
||||||
@@ -30,7 +58,11 @@ void SocketConnectionReceiver::threadFunction(int data)
|
|||||||
std::cout << "Received an empty command" << std::endl;
|
std::cout << "Received an empty command" << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(line=="QUIT" || line=="quit")break;
|
if(line=="QUIT" || line=="quit")
|
||||||
|
{
|
||||||
|
handleQuit();
|
||||||
|
break;
|
||||||
|
}
|
||||||
Block<String> commands = line.split('|');
|
Block<String> commands = line.split('|');
|
||||||
if(0==commands.size())continue;
|
if(0==commands.size())continue;
|
||||||
commands[0].upper();
|
commands[0].upper();
|
||||||
@@ -49,9 +81,9 @@ void SocketConnectionReceiver::threadFunction(int data)
|
|||||||
|
|
||||||
/// @brief The client wants to put a file. put {filename} {lengthbytes}
|
/// @brief The client wants to put a file. put {filename} {lengthbytes}
|
||||||
/// @param commands [0]=PUT, [1]=filename, [2]=total length
|
/// @param commands [0]=PUT, [1]=filename, [2]=total length
|
||||||
/// This will read PCKT {length}.
|
/// This message contains PUT|{filename}|{lengthbytes} followed by as many PCKT|{length} packets as needed to read the entire file contents and save to disk
|
||||||
/// The connection will continue reading and writing until it receives PCKT 0
|
/// The connection will continue reading packets and saving data to disk until it receives PCKT 0 which signals the end of transmission
|
||||||
/// @return
|
/// @return true or false
|
||||||
bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
||||||
{
|
{
|
||||||
Profiler profiler;
|
Profiler profiler;
|
||||||
@@ -61,10 +93,21 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
|||||||
String fileName;
|
String fileName;
|
||||||
size_t fileLength;
|
size_t fileLength;
|
||||||
size_t totalBytesRead=0;
|
size_t totalBytesRead=0;
|
||||||
|
size_t totalPacketsRead=0;
|
||||||
|
size_t availableDiskSpace=0;
|
||||||
|
|
||||||
fileName = commands[1];
|
fileName = commands[1];
|
||||||
fileLength = commands[commands.size()-1].toLong();
|
fileLength = commands[commands.size()-1].toLong();
|
||||||
std::cout << "PUT" << " " << fileName << " " << fileLength << std::endl;
|
std::cout << "PUT" << " " << fileName << " " << Utility::byteCountToString(fileLength) << std::endl;
|
||||||
|
|
||||||
|
availableDiskSpace=Utility::getAvailableDiskSpace(String("./"));
|
||||||
|
std::cout << Utility::byteCountToString(availableDiskSpace,false) << " available space on disk" << std::endl;
|
||||||
|
if(fileLength > availableDiskSpace)
|
||||||
|
{
|
||||||
|
std::cout << "Unsufficient space on disk. Required space " << Utility::byteCountToString(availableDiskSpace,false) << " , available space" << Utility::byteCountToString(availableDiskSpace,false) << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
FileIO writeFile(fileName, FileIO::ByteOrder::LittleEndian, FileIO::Mode::ReadWrite,FileIO::CreationFlags::CreateAlways);
|
FileIO writeFile(fileName, FileIO::ByteOrder::LittleEndian, FileIO::Mode::ReadWrite,FileIO::CreationFlags::CreateAlways);
|
||||||
|
|
||||||
if(!writeFile.isOkay())
|
if(!writeFile.isOkay())
|
||||||
@@ -93,6 +136,7 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
|||||||
receiveBuffer.size(bufferLength);
|
receiveBuffer.size(bufferLength);
|
||||||
size_t bytes_read = read(receiveBuffer);
|
size_t bytes_read = read(receiveBuffer);
|
||||||
totalBytesRead+=bytes_read;
|
totalBytesRead+=bytes_read;
|
||||||
|
totalPacketsRead++;
|
||||||
if(bufferLength!=bytes_read)
|
if(bufferLength!=bytes_read)
|
||||||
{
|
{
|
||||||
std::cout << "Send/Receive size mismatch. The client indicated a data length of " << Utility::formatNumber(bufferLength) << " but a data length of " << Utility::formatNumber(bytes_read) << " was received";
|
std::cout << "Send/Receive size mismatch. The client indicated a data length of " << Utility::formatNumber(bufferLength) << " but a data length of " << Utility::formatNumber(bytes_read) << " was received";
|
||||||
@@ -105,17 +149,19 @@ 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 << "Received " << Utility::formatNumber(totalBytesRead) << " in " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
|
|
||||||
writeFile.close();
|
writeFile.close();
|
||||||
|
std::cout << "Transfer complete. Total packets received " << Utility::formatNumber(totalPacketsRead) << std::endl;
|
||||||
|
std::cout << "Received " << Utility::byteCountToString(totalBytesRead,false) << " in " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
|
||||||
return totalBytesRead==fileLength;
|
return totalBytesRead==fileLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Read a PCKT message from the socket and return the length. This message is of the form PCKT|{length} and the method returns the second parameter (length)
|
||||||
|
/// @param
|
||||||
|
/// @return
|
||||||
size_t SocketConnectionReceiver::expectPacket(void)
|
size_t SocketConnectionReceiver::expectPacket(void)
|
||||||
{
|
{
|
||||||
Block<String> subCommands;
|
Block<String> subCommands;
|
||||||
@@ -128,7 +174,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,12 +184,15 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Read a '\r\n' terminated string from the socket. Return the number of bytes read
|
||||||
|
/// @param line The string to read data into
|
||||||
|
/// @return
|
||||||
size_t SocketConnectionReceiver::readLine(String &line)
|
size_t SocketConnectionReceiver::readLine(String &line)
|
||||||
{
|
{
|
||||||
size_t bytes_read = 0;
|
size_t bytes_read = 0;
|
||||||
@@ -169,6 +218,9 @@ size_t SocketConnectionReceiver::readLine(String &line)
|
|||||||
return total_bytes;
|
return total_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Read bytes from a socket into the provided buffer.
|
||||||
|
/// @param buffer The buffer must be pre-allocated to a given length. The read operation will continue until all bytes in the buffer have been read
|
||||||
|
/// @return
|
||||||
size_t SocketConnectionReceiver::read(Array<char> &buffer)
|
size_t SocketConnectionReceiver::read(Array<char> &buffer)
|
||||||
{
|
{
|
||||||
size_t bytes_read = 0;
|
size_t bytes_read = 0;
|
||||||
@@ -185,3 +237,10 @@ size_t SocketConnectionReceiver::read(Array<char> &buffer)
|
|||||||
}
|
}
|
||||||
return total_bytes_read;
|
return total_bytes_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Handle a QUIT message... which just displays
|
||||||
|
/// @param
|
||||||
|
void SocketConnectionReceiver::handleQuit(void)
|
||||||
|
{
|
||||||
|
std::cout << "Received QUIT" << std::endl;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#ifndef _LISTENER_SOCKETCONNECTIONRECEIVER_HPP_
|
#ifndef _SSTP_SOCKETCONNECTIONRECEIVER_HPP_
|
||||||
#define _LISTENER_SOCKETCONNECTIONRECEIVER_HPP_
|
#define _SSTP_SOCKETCONNECTIONRECEIVER_HPP_
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -16,13 +16,18 @@
|
|||||||
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);
|
||||||
size_t readLine(String &line);
|
size_t readLine(String &line);
|
||||||
size_t expectPacket(void);
|
size_t expectPacket(void);
|
||||||
size_t read(Array<char> &buffer);
|
size_t read(Array<char> &buffer);
|
||||||
@@ -31,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
|
||||||
@@ -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)
|
||||||
@@ -25,8 +32,6 @@ SocketServer::SocketServer(int port)
|
|||||||
// bind socket to network address and port
|
// bind socket to network address and port
|
||||||
mInternalSocketAddress.sin_family = AF_INET;
|
mInternalSocketAddress.sin_family = AF_INET;
|
||||||
mInternalSocketAddress.sin_addr.s_addr=INADDR_ANY;
|
mInternalSocketAddress.sin_addr.s_addr=INADDR_ANY;
|
||||||
|
|
||||||
|
|
||||||
mInternalSocketAddress.sin_port = htons(mListenPort);
|
mInternalSocketAddress.sin_port = htons(mListenPort);
|
||||||
result = ::bind(mSocketFileDescriptor,(struct sockaddr*)&mInternalSocketAddress,sizeof(mInternalSocketAddress));
|
result = ::bind(mSocketFileDescriptor,(struct sockaddr*)&mInternalSocketAddress,sizeof(mInternalSocketAddress));
|
||||||
if(result)
|
if(result)
|
||||||
@@ -37,17 +42,50 @@ SocketServer::SocketServer(int port)
|
|||||||
mIsOkay=true;
|
mIsOkay=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Close down the listener
|
||||||
SocketServer::~SocketServer()
|
SocketServer::~SocketServer()
|
||||||
{
|
{
|
||||||
std::cout << "~SocketServer()" << std::endl;
|
std::cout << "~SocketServer" << std::endl;
|
||||||
close();
|
std::cout << "shutdownConnectionReceivers" << std::endl;
|
||||||
|
shutdownConnectionReceivers();
|
||||||
|
std::cout << "join" << std::endl;
|
||||||
|
join(); // Then join all socket threads
|
||||||
|
std::cout << "close" << std::endl;
|
||||||
|
close(); // close the listener socket
|
||||||
|
std::cout << "~SocketServer, done" << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @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)
|
bool SocketServer::isOkay(void)
|
||||||
{
|
{
|
||||||
return mIsOkay;
|
return mIsOkay;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief This is the listener. It will wait for incoming connections.
|
||||||
|
/// When an incoming connection is received it will create a SocketConnectionReceiver object and call it's threadFunction to handle further interactions
|
||||||
|
/// such as transmitting a file.
|
||||||
void SocketServer::listen(void)
|
void SocketServer::listen(void)
|
||||||
{
|
{
|
||||||
while(isOkay())
|
while(isOkay())
|
||||||
@@ -70,16 +108,20 @@ 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Shut down the listener socket
|
||||||
|
/// @param
|
||||||
void SocketServer::close(void)
|
void SocketServer::close(void)
|
||||||
{
|
{
|
||||||
if(-1!=mSocketFileDescriptor)
|
if(-1==mSocketFileDescriptor)return;
|
||||||
{
|
std::cout << "SocketServer::close, closing socket" << std::endl;
|
||||||
::close(mSocketFileDescriptor);
|
::close(mSocketFileDescriptor);
|
||||||
mSocketFileDescriptor=-1;
|
mSocketFileDescriptor=-1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,14 @@
|
|||||||
#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
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
SocketServer();
|
||||||
SocketServer(int port);
|
SocketServer(int port);
|
||||||
virtual ~SocketServer();
|
virtual ~SocketServer();
|
||||||
void listen(void);
|
void listen(void);
|
||||||
@@ -24,11 +28,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
|
||||||
52
sstp/sstp.cpp
Normal file
52
sstp/sstp.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <sstp/sstp.hpp>
|
||||||
|
#include <common/profiler.hpp>
|
||||||
|
#include <common/utility.hpp>
|
||||||
|
#include <sstp/socketserver.hpp>
|
||||||
|
#include <sstp/clientsocketsender.hpp>
|
||||||
|
|
||||||
|
/// @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 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;
|
||||||
|
mSocketServer = ::new SocketServer(commands[2].toInt());
|
||||||
|
mSocketServer.disposition(PointerDisposition::Delete);
|
||||||
|
mSocketServer->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
|
||||||
|
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());
|
||||||
|
mClientSocketSender.disposition(PointerDisposition::Delete);
|
||||||
|
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;
|
||||||
|
}
|
||||||
33
sstp/sstp.hpp
Normal file
33
sstp/sstp.hpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef _SSTP_SSTP_HPP_
|
||||||
|
#define _SSTP_SSTP_HPP_
|
||||||
|
#include <common/block.hpp>
|
||||||
|
#include <common/pointer.hpp>
|
||||||
|
|
||||||
|
class SocketServer;
|
||||||
|
class ClientSocketSender;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user