MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
fileserve.cpp File Reference
#include "mxnetwork/socket.hpp"
#include <algorithm>
#include <atomic>
#include <charconv>
#include <cstddef>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <limits>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
#include <signal.h>
#include <sys/socket.h>

Go to the source code of this file.

Namespaces

namespace  anonymous_namespace{fileserve.cpp}

Functions

void anonymous_namespace{fileserve.cpp}::connect_client (std::string_view host, std::string_view port)
void anonymous_namespace{fileserve.cpp}::list_directory (mxnetwork::Socket &sock)
void anonymous_namespace{fileserve.cpp}::listen_server (std::string_view port)
void anonymous_namespace{fileserve.cpp}::listen_signal (int)
int main (int argc, char **argv)
std::optional< size_t > anonymous_namespace{fileserve.cpp}::parse_content_length (std::string_view data)
std::optional< std::string > anonymous_namespace{fileserve.cpp}::parse_get_filename (std::string_view command)
bool anonymous_namespace{fileserve.cpp}::parse_port (const char *value, unsigned int &port)
void anonymous_namespace{fileserve.cpp}::print_progress (int &prev, size_t length, size_t progress)
void anonymous_namespace{fileserve.cpp}::process_input (mxnetwork::Socket sock)
std::optional< std::string > anonymous_namespace{fileserve.cpp}::read_command (mxnetwork::Socket &sock)
void anonymous_namespace{fileserve.cpp}::receive_file (mxnetwork::Socket &sock, std::string_view command)
void anonymous_namespace{fileserve.cpp}::receive_listing (mxnetwork::Socket &sock)
void anonymous_namespace{fileserve.cpp}::request_stop ()
void anonymous_namespace{fileserve.cpp}::send_error (mxnetwork::Socket &sock, std::string_view message)
void anonymous_namespace{fileserve.cpp}::send_file (mxnetwork::Socket &sock, std::string_view filename)
bool anonymous_namespace{fileserve.cpp}::send_text (mxnetwork::Socket &sock, std::string_view text)
bool anonymous_namespace{fileserve.cpp}::valid_filename (std::string_view filename)
bool anonymous_namespace{fileserve.cpp}::validate_port (const char *program, const char *value, unsigned int &port)

Variables

std::atomic< bool > anonymous_namespace{fileserve.cpp}::active_loop {false}
constexpr size_t anonymous_namespace{fileserve.cpp}::buffer_size = 4096
std::atomic< mxnetwork::Socket * > anonymous_namespace{fileserve.cpp}::listen_socket {nullptr}
constexpr unsigned int anonymous_namespace{fileserve.cpp}::port_max = 4951
constexpr unsigned int anonymous_namespace{fileserve.cpp}::port_min = 1024
constexpr int anonymous_namespace{fileserve.cpp}::progress_width = 48

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 438 of file fileserve.cpp.

438 {
439 mxnetwork::MXNetworkInit network_init;
441
442#ifdef _WIN32
443 if (!SetConsoleCtrlHandler(console_handler, TRUE)) {
444 std::cerr << "fileserve: Could not install console handler.\n";
445 return EXIT_FAILURE;
446 }
447#else
448 struct sigaction sa = {};
449 sa.sa_handler = listen_signal;
450 sa.sa_flags = 0;
451 if (sigaction(SIGINT, &sa, nullptr) == -1) {
452 std::cerr << "fileserve: Could not install signal handler.\n";
453 return EXIT_FAILURE;
454 }
455#endif
456
457 unsigned int port = 0;
458 try {
459 if (argc == 2) {
460 if (!validate_port(argv[0], argv[1], port)) {
461 return EXIT_FAILURE;
462 }
463 listen_server(argv[1]);
464 } else if (argc == 3) {
465 if (!validate_port(argv[0], argv[2], port)) {
466 return EXIT_FAILURE;
467 }
468 std::cout << "Connecting...\n";
469 connect_client(argv[1], argv[2]);
470 } else {
471 std::cerr << "Error use:\n"
472 << argv[0] << " <port>\t\tfor listen (server)\n"
473 << argv[0] << " <host> <port>\tfor connect (client)\n";
474 return EXIT_SUCCESS;
475 }
476 } catch (const mxnetwork::Exception &error) {
477 std::cerr << "fileserve: " << error.text() << "\n";
478 return EXIT_FAILURE;
479 }
480
481 std::cout << "fileserve: Exiting.\n";
482 return EXIT_SUCCESS;
483}
Lightweight exception wrapper for MXNetwork failures.
Definition exception.hpp:11
std::string text() const
Return the stored error text.
Definition exception.cpp:5
void mx_socket_ignore_pipe_signal()
Ignore SIGPIPE on platforms that require it.
Definition mxsocket.c:577
void connect_client(std::string_view host, std::string_view port)
void listen_server(std::string_view port)
bool validate_port(const char *program, const char *value, unsigned int &port)
RAII helper that initializes and shuts down the platform socket subsystem.
Definition socket.hpp:21