From 97c460fb5a2fe1d31cc1a63f3f8442fbf6e2ac69 Mon Sep 17 00:00:00 2001 From: Nikolai Fedorov Date: Fri, 22 Nov 2024 00:09:18 +0300 Subject: [PATCH] transfer to switch --- include/server.hpp | 1 + source/TitlesLayout.cpp | 19 ++++++++-- server.cpp => source/server.cpp | 63 ++++++++++++++++++++++++++------- 3 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 include/server.hpp rename server.cpp => source/server.cpp (79%) diff --git a/include/server.hpp b/include/server.hpp new file mode 100644 index 0000000..24e3e88 --- /dev/null +++ b/include/server.hpp @@ -0,0 +1 @@ +int startSendingThread(); \ No newline at end of file diff --git a/source/TitlesLayout.cpp b/source/TitlesLayout.cpp index d5ed82e..613d28b 100644 --- a/source/TitlesLayout.cpp +++ b/source/TitlesLayout.cpp @@ -3,6 +3,7 @@ #include #include #include +#include static std::vector accSids, devSids, bcatSids, cacheSids; @@ -34,11 +35,11 @@ namespace ui { Title title; getTitle(title, g_currentUId, index); printf("userid is 0x%lX%lX\n", title.userId().uid[1], title.userId().uid[0]); - printf("current game index is %i\n", index); - int opt = mainApp->CreateShowDialog(title.name().c_str(), "What do you want?", { "Transfer", "Receive" }, true); + // printf("current game index is %i\n", index); + int opt = mainApp->CreateShowDialog(title.name().c_str(), "What do you want?", { "Transfer", "Receive" }, false); + printf("opt is %i\n", opt); switch (opt) { case 0: { - printf("path is %s\n", title.fullPath(0).c_str()); // Transfer selected auto result = io::backup(index, g_currentUId); if (std::get<0>(result)) { @@ -51,6 +52,18 @@ namespace ui { } case 1: { // Receive selected + printf("startSendingThread\n"); + int result = startSendingThread(); + printf("result is %i\n", result); + if (result == 0) { + auto restoreResult = io::restore(index, g_currentUId, 0, title.name()); + if (std::get<0>(restoreResult)) { + printf("%s\n", std::get<2>(restoreResult).c_str()); + } else { + printf("Failed to restore with error %s\n", std::get<2>(restoreResult).c_str()); + } + } + break; } } diff --git a/server.cpp b/source/server.cpp similarity index 79% rename from server.cpp rename to source/server.cpp index 52ff97b..5873062 100644 --- a/server.cpp +++ b/source/server.cpp @@ -13,7 +13,9 @@ #include #ifdef __SWITCH__ +#include #include +#include #endif #define PORT 8080 @@ -23,6 +25,32 @@ namespace fs = std::filesystem; +#ifdef __SWITCH__ +std::string replaceUsername(const std::string &path) { + std::string replacedString = Account::username(g_currentUId); + // Найдём позицию последнего символа '/' + size_t lastSlashPos = path.rfind('/'); + + // Если нет '/', возвращаем исходный путь + if (lastSlashPos == std::string::npos) { + return path; + } + + // Найдём позицию предыдущего символа '/' (начало последней папки) + size_t prevSlashPos = path.rfind('/', lastSlashPos - 1); + + // Если предыдущий '/' не найден, значит путь состоит из одной папки и файла + // Заменим последнюю папку и вернём полный путь + if (prevSlashPos == std::string::npos) { + return replacedString + path.substr(lastSlashPos); + } + + // Собираем путь, заменяя последнюю папку на "name" + return path.substr(0, prevSlashPos + 1) + replacedString + + path.substr(lastSlashPos); +} +#endif + void sendAck(int sock) { const char *ack = "ACK"; std::cout << "Sending ACK " << std::endl; @@ -90,8 +118,12 @@ void *handle_client(void *socket_desc) { delete[] filename; // Clean up filename buffer std::cout << "Received filename_str is " << filename_str << std::endl; - auto is_directory = filename_str.back() == '/'; - std::cout << "Is directory: " << is_directory << std::endl; + + #ifdef __SWITCH__ + std::cout << "Replaced filename from " << filename_str << std::endl; + filename_str = replaceUsername(filename_str); + std::cout << "to " << filename_str << std::endl; + #endif size_t file_size; read(client_socket, &file_size, sizeof(file_size)); @@ -153,6 +185,8 @@ void *broadcast_listener(void *) { (const struct sockaddr *)&client_addr, addr_len); std::cout << "Server discovery response sent to multicast group" << std::endl; + + pthread_exit(0); } } @@ -160,11 +194,7 @@ void *broadcast_listener(void *) { pthread_exit(nullptr); } -int main() { -#ifdef __SWITCH__ - socketInitializeDefault(); - nxlinkStdio(); -#endif +int startSendingThread() { pthread_t broadcast_thread; if (pthread_create(&broadcast_thread, nullptr, broadcast_listener, nullptr) < 0) { @@ -197,6 +227,12 @@ int main() { exit(EXIT_FAILURE); } + std::cout << "Wait for broadcast thread done " << std::endl; + + pthread_join(broadcast_thread, NULL); + + std::cout << "Broadcast thread done " << std::endl; + std::cout << "Server listening on port " << PORT << std::endl; while (true) { @@ -222,14 +258,17 @@ int main() { std::cerr << "Ошибка создания потока\n"; delete pclient; // Освобождаем память при ошибке } else { - pthread_detach(thread_id); // Отсоединяем поток, чтобы он мог завершиться - // самостоятельно + pthread_join(thread_id, NULL); + break; } } close(server_fd); -#ifdef __SWITCH__ - socketExit(); -#endif return 0; } + +#ifndef __SWITCH__ // for desktop +int main() { + return startSendingThread(); +} +#endif