Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aef2d050f5 | |||
| 1da824d8cb | |||
| f441b186b5 |
@@ -15,7 +15,7 @@ class Utility
|
||||
static String formatCurrency(double number,int places=2);
|
||||
static bool fmod(double dividend,double divisor);
|
||||
static bool fmod(double dividend, double divisor,double epsilon);
|
||||
static String bytesTransferredToString(double bytesTransferred);
|
||||
static String byteCountToString(double bytesTransferred, bool perSecond = true);
|
||||
private:
|
||||
static String pad(String theString,char padChar,int toLength);
|
||||
static String addCommas(String& theString);
|
||||
@@ -34,30 +34,35 @@ Utility::~Utility()
|
||||
}
|
||||
|
||||
inline
|
||||
String Utility::bytesTransferredToString(double bytesTransferred)
|
||||
String Utility::byteCountToString(double bytesTransferred, bool perSecond)
|
||||
{
|
||||
double roundedValue = std::round(bytesTransferred * 100.00)/100.00;
|
||||
String strBytesTransferrred;
|
||||
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)
|
||||
{
|
||||
return Utility::formatNumber(roundedValue/1000000000000,2) + "Tb/s";
|
||||
strBytesTransferrred = Utility::formatNumber(roundedValue/1000000000000,2) + "Tb";
|
||||
}
|
||||
else if(roundedValue >= 1073741824)
|
||||
{
|
||||
return Utility::formatNumber(roundedValue/1073741824,2) + "Gbb/s";
|
||||
strBytesTransferrred = Utility::formatNumber(roundedValue/1073741824,2) + "Gb";
|
||||
}
|
||||
else if(roundedValue >= 1000000)
|
||||
{
|
||||
return Utility::formatNumber(roundedValue/1000000,2) + "Mb/s";
|
||||
strBytesTransferrred = Utility::formatNumber(roundedValue/1000000,2) + "Mb";
|
||||
}
|
||||
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
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include <sstp/clientsocketsender.hpp>
|
||||
|
||||
/// @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)
|
||||
: mIPAddress(ipAddress), mPort(port)
|
||||
{
|
||||
@@ -26,11 +29,15 @@ ClientSocketSender::ClientSocketSender(String ipAddress, int port)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// @brief Close the connection on destruction
|
||||
ClientSocketSender::~ClientSocketSender()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
/// @brief Closes the connection
|
||||
/// @param
|
||||
void ClientSocketSender::close(void)
|
||||
{
|
||||
if(-1!=mSocket)
|
||||
@@ -40,6 +47,9 @@ void ClientSocketSender::close(void)
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Send the specified file through the connection
|
||||
/// @param pathFileName The file to send
|
||||
/// @return Returns true if successful
|
||||
bool ClientSocketSender::sendFile(String &pathFileName)
|
||||
{
|
||||
Profiler profiler;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _LISTENER_CLIENTSOCKETSENDER_HPP_
|
||||
#define _LISTENER_CLIENTSOCKETSENDER_HPP_
|
||||
#ifndef _SSTP_CLIENTSOCKETSENDER_HPP_
|
||||
#define _SSTP_CLIENTSOCKETSENDER_HPP_
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <sys/types.h>
|
||||
@@ -21,8 +21,7 @@ class ClientSocketSender
|
||||
bool sendFile(String &pathFileName);
|
||||
bool isOkay(void);
|
||||
private:
|
||||
// static constexpr int BUFFER_LENGTH=65536;
|
||||
static constexpr int BUFFER_LENGTH=1048576;
|
||||
static constexpr int BUFFER_LENGTH=1048576; // This buffer length is used for both reading the file and also the packet transmission length
|
||||
void close(void);
|
||||
bool sendPacketIndicator(DWORD bytesToSend);
|
||||
bool sendPacket(Array<char> &buffer,DWORD bytesToSend);
|
||||
|
||||
@@ -26,41 +26,60 @@ void handleClient(Block<String> &commands);
|
||||
/// @return
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int returnCode(0);
|
||||
Profiler profiler;
|
||||
String version = "0.1.0.1"; // major version, minor version, patch, build
|
||||
|
||||
if(argc<2)
|
||||
std::cout << "sstp version " << version.str() << std::endl;
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
if(argc<2)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
std::cout << argv[1] << std::endl;
|
||||
if(arguments[1]=="SERVERMODE")
|
||||
{
|
||||
handleServer(arguments);
|
||||
std::cout << argv[1] << std::endl;
|
||||
if(arguments[1]=="SERVERMODE")
|
||||
{
|
||||
handleServer(arguments);
|
||||
}
|
||||
else if(arguments[1]=="CLIENTMODE")
|
||||
{
|
||||
handleClient(arguments);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Unknown command " << arguments[1] << std::endl;
|
||||
returnCode=-1;
|
||||
}
|
||||
}
|
||||
else if(arguments[1]=="CLIENTMODE")
|
||||
catch(Exception& exception)
|
||||
{
|
||||
handleClient(arguments);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Unknown command " << arguments[1] << std::endl;
|
||||
std::cout << exception.toString() << std::endl;
|
||||
returnCode=-1;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
std::cout << "An unhandled exception was encountered" << std::endl;
|
||||
returnCode=-1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
|
||||
return returnCode;
|
||||
}
|
||||
|
||||
/// @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)
|
||||
{
|
||||
@@ -74,7 +93,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,15 +2,41 @@
|
||||
#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();
|
||||
}
|
||||
|
||||
/// @brief Close the socket and set the mSocket variable to -1
|
||||
/// @param
|
||||
void SocketConnectionReceiver::close(void)
|
||||
{
|
||||
if(-1==mSocket)return;
|
||||
@@ -19,10 +45,12 @@ void SocketConnectionReceiver::close(void)
|
||||
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)
|
||||
{
|
||||
String line;
|
||||
while(true)
|
||||
while(mIsRunnable)
|
||||
{
|
||||
readLine(line);
|
||||
if(0==line.length())
|
||||
@@ -30,7 +58,11 @@ void SocketConnectionReceiver::threadFunction(int data)
|
||||
std::cout << "Received an empty command" << std::endl;
|
||||
break;
|
||||
}
|
||||
if(line=="QUIT" || line=="quit")break;
|
||||
if(line=="QUIT" || line=="quit")
|
||||
{
|
||||
handleQuit();
|
||||
break;
|
||||
}
|
||||
Block<String> commands = line.split('|');
|
||||
if(0==commands.size())continue;
|
||||
commands[0].upper();
|
||||
@@ -49,9 +81,9 @@ void SocketConnectionReceiver::threadFunction(int data)
|
||||
|
||||
/// @brief The client wants to put a file. put {filename} {lengthbytes}
|
||||
/// @param commands [0]=PUT, [1]=filename, [2]=total length
|
||||
/// This will read PCKT {length}.
|
||||
/// The connection will continue reading and writing until it receives PCKT 0
|
||||
/// @return
|
||||
/// 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 packets and saving data to disk until it receives PCKT 0 which signals the end of transmission
|
||||
/// @return true or false
|
||||
bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
||||
{
|
||||
Profiler profiler;
|
||||
@@ -61,6 +93,7 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
||||
String fileName;
|
||||
size_t fileLength;
|
||||
size_t totalBytesRead=0;
|
||||
size_t totalPacketsRead=0;
|
||||
|
||||
fileName = commands[1];
|
||||
fileLength = commands[commands.size()-1].toLong();
|
||||
@@ -93,6 +126,7 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
||||
receiveBuffer.size(bufferLength);
|
||||
size_t bytes_read = read(receiveBuffer);
|
||||
totalBytesRead+=bytes_read;
|
||||
totalPacketsRead++;
|
||||
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";
|
||||
@@ -105,17 +139,19 @@ bool SocketConnectionReceiver::handlePut(Block<String> &commands)
|
||||
{
|
||||
double elapsedTimeSeconds = profiler.elapsed()/1000.00;
|
||||
double bytesPerSecond = totalBytesRead / elapsedTimeSeconds;
|
||||
String strBytesPerSecond = Utility::bytesTransferredToString(bytesPerSecond);
|
||||
std::cout << "Transferred " << Utility::formatNumber(totalBytesRead) << " of " << Utility::formatNumber(fileLength) << " " << percent << " percent " << strBytesPerSecond << std::endl;
|
||||
String strBytesPerSecond = Utility::byteCountToString(bytesPerSecond);
|
||||
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();
|
||||
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;
|
||||
}
|
||||
|
||||
/// @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)
|
||||
{
|
||||
Block<String> subCommands;
|
||||
@@ -128,7 +164,7 @@ size_t SocketConnectionReceiver::expectPacket(void)
|
||||
sb.append("Received an invalid PCKT command");
|
||||
String str=sb.toString();
|
||||
std::cout << str << std::endl;
|
||||
throw new InvalidOperationException(str);
|
||||
throw InvalidOperationException(str);
|
||||
}
|
||||
if(!(subCommands[0]=="PCKT"))
|
||||
{
|
||||
@@ -138,12 +174,15 @@ size_t SocketConnectionReceiver::expectPacket(void)
|
||||
sb.append("Expected PCKT but received ").append(subCommands[0]);
|
||||
String str = sb.toString();
|
||||
std::cout << str << std::endl;
|
||||
throw new InvalidOperationException(str);
|
||||
throw InvalidOperationException(str);
|
||||
}
|
||||
size_t bufferLength = subCommands[1].toULong();
|
||||
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 bytes_read = 0;
|
||||
@@ -169,6 +208,9 @@ size_t SocketConnectionReceiver::readLine(String &line)
|
||||
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 bytes_read = 0;
|
||||
@@ -185,3 +227,10 @@ size_t SocketConnectionReceiver::read(Array<char> &buffer)
|
||||
}
|
||||
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_
|
||||
#define _LISTENER_SOCKETCONNECTIONRECEIVER_HPP_
|
||||
#ifndef _SSTP_SOCKETCONNECTIONRECEIVER_HPP_
|
||||
#define _SSTP_SOCKETCONNECTIONRECEIVER_HPP_
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <sys/types.h>
|
||||
@@ -16,13 +16,18 @@
|
||||
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);
|
||||
size_t readLine(String &line);
|
||||
size_t expectPacket(void);
|
||||
size_t read(Array<char> &buffer);
|
||||
@@ -31,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
|
||||
@@ -37,17 +37,45 @@ SocketServer::SocketServer(int port)
|
||||
mIsOkay=true;
|
||||
}
|
||||
|
||||
/// @brief Close down the listener
|
||||
SocketServer::~SocketServer()
|
||||
{
|
||||
std::cout << "~SocketServer()" << std::endl;
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
while(isOkay())
|
||||
@@ -70,11 +98,19 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Shut down the listener socket
|
||||
/// @param
|
||||
void SocketServer::close(void)
|
||||
{
|
||||
if(-1!=mSocketFileDescriptor)
|
||||
|
||||
@@ -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