Skip to content
35 changes: 33 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ find_package(ZLIB REQUIRED)

option(FTPD_CLASSIC "Build ${PROJECT_NAME} classic" OFF)

if(NINTENDO_DS OR NINTENDO_WII OR NINTENDO_GAMECUBE)
set(FTPD_CLASSIC ON)
endif()

if(FTPD_CLASSIC AND (NINTENDO_SWITCH OR NINTENDO_3DS))
set(FTPD_TARGET "${PROJECT_NAME}-classic")
else()
Expand Down Expand Up @@ -86,17 +90,23 @@ if(IPO_SUPPORTED)
)
endif()

if(FTPD_CLASSIC OR NINTENDO_DS)
if(FTPD_CLASSIC)
target_compile_definitions(${FTPD_TARGET} PRIVATE CLASSIC)
endif()

if(NINTENDO_SWITCH OR NINTENDO_3DS OR NINTENDO_DS)
if(NINTENDO_SWITCH OR NINTENDO_3DS OR NINTENDO_DS OR NINTENDO_WII OR NINTENDO_GAMECUBE)
target_compile_definitions(${FTPD_TARGET} PRIVATE
NO_IPV6
FTPDCONFIG="/config/${PROJECT_NAME}/${PROJECT_NAME}.cfg"
)
endif()

if(NINTENDO_WII OR NINTENDO_GAMECUBE OR NINTENDO_DS)
target_compile_definitions(${FTPD_TARGET} PRIVATE HAVE_MUTEX=1 HAVE_TLS=0)
else()
target_compile_definitions(${FTPD_TARGET} PRIVATE HAVE_MUTEX=1 HAVE_TLS=1)
endif()

