#include <iostream> #include <string.h> #include <signal.h> using namespace std; #include "../../../src/Tools/SendFile.h" #include "../../../src/Tools/RecvFile.h" #include "../../../src/socket/FileService.h" #include "../../../src/socket/TCPSocketEx.h" #include "../../../src/socket/Private/MemoryPool.h" #include "../../../src/Tools/Tools.h" #define UPLOAD 0 #define DOWNLOAD 1 typedef struct CONFIG { char service[64]; char ip[32]; int port; char dir[255]; char file[255]; int maxSend; int UpLoadOrDownload; }Config,*LPConfig; static Config config; Tools tools; auto TcpSocket = TCPSocketEx::create(); auto *service = new FileService(); bool runing(); void Server(); void Client(); void Signal(int v) { printf("signal %d ",v); SendFile::getInstance()->closeSend(); RecvFile::getInstance()->closeRecv(); tools.freeAll(); exit(0); } void message() { printf("-i/--init server or client "); printf("-p/--port server port "); printf("-t/--target target IP "); printf("-d/--dir current director, send director or is recv director "); printf("-f/--file file name or file full path "); printf("-s/--send set max send byte "); printf("-U/--upload client upload "); printf("-D/--download client download "); printf("-h/--help "); printf(" *********server is recv,client is send********* "); printf(" *********select one service********* "); printf(" *********file size Better not exceed .long. Value range********* "); printf(" *********max send byte 0 or -1 default is 4096 + 1********* "); } int main(int argc, char **argv) { signal(SIGINT, Signal); ZeroMemory(&config, sizeof(Config)); printf(" ************************************* ************************************* ************************************* ************************************* ************************************* **************YZFHKMS-W************** ************************************* ************************************* ************************************* ************************************* ************************************* ************************************* ************************************* "); if (argc > 1) { for (int i = 1; i < argc - 1; i++) { if (argv[i] == string("-h") || argv[i] == string("--help")) { message(); break; } else if (argv[i] == string("-i") || argv[i] == string("--init")) { memcpy(config.service, argv[i + 1], strlen(argv[i + 1])); } else if (argv[i] == string("-p") || argv[i] == string("--port")) { config.port = atoi(argv[i + 1]); } else if (argv[i] == string("-t") || argv[i] == string("--target")) { memcpy(config.ip, argv[i + 1], strlen(argv[i + 1])); } else if (argv[i] == string("-d") || argv[i] == string("--dir")) { memcpy(config.dir, argv[i + 1], strlen(argv[i + 1])); } else if (argv[i] == string("-f") || argv[i] == string("--file")) { memcpy(config.file, argv[i + 1], strlen(argv[i + 1])); } else if (argv[i] == string("-s") || argv[i] == string("--send")) { config.maxSend = atoi(argv[i + 1]); } else if (argv[i] == string("-U") || argv[i] == string("--upload")) { config.UpLoadOrDownload = UPLOAD; } else if (argv[i] == string("-D") || argv[i] == string("--download")) { config.UpLoadOrDownload = DOWNLOAD; } } } else { message(); return 0; } if (runing()) { message(); return 0; } printf("runing service ............. "); int s, r; while (true) { TcpSocket->runing(1); if (TcpSocket->getService(config.service) != nullptr) { if (TcpSocket->getService(config.service)->getServer() == SOCKETTYPES::SERVER) { ((FileService*)(TcpSocket->getService(config.service)))->updateFileStream(); } if (TcpSocket->getService(config.service)->getClient() == SOCKETTYPES::CLIENT) { r = ((FileService*)(TcpSocket->getService(config.service)))->getRecvProgresses(config.file); s = ((FileService*)(TcpSocket->getService(config.service)))->getSendProgresses(config.file); if (r != 0 || s != 0) { printf("Upload:%d, Download:%d ", s, r); if (r == 100 || s == 100) { printf("Upload:%d, Download:%d ", s, r); ((FileService*)(TcpSocket->getService(config.service)))->updateFileStream(); } } } } } return 0; } bool runing() { printf(" service %s ",config.service); printf(" port %d ", config.port); printf(" target %s ", config.ip); printf(" dir %s ", config.dir); printf(" file %s ", config.file); if (config.service == string("server")) { Server(); } else if(config.service == string("client")) { Client(); } else { printf("please select one service! "); return true; } return false; } void Server() { TcpSocket->initServer(config.port, config.maxSend); TcpSocket->addValidityNum(FileServiceValidity); TcpSocket->pushbackService(service); service->m_TCPSocketEx = TcpSocket; service->setServer(SOCKETTYPES::SERVER); service->setName("server"); service->setSaveDir(config.dir); service->addReadDir(config.dir); service->setServiceType(FileServiceType); } void Client() { TcpSocket->initClient(config.ip, config.port, config.maxSend); TcpSocket->addValidityNum(FileServiceValidity); TcpSocket->pushbackService(service); service->m_TCPSocketEx = TcpSocket; service->setName("client"); service->setClient(SOCKETTYPES::CLIENT); service->setSaveDir(config.dir); service->addReadDir(config.dir); service->setServiceType(FileServiceType); if (config.UpLoadOrDownload == UPLOAD) { service->pushSendFile(config.file); } else if (config.UpLoadOrDownload == DOWNLOAD) { auto s = TcpSocket; tools.clear(); tools.add_int(FILESERVICE_RECVFILE); tools.add_strings(config.file); auto r = tools.size(); auto d = tools.data_s_m(); auto ar = s->setValidity(r, FileServiceValidity, FileServiceKey, FileServiceType); auto ss = s->setValidityPackage(d, r, *ar, false); s->pushbackPackage(ss); } }