C/C++ file
#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\n",v); SendFile::getInstance()->closeSend(); RecvFile::getInstance()->closeRecv(); tools.freeAll(); exit(0); } void message() { printf("-i/--init server or client\n"); printf("-p/--port server port\n"); printf("-t/--target target IP\n"); printf("-d/--dir current director, send director or is recv director\n"); printf("-f/--file file name or file full path\n"); printf("-s/--send set max send byte\n"); printf("-U/--upload client upload\n"); printf("-D/--download client download\n"); printf("-h/--help \n"); printf("\n *********server is recv,client is send*********\n\n"); printf("\n *********select one service*********\n\n"); printf("\n *********file size Better not exceed .long. Value range*********\n\n"); printf("\n *********max send byte 0 or -1 default is 4096 + 1*********\n\n"); } int main(int argc, char **argv) { signal(SIGINT, Signal); ZeroMemory(&config, sizeof(Config)); printf("\n\ *************************************\n\ *************************************\n\ *************************************\n\ *************************************\n\ *************************************\n\ **************YZFHKMS-W**************\n\ *************************************\n\ *************************************\n\ *************************************\n\ *************************************\n\ *************************************\n\ *************************************\n\ *************************************\n\ \n"); 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 .............\n"); 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\n", s, r); if (r == 100 || s == 100) { printf("Upload:%d, Download:%d\n", s, r); ((FileService*)(TcpSocket->getService(config.service)))->updateFileStream(); } } } } } return 0; } bool runing() { printf(" service %s\n",config.service); printf(" port %d\n", config.port); printf(" target %s\n", config.ip); printf(" dir %s\n", config.dir); printf(" file %s\n\n", config.file); if (config.service == string("server")) { Server(); } else if(config.service == string("client")) { Client(); } else { printf("please select one service!\n\n"); 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); } }