arch codereview
This commit is contained in:
+136
-196
@@ -1,247 +1,187 @@
|
||||
#include <string>
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
#include <ostream>
|
||||
#include <pthread.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#ifdef __SWITCH__
|
||||
#include <client.hpp>
|
||||
#include <switch.h>
|
||||
#endif
|
||||
#define PORT 8080
|
||||
#define BUFFER_SIZE 65536
|
||||
#define MULTICAST_PORT 8081
|
||||
#define MULTICAST_GROUP "239.0.0.1" // Multicast group IP
|
||||
|
||||
#include <protocol.hpp>
|
||||
#include <TransferState.hpp>
|
||||
#include <net/Socket.hpp>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
using path = fs::path;
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
inline static const std::string INFO = "[INFO]";
|
||||
inline static const std::string DEBUG = "[DEBUG]";
|
||||
inline static const std::string ERROR = "[ERROR]";
|
||||
inline static const std::string WARN = "[WARN]";
|
||||
};
|
||||
static TransferState g_client_state;
|
||||
|
||||
bool isClientTransferDone() { return g_client_state.done.load(); }
|
||||
void cancelClientTransfer() { g_client_state.cancelled.store(true); }
|
||||
double getClientProgress() { return g_client_state.progress(); }
|
||||
std::string getClientStatusText() { return g_client_state.getStatus(); }
|
||||
|
||||
struct ThreadArgs {
|
||||
int sock;
|
||||
fs::path directory;
|
||||
int sock;
|
||||
fs::path directory;
|
||||
};
|
||||
|
||||
static bool send_all(int sock, const void* buf, size_t len) {
|
||||
size_t sent = 0;
|
||||
while (sent < len) {
|
||||
ssize_t n = send(sock, static_cast<const char*>(buf) + sent, len - sent, 0);
|
||||
if (n <= 0) return false;
|
||||
sent += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Отправка строки с учетом её длины
|
||||
void send_string(int sock, const std::string &str) {
|
||||
size_t length = str.size();
|
||||
send(sock, &length, sizeof(length), 0);
|
||||
send(sock, str.c_str(), length, 0);
|
||||
}
|
||||
|
||||
void sendFile(int sock, const fs::path &base_path, const fs::path &filepath) {
|
||||
std::ifstream infile(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!infile.is_open()) {
|
||||
std::cerr << "File not found: " << filepath << std::endl;
|
||||
return;
|
||||
}
|
||||
// std::string relative_path = fs::relative(filepath, base_path).string();
|
||||
uint32_t filename_len = filepath.string().size();
|
||||
|
||||
std::cout << "send filepath length: " << filename_len << std::endl;
|
||||
|
||||
if (send(sock, &filename_len, sizeof(filename_len), 0) == -1) {
|
||||
std::cerr << "Failed to send filename length" << std::endl;
|
||||
}
|
||||
std::cout << "send filepath: " << filepath.string() << std::endl;
|
||||
|
||||
// Send the filename
|
||||
if (send(sock, filepath.c_str(), filename_len, 0) ==
|
||||
-1) { // Send the filename length
|
||||
std::cerr << "Failed to send filename" << std::endl;
|
||||
}
|
||||
|
||||
// Get the size of the file
|
||||
std::streamsize file_size = infile.tellg();
|
||||
infile.seekg(0, std::ios::beg);
|
||||
char* buffer = new char[BUFFER_SIZE];
|
||||
|
||||
std::cout << "send filesize: " << file_size << std::endl;
|
||||
|
||||
// Send the file size
|
||||
if (send(sock, &file_size, sizeof(file_size), 0) == -1) {
|
||||
std::cerr << "Failed to send file size" << std::endl;
|
||||
}
|
||||
|
||||
while (file_size > 0) {
|
||||
infile.read(buffer, BUFFER_SIZE);
|
||||
std::streamsize count = infile.gcount();
|
||||
if (!send_all(sock, buffer, count)) {
|
||||
std::cerr << "Failed to send file data" << std::endl;
|
||||
delete[] buffer;
|
||||
pthread_exit(nullptr);
|
||||
size_t sent = 0;
|
||||
while (sent < len) {
|
||||
ssize_t n = send(sock, static_cast<const char*>(buf) + sent, len - sent, 0);
|
||||
if (n <= 0) return false;
|
||||
sent += n;
|
||||
}
|
||||
file_size -= count;
|
||||
}
|
||||
|
||||
std::cout << "File sent successfully: " << filepath << std::endl;
|
||||
|
||||
delete[] buffer;
|
||||
infile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
void *send_files_thread(void *args) {
|
||||
ThreadArgs *thread_args = static_cast<ThreadArgs *>(args);
|
||||
int sock = thread_args->sock;
|
||||
fs::path cwd = thread_args->directory;
|
||||
delete thread_args;
|
||||
std::cout << "cwd is: " << cwd << std::endl;
|
||||
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
for (const auto &entry : fs::recursive_directory_iterator(cwd)) {
|
||||
path path = entry.path();
|
||||
std::cout << "path is " << path << std::endl;
|
||||
if (fs::is_regular_file(path)) {
|
||||
std::cout << "regular file | path is: " << path << std::endl;
|
||||
sendFile(sock, path.parent_path(), path);
|
||||
static bool sendFile(int sock, const fs::path& filepath) {
|
||||
std::ifstream infile(filepath, std::ios::binary | std::ios::ate);
|
||||
if (!infile.is_open()) {
|
||||
std::cerr << "File not found: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
close(sock);
|
||||
pthread_exit(nullptr);
|
||||
uint32_t filename_len = (uint32_t)filepath.string().size();
|
||||
uint64_t file_size = (uint64_t)infile.tellg();
|
||||
infile.seekg(0, std::ios::beg);
|
||||
|
||||
std::cout << "Sending: " << filepath << " (" << file_size << " bytes)" << std::endl;
|
||||
|
||||
if (!send_all(sock, &filename_len, sizeof(filename_len))) return false;
|
||||
if (!send_all(sock, filepath.c_str(), filename_len)) return false;
|
||||
if (!send_all(sock, &file_size, sizeof(file_size))) return false;
|
||||
|
||||
std::vector<char> buffer(proto::BUF_SIZE);
|
||||
uint64_t remaining = file_size;
|
||||
while (remaining > 0) {
|
||||
size_t to_read = (size_t)std::min(remaining, (uint64_t)proto::BUF_SIZE);
|
||||
infile.read(buffer.data(), (std::streamsize)to_read);
|
||||
std::streamsize count = infile.gcount();
|
||||
if (count <= 0) break;
|
||||
if (!send_all(sock, buffer.data(), (size_t)count)) {
|
||||
std::cerr << "Failed to send data for: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
g_client_state.bytes_done.fetch_add((uint64_t)count);
|
||||
remaining -= (uint64_t)count;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int find_server(char *server_ip) {
|
||||
std::cout << Logger::INFO << "Init find_server" << std::endl;
|
||||
int sockfd;
|
||||
struct sockaddr_in multicast_addr;
|
||||
static void* send_files_thread(void* arg) {
|
||||
ThreadArgs* targs = static_cast<ThreadArgs*>(arg);
|
||||
int sock = targs->sock;
|
||||
fs::path cwd = targs->directory;
|
||||
delete targs;
|
||||
|
||||
std::cout << Logger::INFO << "Create socket" << std::endl;
|
||||
for (const auto& entry : fs::recursive_directory_iterator(cwd)) {
|
||||
if (g_client_state.cancelled.load()) break;
|
||||
const path& p = entry.path();
|
||||
if (fs::is_regular_file(p)) {
|
||||
g_client_state.setStatus(p.filename().string());
|
||||
if (!sendFile(sock, p)) break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
std::cout << Logger::ERROR << "Socket creation error" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
// EOF sentinel — server reads this and exits its receive loop cleanly
|
||||
uint32_t sentinel = proto::EOF_SENTINEL;
|
||||
send_all(sock, &sentinel, sizeof(sentinel));
|
||||
|
||||
memset(&multicast_addr, 0, sizeof(multicast_addr));
|
||||
g_client_state.setStatus("");
|
||||
close(sock);
|
||||
g_client_state.done.store(true);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
multicast_addr.sin_family = AF_INET;
|
||||
multicast_addr.sin_port = htons(MULTICAST_PORT);
|
||||
multicast_addr.sin_addr.s_addr = inet_addr(MULTICAST_GROUP);
|
||||
static int find_server(char* server_ip) {
|
||||
Socket udp(socket(AF_INET, SOCK_DGRAM, 0));
|
||||
if (!udp.valid()) {
|
||||
std::cerr << "find_server: socket failed" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << Logger::INFO << "Send DISCOVER_SERVER" << std::endl;
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(proto::MULTICAST_PORT);
|
||||
addr.sin_addr.s_addr = inet_addr(proto::MULTICAST_GROUP);
|
||||
|
||||
const char *multicast_message = "DISCOVER_SERVER";
|
||||
if (sendto(sockfd, multicast_message, strlen(multicast_message), 0,
|
||||
(struct sockaddr *)&multicast_addr, sizeof(multicast_addr)) < 0) {
|
||||
std::cout << Logger::ERROR << "sendto failed" << std::endl;
|
||||
close(sockfd);
|
||||
return -1;
|
||||
} else {
|
||||
std::cout << Logger::INFO << "send multicast message success" << std::endl;
|
||||
}
|
||||
const char* msg = "DISCOVER_SERVER";
|
||||
if (sendto(udp, msg, strlen(msg), 0, (sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||
std::cerr << "find_server: sendto failed" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in cliaddr;
|
||||
socklen_t len = sizeof(cliaddr);
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
ssize_t n = recvfrom(sockfd, buffer, BUFFER_SIZE, 0,
|
||||
(struct sockaddr *)&cliaddr, &len);
|
||||
std::cout << Logger::INFO << "recvfrom n: %i" << n << std::endl;
|
||||
if (n < 0) {
|
||||
std::cout << Logger::ERROR << "recvfrom failed" << std::endl;
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
std::cout << Logger::INFO << "buffer: " << buffer << std::endl;
|
||||
buffer[n] = '\0';
|
||||
if (strcmp(buffer, "SERVER_HERE") == 0) {
|
||||
std::cout << Logger::INFO << "Server found" << std::endl;
|
||||
inet_ntop(AF_INET, &cliaddr.sin_addr, server_ip, INET_ADDRSTRLEN);
|
||||
} else {
|
||||
std::cout << Logger::ERROR << "Unable to find server, close socket"
|
||||
<< std::endl;
|
||||
;
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
return 0;
|
||||
sockaddr_in from{};
|
||||
socklen_t fromlen = sizeof(from);
|
||||
char buf[256];
|
||||
ssize_t n = recvfrom(udp, buf, sizeof(buf) - 1, 0, (sockaddr*)&from, &fromlen);
|
||||
if (n < 0) {
|
||||
std::cerr << "find_server: recvfrom failed" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
buf[n] = '\0';
|
||||
if (strcmp(buf, "SERVER_HERE") != 0) {
|
||||
std::cerr << "find_server: unexpected response: " << buf << std::endl;
|
||||
return -1;
|
||||
}
|
||||
inet_ntop(AF_INET, &from.sin_addr, server_ip, INET_ADDRSTRLEN);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int transfer_files(fs::path directory) {
|
||||
std::cout << Logger::INFO << "Init transfer_files" << std::endl;
|
||||
char server_ip[INET_ADDRSTRLEN];
|
||||
if (find_server(server_ip) != 0) {
|
||||
std::cout << Logger::ERROR << "Failed to find server" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
char server_ip[INET_ADDRSTRLEN];
|
||||
if (find_server(server_ip) != 0) return -1;
|
||||
|
||||
int sock = 0;
|
||||
struct sockaddr_in serv_addr;
|
||||
Socket tcp(socket(AF_INET, SOCK_STREAM, 0));
|
||||
if (!tcp.valid()) return -1;
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
std::cout << Logger::ERROR << "Socket creation error" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
sockaddr_in serv{};
|
||||
serv.sin_family = AF_INET;
|
||||
serv.sin_port = htons(proto::TCP_PORT);
|
||||
if (inet_pton(AF_INET, server_ip, &serv.sin_addr) <= 0) return -1;
|
||||
if (connect(tcp, (sockaddr*)&serv, sizeof(serv)) < 0) return -1;
|
||||
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(PORT);
|
||||
uint64_t total = 0;
|
||||
for (const auto& entry : fs::recursive_directory_iterator(directory)) {
|
||||
if (fs::is_regular_file(entry.path()))
|
||||
total += fs::file_size(entry.path());
|
||||
}
|
||||
|
||||
if (inet_pton(AF_INET, server_ip, &serv_addr.sin_addr) <= 0) {
|
||||
std::cout << Logger::ERROR << "Invalid address / Address not supported"
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
g_client_state.reset();
|
||||
g_client_state.bytes_total.store(total);
|
||||
g_client_state.setStatus("Connecting...");
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
std::cout << Logger::ERROR << "Connection failed" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
ThreadArgs* args = new ThreadArgs{tcp.fd, directory};
|
||||
tcp.release(); // ownership transferred to thread
|
||||
|
||||
pthread_t file_thread;
|
||||
ThreadArgs *thread_args = new ThreadArgs{sock, directory};
|
||||
if (pthread_create(&file_thread, nullptr, send_files_thread,
|
||||
(void *)thread_args) < 0) {
|
||||
std::cout << Logger::ERROR << "Thread creation failed" << std::endl;
|
||||
close(sock);
|
||||
delete thread_args;
|
||||
return -1;
|
||||
} else {
|
||||
std::cout << Logger::INFO << "Wait for file_thread" << std::endl;
|
||||
pthread_join(file_thread, nullptr);
|
||||
}
|
||||
return 0;
|
||||
pthread_t thread;
|
||||
if (pthread_create(&thread, nullptr, send_files_thread, args) != 0) {
|
||||
delete args;
|
||||
return -1;
|
||||
}
|
||||
pthread_detach(thread);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef __SWITCH__ // for desktop
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 1) {
|
||||
std::cerr << "Usage: " << argv[0] << " <path> " << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fs::path directory = fs::path(argv[1]);
|
||||
std::cout << "directory is " << directory << std::endl;
|
||||
transfer_files(directory);
|
||||
|
||||
return 0;
|
||||
#ifndef __SWITCH__
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " <path>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (transfer_files(fs::path(argv[1])) != 0) return 1;
|
||||
while (!isClientTransferDone()) usleep(16000);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user