Files
CPP/sstp/main.cpp

104 lines
2.8 KiB
C++

#include <stdio.h>
#include <iostream>
#include <sys/types.h>
#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/profiler.hpp>
#include <common/block.hpp>
#include <common/fileio.hpp>
#include <common/utility.hpp>
#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);
/// @brief
/// @param argc
/// @param argv [0] program. [1] SERVERMODE {port} [2] CLIENTMODE {serveripaddress} {serverport} SEND {FileName}
/// @return
int main(int argc, char **argv)
{
Profiler profiler;
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);
}
else if(arguments[1]=="CLIENTMODE")
{
handleClient(arguments);
}
else
{
std::cout << "Unknown command " << arguments[1] << std::endl;
}
return 0;
std::cout << "Done, total took " << Utility::formatNumber(profiler.end()) << "(ms)" << std::endl;
}
/// @brief [0]=program [1]=SERVERMODE [2]={port}
/// @param commands
void 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;
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
/// @param commands
void handleClient(Block<String> &commands)
{
Profiler profiler;
if(commands.size()!=5)
{
std::cout << "Missing required parameters" << std::endl;
return;
}
ClientSocketSender clientSocketSender(commands[2],commands[3].toInt());
bool returnCode = clientSocketSender.sendFile(commands[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;
}