ftpiiu_plugin/src/BackgroundThread.cpp

45 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();
2022-01-30 21:05:09 +01:00
DEBUG_FUNCTION_LINE("Resume Thread");
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");
2022-01-30 21:05:09 +01:00
mutex.lock();
if (this->serverSocket >= 0) {
2021-09-24 19:49:11 +02:00
cleanup_ftp();
network_close(this->serverSocket);
2019-11-24 14:20:08 +01:00
this->serverSocket = -1;
}
2022-01-30 21:05:09 +01:00
mutex.unlock();
2019-11-24 14:20:08 +01:00
}
BOOL BackgroundThread::whileLoop() {
2022-01-30 21:05:09 +01:00
mutex.lock();
if (this->serverSocket >= 0) {
2021-09-24 19:49:11 +02:00
network_down = process_ftp_events(this->serverSocket);
if (network_down) {
2020-06-27 12:27:39 +02:00
DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket);
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
}
}
2022-01-30 21:05:09 +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;
}