From 1da824d8cb5ec4ca807709ceefc8bed739e5b765 Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 14 Aug 2025 20:23:45 -0400 Subject: [PATCH] Add comments --- sstp/clientsocketsender.cpp | 10 +++++++ sstp/clientsocketsender.hpp | 7 +++-- sstp/socketconnectionreceiver.cpp | 44 ++++++++++++++++++++++++++----- sstp/socketconnectionreceiver.hpp | 5 ++-- sstp/socketserver.cpp | 9 ++++++- 5 files changed, 61 insertions(+), 14 deletions(-) diff --git a/sstp/clientsocketsender.cpp b/sstp/clientsocketsender.cpp index b34bbce..19a2423 100644 --- a/sstp/clientsocketsender.cpp +++ b/sstp/clientsocketsender.cpp @@ -1,5 +1,8 @@ #include +/// @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; diff --git a/sstp/clientsocketsender.hpp b/sstp/clientsocketsender.hpp index 535b883..a0dd3dd 100644 --- a/sstp/clientsocketsender.hpp +++ b/sstp/clientsocketsender.hpp @@ -1,5 +1,5 @@ -#ifndef _LISTENER_CLIENTSOCKETSENDER_HPP_ -#define _LISTENER_CLIENTSOCKETSENDER_HPP_ +#ifndef _SSTP_CLIENTSOCKETSENDER_HPP_ +#define _SSTP_CLIENTSOCKETSENDER_HPP_ #include #include #include @@ -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 &buffer,DWORD bytesToSend); diff --git a/sstp/socketconnectionreceiver.cpp b/sstp/socketconnectionreceiver.cpp index 0e72d4a..fb83155 100644 --- a/sstp/socketconnectionreceiver.cpp +++ b/sstp/socketconnectionreceiver.cpp @@ -2,15 +2,22 @@ #include #include +/// @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) { } +/// @brief The destructor will close the socket SocketConnectionReceiver::~SocketConnectionReceiver() { + close(); } +/// @brief Close the socket and set the mSocket variable to -1 +/// @param void SocketConnectionReceiver::close(void) { if(-1==mSocket)return; @@ -19,6 +26,8 @@ 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; @@ -30,7 +39,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 commands = line.split('|'); if(0==commands.size())continue; commands[0].upper(); @@ -49,9 +62,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 &commands) { Profiler profiler; @@ -61,6 +74,7 @@ bool SocketConnectionReceiver::handlePut(Block &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 +107,7 @@ bool SocketConnectionReceiver::handlePut(Block &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"; @@ -108,14 +123,16 @@ bool SocketConnectionReceiver::handlePut(Block &commands) 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::byteCountToString(totalBytesRead,false) << " 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 subCommands; @@ -144,6 +161,9 @@ size_t SocketConnectionReceiver::expectPacket(void) 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 +189,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 &buffer) { size_t bytes_read = 0; @@ -185,3 +208,10 @@ size_t SocketConnectionReceiver::read(Array &buffer) } return total_bytes_read; } + +/// @brief Handle a QUIT message... which just displays +/// @param +void SocketConnectionReceiver::handleQuit(void) +{ + std::cout << "Received QUIT" << std::endl; +} diff --git a/sstp/socketconnectionreceiver.hpp b/sstp/socketconnectionreceiver.hpp index 116d9d0..0a75b11 100644 --- a/sstp/socketconnectionreceiver.hpp +++ b/sstp/socketconnectionreceiver.hpp @@ -1,5 +1,5 @@ -#ifndef _LISTENER_SOCKETCONNECTIONRECEIVER_HPP_ -#define _LISTENER_SOCKETCONNECTIONRECEIVER_HPP_ +#ifndef _SSTP_SOCKETCONNECTIONRECEIVER_HPP_ +#define _SSTP_SOCKETCONNECTIONRECEIVER_HPP_ #include #include #include @@ -23,6 +23,7 @@ class SocketConnectionReceiver static constexpr size_t BUFFER_LENGTH=65536; // this is the buffer length for the socket void threadFunction(int data); bool handlePut(Block &commands); + void handleQuit(void); size_t readLine(String &line); size_t expectPacket(void); size_t read(Array &buffer); diff --git a/sstp/socketserver.cpp b/sstp/socketserver.cpp index b29c919..fd8643b 100644 --- a/sstp/socketserver.cpp +++ b/sstp/socketserver.cpp @@ -37,17 +37,22 @@ SocketServer::SocketServer(int port) mIsOkay=true; } +/// @brief Close down the listener SocketServer::~SocketServer() { - std::cout << "~SocketServer()" << std::endl; close(); } +/// @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()) @@ -75,6 +80,8 @@ void SocketServer::listen(void) } } +/// @brief Shut down the listener socket +/// @param void SocketServer::close(void) { if(-1!=mSocketFileDescriptor)