target_sources(${FTPD_TARGET} PRIVATE
include/fs.h
include/ftpConfig.h
Expand Down Expand Up @@ -158,6 +168,7 @@ if(NINTENDO_SWITCH)
target_sources(${FTPD_TARGET} PRIVATE
source/switch/init.c
source/switch/platform.cpp
source/common/thread.cpp
)

if(FTPD_CLASSIC)
Expand Down Expand Up @@ -362,6 +373,7 @@ elseif(NINTENDO_DS)

target_sources(${FTPD_TARGET} PRIVATE
source/nds/platform.cpp
source/common/thread.cpp
)

nds_create_rom(${FTPD_TARGET}
Expand All @@ -370,6 +382,25 @@ elseif(NINTENDO_DS)
SUBTITLE2 "(c) 2025 mtheall"
ICON nds/icon.bmp
)
elseif(NINTENDO_WII OR NINTENDO_GAMECUBE)

target_link_libraries(${FTPD_TARGET} PRIVATE
fat
)

if(NINTENDO_GAMECUBE)
target_link_libraries(${FTPD_TARGET} PRIVATE
bba
)
endif()

target_sources(${FTPD_TARGET} PRIVATE
source/ogc/platform.cpp
source/common/thread.cpp
)

ogc_create_dol(${PROJECT_NAME})

else()
find_package(PkgConfig REQUIRED)
find_package(glfw3 REQUIRED)
Expand Down
6 changes: 2 additions & 4 deletions include/ftpConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ class FtpConfig
/// \param path_ Path to config file
static UniqueFtpConfig load (gsl::not_null<gsl::czstring> path_);

#ifndef __NDS__
#if HAVE_MUTEX
std::scoped_lock<platform::Mutex> lockGuard ();
#endif

/// \brief Save config
/// \param path_ Path to config file
bool save (gsl::not_null<gsl::czstring> path_);
Expand Down Expand Up @@ -139,11 +138,10 @@ class FtpConfig
private:
FtpConfig ();

#ifndef __NDS__
#if HAVE_MUTEX
/// \brief Mutex
mutable platform::Mutex m_lock;
#endif

/// \brief Username
std::string m_user;

Expand Down
2 changes: 1 addition & 1 deletion include/ftpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class FtpServer
/// \brief Thread entry point
void threadFunc ();

#ifndef __NDS__
#if HAVE_MUTEX
/// \brief Thread
platform::Thread m_thread;

Expand Down
14 changes: 12 additions & 2 deletions include/ftpSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class FtpSession

/// \brief Transfer buffersize
constexpr static auto XFER_BUFFERSIZE = 8192;
#elif defined (__wii__) || defined (__gamecube__)
/// \brief Response buffer size
constexpr static auto RESPONSE_BUFFERSIZE = 8192;

/// \brief Transfer buffersize
constexpr static auto XFER_BUFFERSIZE = 16384;
#else
/// \brief Response buffer size
constexpr static auto RESPONSE_BUFFERSIZE = 32768;
Expand All @@ -106,6 +112,11 @@ class FtpSession
/// \brief Socket buffer size
constexpr static auto SOCK_BUFFERSIZE = 32768;

/// \brief Amount of file position history to keep
constexpr static auto POSITION_HISTORY = 100;
#elif defined(__wii__) || defined(__gamecube__)
/// \brief Socket buffer size
constexpr static auto SOCK_BUFFERSIZE = 16384;
/// \brief Amount of file position history to keep
constexpr static auto POSITION_HISTORY = 100;
#else
Expand Down Expand Up @@ -246,11 +257,10 @@ class FtpSession
/// \brief Transfer upload
bool storeTransfer ();

#ifndef __NDS__
#if HAVE_MUTEX
/// \brief Mutex
platform::Mutex m_lock;
#endif

/// \brief FTP config
FtpConfig &m_config;

Expand Down
10 changes: 8 additions & 2 deletions include/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@
#include <switch.h>
#endif

#if defined (__wii__) || defined(__gamecube__)
#include <ogcsys.h>
#include <gccore.h>
#include <ogc/console.h>
#endif

#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>

#ifdef CLASSIC
#if defined(CLASSIC)
extern PrintConsole g_statusConsole;
extern PrintConsole g_logConsole;
extern PrintConsole g_sessionConsole;
Expand Down Expand Up @@ -113,7 +119,7 @@ struct steady_clock
using steady_clock = std::chrono::steady_clock;
#endif

#ifndef __NDS__
#if HAVE_MUTEX
/// \brief Platform thread
class Thread
{
Expand Down
99 changes: 99 additions & 0 deletions source/common/thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "platform.h"

#include <mutex>
#include <thread>


///////////////////////////////////////////////////////////////////////////
/// \brief Platform thread pimpl
class platform::Thread::privateData_t
{
public:
privateData_t () = default;

/// \brief Parameterized constructor
/// \param func_ Thread entry point
privateData_t (std::function<void ()> &&func_) : thread (std::move (func_))
{
}

/// \brief Underlying thread
std::thread thread;
};

///////////////////////////////////////////////////////////////////////////
platform::Thread::~Thread () = default;

platform::Thread::Thread () : m_d (new privateData_t ())
{
}

platform::Thread::Thread (std::function<void ()> &&func_)
: m_d (new privateData_t (std::move (func_)))
{
}

platform::Thread::Thread (Thread &&that_) : m_d (new privateData_t ())
{
std::swap (m_d, that_.m_d);
}

platform::Thread &platform::Thread::operator= (Thread &&that_)
{
std::swap (m_d, that_.m_d);
return *this;
}

void platform::Thread::join ()
{
m_d->thread.join ();
}

void platform::Thread::sleep (std::chrono::milliseconds const timeout_)
{
std::this_thread::sleep_for (timeout_);
}

///////////////////////////////////////////////////////////////////////////
#define USE_STD_MUTEX 1

/// \brief Platform mutex pimpl
class platform::Mutex::privateData_t
{
public:
#if USE_STD_MUTEX
/// \brief Underlying mutex
std::mutex mutex;
#else
/// \brief Underlying mutex
::Mutex mutex;
#endif
};

///////////////////////////////////////////////////////////////////////////
platform::Mutex::~Mutex () = default;

platform::Mutex::Mutex () : m_d (new privateData_t ())
{
#if !USE_STD_MUTEX
mutexInit (&m_d->mutex);
#endif
}

void platform::Mutex::lock ()
{
#if USE_STD_MUTEX
m_d->mutex.lock ();
#else
mutexLock (&m_d->mutex);
#endif
}

void platform::Mutex::unlock ()
{
#if USE_STD_MUTEX
m_d->mutex.unlock ();
#else
mutexUnlock (&m_d->mutex);
#endif
}
2 changes: 1 addition & 1 deletion source/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <type_traits>
#include <utility>

#if defined(__NDS__) || defined(__3DS__) || defined(__SWITCH__)
#if defined(__NDS__) || defined(__3DS__) || defined(__SWITCH__) || defined(__wii__) || defined (__gamecube__)
#define getline __getline
#endif

Expand Down
3 changes: 2 additions & 1 deletion source/ftpConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ using stat_t = struct stat;
#include <string_view>
#include <system_error>
#include <utility>
#include <limits>

namespace
{
Expand Down Expand Up @@ -196,7 +197,7 @@ UniqueFtpConfig FtpConfig::load (gsl::not_null<gsl::czstring> const path_)
return config;
}

#ifndef __NDS__
#if HAVE_MUTEX
std::scoped_lock<platform::Mutex> FtpConfig::lockGuard ()
{
return std::scoped_lock<platform::Mutex> (m_lock);
Expand Down
Loading