ftpiiu_plugin/src/BackgroundThread.cpp

46 lines
1.3 KiB
C++
Raw Normal View History

2019-11-24 14:20:08 +01:00
#include "BackgroundThread.hpp"
2021-09-24 19:49:11 +02:00
#include <cstring>
2019-11-24 14:20:08 +01:00
#include "ftp.h"
#include "net.h"
2021-09-24 19:49:11 +02:00
BackgroundThread *BackgroundThread::instance = nullptr;
2019-11-24 14:20:08 +01:00
2021-09-24 19:49:11 +02:00
BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread::getPriority()) {
DEBUG_FUNCTION_LINE("Start FTP Server");
2019-11-24 14:20:08 +01:00
mutex.lock();
2021-09-24 19:49:11 +02:00
this->serverSocket = create_server(PORT);
DCFlushRange(&(this->serverSocket), 4);
2019-11-24 14:20:08 +01:00
mutex.unlock();
2020-11-26 23:32:44 +01:00
CThread::resumeThread();
2019-11-24 14:20:08 +01:00
}
BackgroundThread::~BackgroundThread() {
2021-09-24 19:49:11 +02:00
DEBUG_FUNCTION_LINE("Shutting down FTP Server");
if (this->serverSocket != -1) {
2019-11-24 14:20:08 +01:00
mutex.lock();
2021-09-24 19:49:11 +02:00
cleanup_ftp();
network_close(this->serverSocket);
2019-11-24 14:20:08 +01:00
mutex.unlock();
this->serverSocket = -1;
}
}
BOOL BackgroundThread::whileLoop() {
2021-09-24 19:49:11 +02:00
if (this->serverSocket != -1) {
2019-11-24 14:20:08 +01:00
mutex.lock();
2021-09-24 19:49:11 +02:00
network_down = process_ftp_events(this->serverSocket);
2019-11-24 14:20:08 +01:00
mutex.unlock();
2021-09-24 19:49:11 +02:00
if (network_down) {
2020-06-27 12:27:39 +02:00
DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket);
2019-11-24 14:20:08 +01:00
mutex.lock();
2021-09-24 19:49:11 +02:00
cleanup_ftp();
network_close(this->serverSocket);
this->serverSocket = -1;
DCFlushRange(&(this->serverSocket), 4);
2019-11-24 14:20:08 +01:00
mutex.unlock();
}
}
2020-11-26 23:32:44 +01:00
OSSleepTicks(OSMillisecondsToTicks(16));
2019-11-24 14:20:08 +01:00
return true;
}