transfer to switch

This commit is contained in:
2024-11-22 00:09:18 +03:00
parent 3851d474d0
commit 97c460fb5a
3 changed files with 68 additions and 15 deletions

1
include/server.hpp Normal file
View File

@@ -0,0 +1 @@
int startSendingThread();

View File

@@ -3,6 +3,7 @@
#include <main.hpp>
#include <const.h>
#include <client.hpp>
#include <server.hpp>
static std::vector<uint64_t> 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;
}
}

View File

@@ -13,7 +13,9 @@
#include <unistd.h>
#ifdef __SWITCH__
#include <server.hpp>
#include <switch.h>
#include <main.hpp>
#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