Formatting and cleanup

This commit is contained in:
Maschell 2022-01-30 21:05:09 +01:00
parent 8c9c2f3bb1
commit 5e6e8bdb53
7 changed files with 37 additions and 45 deletions

View File

@ -11,35 +11,34 @@ BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread:
this->serverSocket = create_server(PORT); this->serverSocket = create_server(PORT);
DCFlushRange(&(this->serverSocket), 4); DCFlushRange(&(this->serverSocket), 4);
mutex.unlock(); mutex.unlock();
DEBUG_FUNCTION_LINE("Resume Thread");
CThread::resumeThread(); CThread::resumeThread();
} }
BackgroundThread::~BackgroundThread() { BackgroundThread::~BackgroundThread() {
DEBUG_FUNCTION_LINE("Shutting down FTP Server"); DEBUG_FUNCTION_LINE("Shutting down FTP Server");
if (this->serverSocket != -1) {
mutex.lock(); mutex.lock();
if (this->serverSocket >= 0) {
cleanup_ftp(); cleanup_ftp();
network_close(this->serverSocket); network_close(this->serverSocket);
mutex.unlock();
this->serverSocket = -1; this->serverSocket = -1;
} }
mutex.unlock();
} }
BOOL BackgroundThread::whileLoop() { BOOL BackgroundThread::whileLoop() {
if (this->serverSocket != -1) {
mutex.lock(); mutex.lock();
if (this->serverSocket >= 0) {
network_down = process_ftp_events(this->serverSocket); network_down = process_ftp_events(this->serverSocket);
mutex.unlock();
if (network_down) { if (network_down) {
DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket); DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket);
mutex.lock();
cleanup_ftp(); cleanup_ftp();
network_close(this->serverSocket); network_close(this->serverSocket);
this->serverSocket = -1; this->serverSocket = -1;
DCFlushRange(&(this->serverSocket), 4); DCFlushRange(&(this->serverSocket), 4);
}
}
mutex.unlock(); mutex.unlock();
}
}
OSSleepTicks(OSMillisecondsToTicks(16)); OSSleepTicks(OSMillisecondsToTicks(16));
return true; return true;
} }

View File

