transfer to switch
This commit is contained in:
1
include/server.hpp
Normal file
1
include/server.hpp
Normal file
@@ -0,0 +1 @@
|
|||||||
|
int startSendingThread();
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <main.hpp>
|
#include <main.hpp>
|
||||||
#include <const.h>
|
#include <const.h>
|
||||||
#include <client.hpp>
|
#include <client.hpp>
|
||||||
|
#include <server.hpp>
|
||||||
|
|
||||||
static std::vector<uint64_t> accSids, devSids, bcatSids, cacheSids;
|
static std::vector<uint64_t> accSids, devSids, bcatSids, cacheSids;
|
||||||
|
|
||||||
@@ -34,11 +35,11 @@ namespace ui {
|
|||||||
Title title;
|
Title title;
|
||||||
getTitle(title, g_currentUId, index);
|
getTitle(title, g_currentUId, index);
|
||||||
printf("userid is 0x%lX%lX\n", title.userId().uid[1], title.userId().uid[0]);
|
printf("userid is 0x%lX%lX\n", title.userId().uid[1], title.userId().uid[0]);
|
||||||
printf("current game index is %i\n", index);
|
// printf("current game index is %i\n", index);
|
||||||
int opt = mainApp->CreateShowDialog(title.name().c_str(), "What do you want?", { "Transfer", "Receive" }, true);
|
int opt = mainApp->CreateShowDialog(title.name().c_str(), "What do you want?", { "Transfer", "Receive" }, false);
|
||||||
|
printf("opt is %i\n", opt);
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 0: {
|
case 0: {
|
||||||
printf("path is %s\n", title.fullPath(0).c_str());
|
|
||||||
// Transfer selected
|
// Transfer selected
|
||||||
auto result = io::backup(index, g_currentUId);
|
auto result = io::backup(index, g_currentUId);
|
||||||
if (std::get<0>(result)) {
|
if (std::get<0>(result)) {
|
||||||
@@ -51,6 +52,18 @@ namespace ui {
|
|||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
// Receive selected
|
// 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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,9 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#ifdef __SWITCH__
|
#ifdef __SWITCH__
|
||||||
|
#include <server.hpp>
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
#include <main.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define PORT 8080
|
#define PORT 8080
|
||||||
@@ -23,6 +25,32 @@
|
|||||||
|
|
||||||
namespace fs = std::filesystem;
|
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) {
|
void sendAck(int sock) {
|
||||||
const char *ack = "ACK";
|
const char *ack = "ACK";
|
||||||
std::cout << "Sending ACK " << std::endl;
|
std::cout << "Sending ACK " << std::endl;
|
||||||
@@ -90,8 +118,12 @@ void *handle_client(void *socket_desc) {
|
|||||||
delete[] filename; // Clean up filename buffer
|
delete[] filename; // Clean up filename buffer
|
||||||
|
|
||||||
std::cout << "Received filename_str is " << filename_str << std::endl;
|
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;
|
size_t file_size;
|
||||||
read(client_socket, &file_size, sizeof(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);
|
(const struct sockaddr *)&client_addr, addr_len);
|
||||||
std::cout << "Server discovery response sent to multicast group"
|
std::cout << "Server discovery response sent to multicast group"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,11 +194,7 @@ void *broadcast_listener(void *) {
|
|||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int startSendingThread() {
|
||||||
#ifdef __SWITCH__
|
|
||||||
socketInitializeDefault();
|
|
||||||
nxlinkStdio();
|
|
||||||
#endif
|
|
||||||
pthread_t broadcast_thread;
|
pthread_t broadcast_thread;
|
||||||
if (pthread_create(&broadcast_thread, nullptr, broadcast_listener, nullptr) <
|
if (pthread_create(&broadcast_thread, nullptr, broadcast_listener, nullptr) <
|
||||||
0) {
|
0) {
|
||||||
@@ -197,6 +227,12 @@ int main() {
|
|||||||
exit(EXIT_FAILURE);
|
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;
|
std::cout << "Server listening on port " << PORT << std::endl;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -222,14 +258,17 @@ int main() {
|
|||||||
std::cerr << "Ошибка создания потока\n";
|
std::cerr << "Ошибка создания потока\n";
|
||||||
delete pclient; // Освобождаем память при ошибке
|
delete pclient; // Освобождаем память при ошибке
|
||||||
} else {
|
} else {
|
||||||
pthread_detach(thread_id); // Отсоединяем поток, чтобы он мог завершиться
|
pthread_join(thread_id, NULL);
|
||||||
// самостоятельно
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
#ifdef __SWITCH__
|
|
||||||
socketExit();
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef __SWITCH__ // for desktop
|
||||||
|
int main() {
|
||||||
|
return startSendingThread();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user