73 lines
1.8 KiB
C++
73 lines
1.8 KiB
C++
|
//
|
||
|
// Created by WolverinDEV on 28/04/2020.
|
||
|
//
|
||
|
|
||
|
#include <netinet/in.h>
|
||
|
#include "LocalFileProvider.h"
|
||
|
|
||
|
using namespace ts::server;
|
||
|
using LocalFileServer = file::LocalFileProvider;
|
||
|
|
||
|
std::shared_ptr<LocalFileServer> server_instance{};
|
||
|
bool file::initialize(std::string &error) {
|
||
|
server_instance = std::make_shared<LocalFileProvider>();
|
||
|
if(!server_instance->initialize(error)) {
|
||
|
server_instance = nullptr;
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void file::finalize() {
|
||
|
auto server = std::exchange(server_instance, nullptr);
|
||
|
if(!server) return;
|
||
|
|
||
|
server->finalize();
|
||
|
}
|
||
|
|
||
|
std::shared_ptr<file::AbstractFileServer> file::server() {
|
||
|
return server_instance;
|
||
|
}
|
||
|
|
||
|
LocalFileServer::LocalFileProvider() : file_system_{}, file_transfer_{this->file_system_} {}
|
||
|
LocalFileServer::~LocalFileProvider() {}
|
||
|
|
||
|
bool LocalFileServer::initialize(std::string &error) {
|
||
|
if(!this->file_system_.initialize(error, "file-root/"))
|
||
|
return false;
|
||
|
|
||
|
|
||
|
std::deque<std::shared_ptr<transfer::NetworkBinding>> bindings{};
|
||
|
{
|
||
|
auto binding = std::make_shared<transfer::NetworkBinding>();
|
||
|
|
||
|
binding->hostname = "localhost";
|
||
|
|
||
|
auto& iaddr = *(sockaddr_in*) &binding->address;
|
||
|
iaddr.sin_family = AF_INET;
|
||
|
iaddr.sin_port = htons(1112);
|
||
|
iaddr.sin_addr.s_addr = INADDR_ANY;
|
||
|
|
||
|
bindings.push_back(std::move(binding));
|
||
|
}
|
||
|
|
||
|
if(!this->file_transfer_.start(bindings)) {
|
||
|
error = "transfer server startup failed";
|
||
|
this->file_system_.finalize();
|
||
|
return false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void LocalFileServer::finalize() {
|
||
|
this->file_transfer_.stop();
|
||
|
this->file_system_.finalize();
|
||
|
}
|
||
|
|
||
|
file::filesystem::AbstractProvider &LocalFileServer::file_system() {
|
||
|
return this->file_system_;
|
||
|
}
|
||
|
|
||
|
file::transfer::AbstractProvider & LocalFileServer::file_transfer() {
|
||
|
return this->file_transfer_;
|
||
|
}
|