@ -9,25 +9,18 @@
class BackgroundThread : BackgroundThreadWrapper { class BackgroundThread : BackgroundThreadWrapper {
public: public:
static BackgroundThread *getInstance() { static BackgroundThread *getInstance() {
DCFlushRange(&instance, sizeof(BackgroundThread));
ICInvalidateRange(&instance, sizeof(BackgroundThread));
if (instance == nullptr) { if (instance == nullptr) {
instance = new BackgroundThread(); instance = new BackgroundThread();
DCFlushRange(&instance, sizeof(BackgroundThread)); DCFlushRange(&instance, 4);
ICInvalidateRange(&instance, sizeof(BackgroundThread));
} }
return instance; return instance;
} }
static void destroyInstance() { static void destroyInstance() {
DCFlushRange(&instance, sizeof(BackgroundThread));
ICInvalidateRange(&instance, sizeof(BackgroundThread));
OSSleepTicks(OSSecondsToTicks(1));
if (instance != nullptr) { if (instance != nullptr) {
delete instance; delete instance;
instance = nullptr; instance = nullptr;
DCFlushRange(&instance, sizeof(BackgroundThread)); DCFlushRange(&instance, 4);
ICInvalidateRange(&instance, sizeof(BackgroundThread));
} }
} }

View File

@ -56,8 +56,7 @@ static uint8_t num_clients = 0;
static uint16_t passive_port = 1024; static uint16_t passive_port = 1024;
static char *password = NULL; static char *password = NULL;
void console_printf(const char *format, ...) { #define console_printf(FMT, ARGS...) DEBUG_FUNCTION_LINE_WRITE(FMT, ## ARGS);
}
typedef int32_t (*data_connection_callback)(int32_t data_socket, void *arg); typedef int32_t (*data_connection_callback)(int32_t data_socket, void *arg);
@ -330,7 +329,7 @@ static int32_t ftp_PASV(client_t *client, char *rest UNUSED) {
char reply[49]; char reply[49];
uint16_t port = bindAddress.sin_port; uint16_t port = bindAddress.sin_port;
uint32_t ip = network_gethostip(); uint32_t ip = network_gethostip();
struct in_addr addr; struct in_addr addr = {};
addr.s_addr = ip; addr.s_addr = ip;
console_printf("Listening for data connections at %s:%u...\n", inet_ntoa(addr), port); console_printf("Listening for data connections at %s:%u...\n", inet_ntoa(addr), port);
sprintf(reply, "Entering Passive Mode (%u,%u,%u,%u,%u,%u).", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, (port >> 8) & 0xff, port & 0xff); sprintf(reply, "Entering Passive Mode (%u,%u,%u,%u,%u,%u).", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, (port >> 8) & 0xff, port & 0xff);
@ -748,7 +747,7 @@ void cleanup_ftp() {
static bool process_accept_events(int32_t server) { static bool process_accept_events(int32_t server) {
int32_t peer; int32_t peer;
struct sockaddr_in client_address; struct sockaddr_in client_address;
int32_t addrlen = sizeof(client_address); socklen_t addrlen = sizeof(client_address);
while ((peer = network_accept(server, (struct sockaddr *) &client_address, &addrlen)) != -EAGAIN) { while ((peer = network_accept(server, (struct sockaddr *) &client_address, &addrlen)) != -EAGAIN) {
if (peer < 0) { if (peer < 0) {
console_printf("Error accepting connection: [%i] %s\n", -peer, strerror(-peer)); console_printf("Error accepting connection: [%i] %s\n", -peer, strerror(-peer));
@ -807,7 +806,7 @@ static void process_data_events(client_t *client) {
if (!client->data_connection_connected) { if (!client->data_connection_connected) {
if (client->passive_socket >= 0) { if (client->passive_socket >= 0) {
struct sockaddr_in data_peer_address; struct sockaddr_in data_peer_address;
int32_t addrlen = sizeof(data_peer_address); socklen_t addrlen = sizeof(data_peer_address);
result = network_accept(client->passive_socket, (struct sockaddr *) &data_peer_address, &addrlen); result = network_accept(client->passive_socket, (struct sockaddr *) &data_peer_address, &addrlen);
if (result >= 0) { if (result >= 0) {
client->data_socket = result; client->data_socket = result;

View File

@ -11,9 +11,6 @@
#include "virtualpath.h" #include "virtualpath.h"
#include "BackgroundThread.hpp" #include "BackgroundThread.hpp"
#define MAX_CONSOLE_LINES_TV 27
#define MAX_CONSOLE_LINES_DRC 18
WUPS_PLUGIN_NAME("FTPiiU"); WUPS_PLUGIN_NAME("FTPiiU");
WUPS_PLUGIN_DESCRIPTION("FTP Server"); WUPS_PLUGIN_DESCRIPTION("FTP Server");
WUPS_PLUGIN_VERSION("0.1"); WUPS_PLUGIN_VERSION("0.1");
@ -39,11 +36,15 @@ ON_APPLICATION_START() {
//! Initialize FS * //! Initialize FS *
//!******************************************************************* //!*******************************************************************
VirtualMountDevice("fs:/");
AddVirtualFSPath("vol", nullptr, nullptr);
AddVirtualFSVOLPath("external01", nullptr, nullptr);
AddVirtualFSVOLPath("content", nullptr, nullptr);
DEBUG_FUNCTION_LINE("IOSUHAX_Open"); DEBUG_FUNCTION_LINE("IOSUHAX_Open");
int res = IOSUHAX_Open(nullptr); int res = IOSUHAX_Open(nullptr);
if (res < 0) { if (res < 0) {
DEBUG_FUNCTION_LINE("IOSUHAX_open failed"); DEBUG_FUNCTION_LINE("IOSUHAX_open failed");
VirtualMountDevice("fs:/");
} else { } else {
iosuhaxMount = 1; iosuhaxMount = 1;
//fatInitDefault(); //fatInitDefault();
@ -65,7 +66,6 @@ ON_APPLICATION_START() {
mount_fs("storage_mlc", fsaFd, nullptr, "/vol/storage_mlc01"); mount_fs("storage_mlc", fsaFd, nullptr, "/vol/storage_mlc01");
mount_fs("storage_usb", fsaFd, nullptr, "/vol/storage_usb01"); mount_fs("storage_usb", fsaFd, nullptr, "/vol/storage_usb01");
VirtualMountDevice("fs:/");
VirtualMountDevice("slccmpt01:/"); VirtualMountDevice("slccmpt01:/");
VirtualMountDevice("storage_odd_tickets:/"); VirtualMountDevice("storage_odd_tickets:/");
VirtualMountDevice("storage_odd_updates:/"); VirtualMountDevice("storage_odd_updates:/");
@ -75,10 +75,6 @@ ON_APPLICATION_START() {
VirtualMountDevice("storage_mlc:/"); VirtualMountDevice("storage_mlc:/");
VirtualMountDevice("storage_usb:/"); VirtualMountDevice("storage_usb:/");
VirtualMountDevice("usb:/"); VirtualMountDevice("usb:/");
AddVirtualFSPath("vol", nullptr, nullptr);
AddVirtualFSVOLPath("external01", nullptr, nullptr);
AddVirtualFSVOLPath("content", nullptr, nullptr);
} }
thread = BackgroundThread::getInstance(); thread = BackgroundThread::getInstance();
@ -93,6 +89,8 @@ ON_APPLICATION_REQUESTS_EXIT() {
DEBUG_FUNCTION_LINE("Ending ftp server"); DEBUG_FUNCTION_LINE("Ending ftp server");
stopThread(); stopThread();
DEBUG_FUNCTION_LINE("Ended ftp Server.");
if (iosuhaxMount) { if (iosuhaxMount) {
IOSUHAX_sdio_disc_interface.shutdown(); IOSUHAX_sdio_disc_interface.shutdown();
IOSUHAX_usb_disc_interface.shutdown(); IOSUHAX_usb_disc_interface.shutdown();
@ -109,7 +107,9 @@ ON_APPLICATION_REQUESTS_EXIT() {
IOSUHAX_Close(); IOSUHAX_Close();
} }
DEBUG_FUNCTION_LINE("Unmount virtual paths");
UnmountVirtualPaths(); UnmountVirtualPaths();
deinitLogging(); deinitLogging();
} }

View File

@ -67,7 +67,7 @@ void initialise_network() {
} }
#endif #endif
int32_t network_socket(uint32_t domain, uint32_t type, uint32_t protocol) { int32_t network_socket(int32_t domain, int32_t type, int32_t protocol) {
int sock = socket(domain, type, protocol); int sock = socket(domain, type, protocol);
if (sock < 0) { if (sock < 0) {
int err = -wiiu_geterrno(); int err = -wiiu_geterrno();
@ -94,7 +94,7 @@ int32_t network_listen(int32_t s, uint32_t backlog) {
return res; return res;
} }
int32_t network_accept(int32_t s, struct sockaddr *addr, int32_t *addrlen) { int32_t network_accept(int32_t s, struct sockaddr *addr, socklen_t *addrlen) {
int res = accept(s, addr, addrlen); int res = accept(s, addr, addrlen);
if (res < 0) { if (res < 0) {
int err = -wiiu_geterrno(); int err = -wiiu_geterrno();
@ -144,10 +144,17 @@ int32_t network_write(int32_t s, const void *mem, int32_t len) {
} }
int32_t network_close(int32_t s) { int32_t network_close(int32_t s) {
if (s < 0) if (s < 0) {
return -1; return -1;
}
return close(s); int res = close(s);
if (res < 0) {
int err = -wiiu_geterrno();
return (err < 0) ? err : res;
}
return res;
} }
int32_t set_blocking(int32_t s, bool blocking) { int32_t set_blocking(int32_t s, bool blocking) {
@ -163,9 +170,9 @@ int32_t network_close_blocking(int32_t s) {
int32_t create_server(uint16_t port) { int32_t create_server(uint16_t port) {
int32_t server = network_socket(AF_INET, SOCK_STREAM, IPPROTO_IP); int32_t server = network_socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (server < 0) if (server < 0) {
return -1; return -1;
}
set_blocking(server, false); set_blocking(server, false);
uint32_t enable = 1; uint32_t enable = 1;

View File

@ -32,7 +32,6 @@ extern "C"{
#include <stdbool.h> #include <stdbool.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
@ -43,13 +42,13 @@ extern "C"{
void initialise_network(); void initialise_network();
#endif #endif
int32_t network_socket(uint32_t domain, uint32_t type, uint32_t protocol); int32_t network_socket(int32_t domain, int32_t type, int32_t protocol);
int32_t network_bind(int32_t s, struct sockaddr *name, int32_t namelen); int32_t network_bind(int32_t s, struct sockaddr *name, int32_t namelen);
int32_t network_listen(int32_t s, uint32_t backlog); int32_t network_listen(int32_t s, uint32_t backlog);
int32_t network_accept(int32_t s, struct sockaddr *addr, int32_t *addrlen); int32_t network_accept(int32_t s, struct sockaddr *addr, socklen_t *addrlen);
int32_t network_connect(int32_t s, struct sockaddr *, int32_t); int32_t network_connect(int32_t s, struct sockaddr *, int32_t);

View File

@ -23,12 +23,7 @@ protected:
private: private:
void executeThread() override; void executeThread() override;
/**
Called when a connection has be accepted.
**/
virtual BOOL whileLoop() = 0; virtual BOOL whileLoop() = 0;
volatile int32_t exitThread = 0; volatile int32_t exitThread = 0;
}; };