first working prototype
This commit is contained in:
+83
-26
@@ -1,6 +1,7 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <iomanip>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@@ -9,6 +10,7 @@
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -19,7 +21,7 @@
|
||||
#endif
|
||||
|
||||
#define PORT 8080
|
||||
#define BUFFER_SIZE 1024
|
||||
#define BUFFER_SIZE 65536
|
||||
#define MULTICAST_PORT 8081
|
||||
#define MULTICAST_GROUP "239.0.0.1" // Multicast group IP
|
||||
|
||||
@@ -27,7 +29,8 @@ namespace fs = std::filesystem;
|
||||
|
||||
#ifdef __SWITCH__
|
||||
std::string replaceUsername(const std::string &path) {
|
||||
std::string replacedString = Account::username(g_currentUId);
|
||||
std::string replacedString = StringUtils::removeNotAscii(
|
||||
StringUtils::removeAccents(Account::username(g_currentUId)));
|
||||
// Найдём позицию последнего символа '/'
|
||||
size_t lastSlashPos = path.rfind('/');
|
||||
|
||||
@@ -51,42 +54,91 @@ std::string replaceUsername(const std::string &path) {
|
||||
}
|
||||
#endif
|
||||
|
||||
void sendAck(int sock) {
|
||||
const char *ack = "ACK";
|
||||
std::cout << "Sending ACK " << std::endl;
|
||||
send(sock, ack, strlen(ack), 0);
|
||||
// Читает ровно len байт из сокета, повторяя read при частичном получении.
|
||||
static bool recv_all(int sock, void *buf, size_t len) {
|
||||
size_t received = 0;
|
||||
while (received < len) {
|
||||
ssize_t n = read(sock, static_cast<char *>(buf) + received, len - received);
|
||||
if (n <= 0) return false;
|
||||
received += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Создаёт все компоненты пути через POSIX mkdir.
|
||||
// std::filesystem::create_directories не работает с devkitPro-путями sdmc:/.
|
||||
static void mkdirs(const std::string &path) {
|
||||
for (size_t i = 1; i < path.size(); i++) {
|
||||
if (path[i] == '/') {
|
||||
std::string component = path.substr(0, i);
|
||||
int rc = mkdir(component.c_str(), 0777);
|
||||
std::cout << "mkdirs: mkdir [" << component << "] rc=" << rc << " errno=" << errno << std::endl;
|
||||
}
|
||||
}
|
||||
int rc = mkdir(path.c_str(), 0777);
|
||||
std::cout << "mkdirs: mkdir [" << path << "] rc=" << rc << " errno=" << errno << std::endl;
|
||||
}
|
||||
|
||||
// Функция для получения файла
|
||||
void receive_file(int sock, const std::string &relative_path,
|
||||
size_t file_size) {
|
||||
fs::path filepath(relative_path);
|
||||
// Create parent directories if they do not exist
|
||||
|
||||
std::cout << "relative_path is: " << relative_path << std::endl;
|
||||
|
||||
fs::create_directories(filepath.parent_path());
|
||||
// Печатаем путь побайтово — ловим невидимые символы
|
||||
std::cout << "receive_file len=" << relative_path.size() << " path=[";
|
||||
for (unsigned char c : relative_path) {
|
||||
if (c >= 0x20 && c <= 0x7e) std::cout << c;
|
||||
else std::cout << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (int)c << std::dec;
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
|
||||
std::ofstream outfile(filepath, std::ios::binary);
|
||||
char buffer[BUFFER_SIZE] = {0};
|
||||
size_t last_slash = relative_path.rfind('/');
|
||||
std::string dir = (last_slash != std::string::npos)
|
||||
? relative_path.substr(0, last_slash)
|
||||
: "";
|
||||
std::cout << "receive_file dir=[" << dir << "]" << std::endl;
|
||||
if (!dir.empty()) mkdirs(dir);
|
||||
|
||||
// Проверяем stat на папке перед fopen
|
||||
struct stat st;
|
||||
int statrc = stat(dir.c_str(), &st);
|
||||
std::cout << "stat(dir) rc=" << statrc << " is_dir="
|
||||
<< (statrc == 0 && S_ISDIR(st.st_mode)) << std::endl;
|
||||
|
||||
FILE *outfile = fopen(relative_path.c_str(), "wb");
|
||||
if (!outfile) {
|
||||
int saved_errno = errno;
|
||||
std::cerr << "Failed to open for writing: " << relative_path
|
||||
<< " dir=[" << dir << "] fopen_errno=" << saved_errno << std::endl;
|
||||
// Дренируем байты, чтобы отправитель не завис
|
||||
char* drain_buf = new char[BUFFER_SIZE];
|
||||
size_t remaining = file_size;
|
||||
while (remaining > 0) {
|
||||
ssize_t n = read(sock, drain_buf, remaining < (size_t)BUFFER_SIZE ? remaining : (size_t)BUFFER_SIZE);
|
||||
if (n <= 0) break;
|
||||
remaining -= n;
|
||||
}
|
||||
delete[] drain_buf;
|
||||
return;
|
||||
}
|
||||
|
||||
char* buffer = new char[BUFFER_SIZE]();
|
||||
size_t total_bytes_received = 0;
|
||||
while (total_bytes_received < file_size) {
|
||||
ssize_t bytes_received = read(sock, buffer, BUFFER_SIZE);
|
||||
size_t to_read = std::min((size_t)BUFFER_SIZE, file_size - total_bytes_received);
|
||||
ssize_t bytes_received = read(sock, buffer, to_read);
|
||||
std::cout << "Bytes received: " << bytes_received << std::endl;
|
||||
if (bytes_received <= 0) {
|
||||
std::cerr << "Error reading file data from socket." << std::endl;
|
||||
break;
|
||||
}
|
||||
outfile.write(buffer, bytes_received);
|
||||
fwrite(buffer, 1, bytes_received, outfile);
|
||||
total_bytes_received += bytes_received;
|
||||
|
||||
// Send acknowledgment for each chunk received
|
||||
sendAck(sock);
|
||||
}
|
||||
|
||||
std::cout << "File received successfully: " << relative_path << std::endl;
|
||||
outfile.close();
|
||||
delete[] buffer;
|
||||
fclose(outfile);
|
||||
}
|
||||
|
||||
void *handle_client(void *socket_desc) {
|
||||
@@ -108,14 +160,16 @@ void *handle_client(void *socket_desc) {
|
||||
pthread_exit(nullptr);
|
||||
}
|
||||
|
||||
// Receive filename or directory name
|
||||
char *filename = new char[filename_len + 1];
|
||||
read(client_socket, filename, filename_len);
|
||||
|
||||
// Receive filename
|
||||
char *filename = new char[filename_len + 1]();
|
||||
if (!recv_all(client_socket, filename, filename_len)) {
|
||||
std::cerr << "Short read on filename, aborting." << std::endl;
|
||||
delete[] filename;
|
||||
break;
|
||||
}
|
||||
filename[filename_len] = '\0';
|
||||
std::string filename_str(filename);
|
||||
std::cout << "Receive filename: " << filename_str << std::endl;
|
||||
delete[] filename; // Clean up filename buffer
|
||||
delete[] filename;
|
||||
|
||||
std::cout << "Received filename_str is " << filename_str << std::endl;
|
||||
|
||||
@@ -126,7 +180,10 @@ void *handle_client(void *socket_desc) {
|
||||
#endif
|
||||
|
||||
size_t file_size;
|
||||
read(client_socket, &file_size, sizeof(file_size));
|
||||
if (!recv_all(client_socket, &file_size, sizeof(file_size))) {
|
||||
std::cerr << "Short read on file_size, aborting." << std::endl;
|
||||
break;
|
||||
}
|
||||
std::cout << "file size is: " << file_size << std::endl;
|
||||
receive_file(client_socket, filename_str, file_size);
|
||||
}
|
||||
@@ -138,7 +195,7 @@ void *handle_client(void *socket_desc) {
|
||||
void *broadcast_listener(void *) {
|
||||
int sockfd;
|
||||
struct sockaddr_in servaddr;
|
||||
char buffer[BUFFER_SIZE];
|
||||
char buffer[BUFFER_SIZE + 1];
|
||||
struct ip_mreq group;
|
||||
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
|
||||
Reference in New Issue
Block a user