commit d6e3180e246d82e01f17fe84983d657de7a23a25 from: Aleksey Ryndin date: Wed Aug 30 09:20:05 2023 UTC Add C++ coding style commit - 51254cccedb5bd0fe1e68c8d11c0af9941de9b73 commit + d6e3180e246d82e01f17fe84983d657de7a23a25 blob - /dev/null blob + 3adb3f1a33725888bb9995182902bfa952a29d97 (mode 644) --- /dev/null +++ capsule/coding_style.gmi @@ -0,0 +1,32 @@ +# vostok C++ coding style + +=> https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines C++ Core Guidelines + +## Files + +* .cc +* .h + +## Names + +Styles: +* CamelCase for class and struct; +* snake_case for namespace, variable, function and method; +* SCREAMING_CASE for constants. + +Prefixes: +* Use "m_" prefix for private members of class ; +* Use "s_" prefix for static members of class; +* Use "g_" prefix for global variables. + +## class / struct + +struct: +* data members can vary independently; +* all members are public; +* does not contain methods. + +class: +* has an invariant +* all members are private; + blob - cea622f498bd0f077d52e69efb29a893e6b0bdb7 blob + 3f4fbd8aecafd4938a205056f29188facaeb1e3c --- tests/test_open_file.cc +++ tests/test_open_file.cc @@ -24,23 +24,23 @@ TEST_START(test_open_file) ss << "/tmp/test_open_file." << getpid() << "." << time(nullptr); IS_TRUE_ERRNO(mkdir(ss.str().c_str(), S_IRWXU) != -1, "Create directory " << ss.str().c_str()); - const unique_fd dir{open(ss.str().c_str(), O_RDONLY | O_DIRECTORY)}; + const UniqueFd dir{open(ss.str().c_str(), O_RDONLY | O_DIRECTORY)}; IS_TRUE_ERRNO(dir, "Open directory " << ss.str().c_str()); { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "", opened_file) == file_not_found and !opened_file); IS_TRUE(open_file(dir.get(), "non-existent-file", opened_file) == file_not_found and !opened_file); IS_TRUE(open_file(dir.get(), "non-existent-dir/file", opened_file) == file_not_found and !opened_file); } - const unique_fd index_gmi{openat(dir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)}; + const UniqueFd index_gmi{openat(dir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)}; IS_TRUE_ERRNO(index_gmi, "Create file " << ss.str().c_str() << "/index.gmi"); struct stat sb1{}; IS_TRUE_ERRNO(fstat(index_gmi.get(), &sb1) != -1, "Stat file " << ss.str().c_str() << "/index.gmi"); { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "", opened_file) == file_opened and opened_file); struct stat sb2{}; IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/index.gmi"); @@ -48,7 +48,7 @@ TEST_START(test_open_file) } { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "index.gmi", opened_file) == file_opened and opened_file); struct stat sb2{}; IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/index.gmi"); @@ -57,19 +57,19 @@ TEST_START(test_open_file) IS_TRUE_ERRNO(mkdirat(dir.get(), "subdir", S_IRWXU) != -1, "Create directory " << ss.str().c_str() << "/subdir"); { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "subdir", opened_file) == file_not_found and !opened_file); IS_TRUE(open_file(dir.get(), "subdir/", opened_file) == file_not_found and !opened_file); } - const unique_fd subdir{openat(dir.get(), "subdir", O_RDONLY | O_DIRECTORY)}; + const UniqueFd subdir{openat(dir.get(), "subdir", O_RDONLY | O_DIRECTORY)}; IS_TRUE_ERRNO(subdir, "Open directory " << ss.str().c_str() << "/subdir"); - const unique_fd sub_index_gmi{openat(subdir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)}; + const UniqueFd sub_index_gmi{openat(subdir.get(), "index.gmi", O_RDONLY | O_CREAT | O_EXCL, S_IRWXU)}; IS_TRUE_ERRNO(sub_index_gmi, "Create file " << ss.str().c_str() << "/subdir/index.gmi"); IS_TRUE_ERRNO(fstat(sub_index_gmi.get(), &sb1) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi"); { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "subdir", opened_file) == file_opened and opened_file); struct stat sb2{}; IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi"); @@ -77,7 +77,7 @@ TEST_START(test_open_file) } { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "subdir/", opened_file) == file_opened and opened_file); struct stat sb2{}; IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi"); @@ -85,18 +85,18 @@ TEST_START(test_open_file) } { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "subdir/index.gmi", opened_file) == file_opened and opened_file); struct stat sb2{}; IS_TRUE_ERRNO(fstat(opened_file.get(), &sb2) != -1, "Stat file " << ss.str().c_str() << "/subdir/index.gmi"); IS_TRUE((sb1.st_dev == sb2.st_dev) && (sb1.st_ino == sb2.st_ino)); } - const unique_fd eperm{openat(subdir.get(), "EPERM.gmi", O_RDONLY | O_CREAT | O_EXCL, 0)}; + const UniqueFd eperm{openat(subdir.get(), "EPERM.gmi", O_RDONLY | O_CREAT | O_EXCL, 0)}; IS_TRUE_ERRNO(eperm, "Create file " << ss.str().c_str() << "/subdir/index.gmi"); { - unique_fd opened_file{}; + UniqueFd opened_file; IS_TRUE(open_file(dir.get(), "subdir/EPERM.gmi", opened_file) == file_opening_error and !opened_file); } blob - 94b5019fb6f0bd231cc26ba512811ea27082155b blob + 19144e4a70be55173bbd017c1697f77ebf2669bc --- vostok/args.cc +++ vostok/args.cc @@ -35,40 +35,45 @@ bool usage(const char *program) } // namespace -bool command_line_arguments::parse(int argc, char *argv[]) +bool +parse_command_line_arguments( + /* in */ int argc, + /* in */ char *argv[], + /* out */ CommandLineArguments &args +) { int ch; char *p = nullptr; while ((ch = getopt(argc, argv, "a:p:c:k:f:")) != -1) { switch (ch) { case 'a': - m_addr = optarg; + args.addr = optarg; break; case 'p': p = nullptr; - m_port = std::strtoul(optarg, &p, 10); - if (!m_port) + args.port = std::strtoul(optarg, &p, 10); + if (!args.port) { error::g_log << "Invalid PORT value: " << optarg << std::endl; return usage(argv[0]); } break; case 'c': - m_cert_file = optarg; + args.cert_file = optarg; break; case 'k': - m_key_file = optarg; + args.key_file = optarg; break; case 'f': - m_directory.reset(open(optarg, O_RDONLY)); - if (!m_directory) + args.directory.reset(open(optarg, O_RDONLY)); + if (!args.directory) { error::occurred( [] { error::g_log << "Open directory \"" << optarg << "\""; }, - error::print{} + error::Print{} ); return false; } @@ -78,17 +83,17 @@ bool command_line_arguments::parse(int argc, char *arg return usage(argv[0]); } } - if (!m_cert_file) + if (!args.cert_file) { error::g_log << "Invalid command line: -c option required" << std::endl; return usage(argv[0]); } - if (!m_key_file) + if (!args.key_file) { error::g_log << "Invalid command line: -k option required" << std::endl; return usage(argv[0]); } - if (!m_directory) + if (!args.directory) { error::g_log << "Invalid command line: -d option required" << std::endl; return usage(argv[0]); blob - 7f7b8682627a3b5ff65838d3724972184ead7e07 blob + 0d5d970247a1b2947a8fb3f7fb034b373f784656 --- vostok/args.h +++ vostok/args.h @@ -1,25 +1,30 @@ /** Parse command line arguments */ +#include "utils.h" #pragma once -#include "utils.h" - namespace vostok { -struct command_line_arguments +struct CommandLineArguments { - not_null m_addr{"127.0.0.1"}; - int m_port{1965}; - czstring m_cert_file{nullptr}; - czstring m_key_file{nullptr}; - unique_fd m_directory; - - bool parse(int argc, char *argv[]); + NotNull addr{"127.0.0.1"}; + int port{1965}; + czstring cert_file{nullptr}; + czstring key_file{nullptr}; + UniqueFd directory; }; +bool +parse_command_line_arguments( + /* in */ int argc, + /* in */ char *argv[], + /* out */ CommandLineArguments &args +); + + } // namespace vostok blob - a1791a32da6f24b2d3a3060d95fb971e744eaecd blob + 84d7f97f0e3b15bfa61f8da4c5e2419468355860 --- vostok/error.h +++ vostok/error.h @@ -18,33 +18,37 @@ extern std::ostream &g_log; /** Default error code (errno) printer */ -struct print +class Print { - const int m_error; - explicit print(int error=errno) : m_error{error} {} +public: + explicit Print(int error=errno) : m_error{error} {} void operator() () const { g_log << "Error code: " << std::dec << m_error << ". " << strerror(m_error) << std::endl; } + +private: + const int m_error; }; /** Empty error code printer */ -struct none +class None { +public: void operator() () const {} }; /** Error handler: print action, print error */ template< - typename TPrintAction, - typename TPrintError + typename PrintAction, + typename PrintError > void occurred( - TPrintAction print_action, - TPrintError print_error + PrintAction print_action, + PrintError print_error ) { print_action(); blob - 224e22e17c796b4e3b805df0d6582f1ad60cc932 blob + b9f759c3f7cb8c276e7736ddb6cf270de97787d4 --- vostok/open_file.cc +++ vostok/open_file.cc @@ -11,11 +11,11 @@ namespace vostok { namespace { -not_null index_gmi = "index.gmi"; +NotNull index_gmi = "index.gmi"; } // namespace -open_file_result open_file(int directory_fd, not_null file_name, unique_fd &opened_file) +open_file_result open_file(int directory_fd, NotNull file_name, UniqueFd &opened_file) { if (!*file_name) file_name = index_gmi; @@ -29,7 +29,7 @@ open_file_result open_file(int directory_fd, not_null< { error::g_log << "Open file \"" << file_name << "\""; }, - error::print{error_code} + error::Print{error_code} ); return (error_code == ENOENT) ? file_not_found : file_opening_error; } @@ -41,13 +41,13 @@ open_file_result open_file(int directory_fd, not_null< { error::g_log << "Stat file \"" << file_name << "\""; }, - error::print{} + error::Print{} ); return file_opening_error; } if (S_ISDIR(sb.st_mode)) { - const unique_fd new_parent{opened_file.release()}; + const UniqueFd new_parent{opened_file.release()}; opened_file.reset(openat(new_parent.get(), index_gmi, O_RDONLY)); if (!opened_file) { @@ -57,7 +57,7 @@ open_file_result open_file(int directory_fd, not_null< { error::g_log << "Open file \"" << file_name << "\" /" << index_gmi; }, - error::print{error_code} + error::Print{error_code} ); return (error_code == ENOENT) ? file_not_found : file_opening_error; } blob - b370afb5847fa49a237389954a908a3b9b1ab47f blob + fbbac29f2d23f9f486cc070d8bee803ae886cfc0 --- vostok/open_file.h +++ vostok/open_file.h @@ -17,7 +17,7 @@ enum open_file_result file_opening_error, }; -open_file_result open_file(int directory_fd, not_null file_name, unique_fd &opened_file); +open_file_result open_file(int directory_fd, NotNull file_name, UniqueFd &opened_file); } // namespace vostok blob - 1aaa90926bb3db42ac911c8ea9084866c41cc1a5 blob + d81f5fd62b340269f56acd085a42c8a107fe12e0 --- vostok/parse_url.cc +++ vostok/parse_url.cc @@ -25,10 +25,10 @@ inline bool is_gemini_scheme(const std::vector & ); } -class path_normalization +class PathNormalization { public: - using path_components_t = std::list< span >; + using Components = std::list< Span >; url_normalization_result operator() ( @@ -47,18 +47,18 @@ class path_normalization (protected) void fill(std::vector &url) const; private: - path_components_t::value_type m_inprogress; - path_components_t m_result; + Components::value_type m_inprogress; + Components m_result; }; url_normalization_result -path_normalization::operator() ( +PathNormalization::operator() ( std::vector::const_iterator p, std::vector &url ) { - m_inprogress = path_components_t::value_type{nullptr, 0}; + m_inprogress = Components::value_type{nullptr, 0}; m_result.clear(); for (; p != url.cend(); ++p) @@ -85,7 +85,7 @@ path_normalization::operator() ( } -url_normalization_result path_normalization::on_component() +url_normalization_result PathNormalization::on_component() { if ((m_inprogress.size() == 0) || (m_inprogress.size() == 1 && m_inprogress[0] == '.')) { @@ -104,7 +104,7 @@ url_normalization_result path_normalization::on_compon return on_component_ok(); } -void path_normalization::fill(std::vector &url) const +void PathNormalization::fill(std::vector &url) const { auto current = url.begin(); for (auto it = m_result.cbegin(); it != m_result.cend(); ++it) @@ -149,8 +149,7 @@ extract_url_path( } // normalize '.' and '..' - path_normalization normalizer; - return normalizer(current, url); + return PathNormalization{}(current, url); } blob - eee8490d9b37cbbf3be882e1591c5b26f75531d0 blob + 4fece0d30541a02b52be7cfc58bc84d700aaaab7 --- vostok/parse_url.h +++ vostok/parse_url.h @@ -22,9 +22,6 @@ enum url_normalization_result url_root_traverse, }; -/** Zero-terminated path from gemini URL */ -using zs_url_path_t = std::array; - /** Extract normalized path from URL as null-terminated string (inplace) */ url_normalization_result extract_url_path( blob - 51ee360ea9d26a76b2e8f366dbb3d17a9967b0ec blob + 2aaa254c5a3bfa60d5b3f39508e1eac1210709e8 --- vostok/transport.cc +++ vostok/transport.cc @@ -14,12 +14,19 @@ namespace transport namespace { -using config_t = std::unique_ptr; constexpr auto protocols = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3; -bool read(not_null ctx, std::vector &buff) +using ConfigPtrBase = std::unique_ptr; +class ConfigPtr : public ConfigPtrBase { +public: + explicit ConfigPtr(struct tls_config *p=nullptr) : ConfigPtrBase{p, tls_config_free} {} +}; + + +bool read(NotNull ctx, std::vector &buff) +{ ssize_t ret{}; for (; ; ) { @@ -53,7 +60,7 @@ bool init() { if (tls_init() == -1) { - error::occurred("TLS initialization", error::none{}); + error::occurred("TLS initialization", error::None{}); return false; } @@ -61,18 +68,18 @@ bool init() } -void create_server(not_null cert_file, not_null key_file, context_t &ret_ctx) +void create_server(NotNull cert_file, NotNull key_file, ContextPtr &ret_ctx) { - config_t cfg{tls_config_new(), tls_config_free}; + ConfigPtr cfg{tls_config_new()}; if (!cfg) { - error::occurred("Create TLS configuration", error::none{}); + error::occurred("Create TLS configuration", error::None{}); return; } if (tls_config_set_protocols(cfg.get(), protocols) == -1) { - error::occurred("Allow TLS 1.2 and TLS 1.3 protocols", error::none{}); + error::occurred("Allow TLS 1.2 and TLS 1.3 protocols", error::None{}); return; } if (tls_config_set_cert_file(cfg.get(), cert_file) == -1) @@ -82,7 +89,7 @@ void create_server(not_null cert_file, not_n { error::g_log << "Load certificate file " << cert_file; }, - error::none{} + error::None{} ); return; } @@ -93,15 +100,15 @@ void create_server(not_null cert_file, not_n { error::g_log << "Load key file " << key_file; }, - error::none{} + error::None{} ); return; } - context_t ctx{tls_server(), tls_free}; + ContextPtr ctx{tls_server()}; if (!ctx) { - error::occurred("Create TLS server context", error::none{}); + error::occurred("Create TLS server context", error::None{}); return; } @@ -123,7 +130,7 @@ void create_server(not_null cert_file, not_n } -accepted_client::accepted_client(int server_socket, struct tls *server_ctx) +AcceptedClient::AcceptedClient(int server_socket, struct tls *server_ctx) { struct sockaddr_in addr{}; socklen_t addrlen = sizeof(addr); @@ -131,14 +138,14 @@ accepted_client::accepted_client(int server_socket, st m_fd.reset(accept(server_socket, (struct sockaddr *)&addr, &addrlen)); if (!m_fd) { - error::occurred("Accept socket", error::print{}); + error::occurred("Accept socket", error::Print{}); return; } struct tls *ctx = nullptr; if (tls_accept_socket(server_ctx, &ctx, m_fd.get()) == -1) { - error::occurred("TLS accept", error::none{}); + error::occurred("TLS accept", error::None{}); return; } @@ -147,7 +154,7 @@ accepted_client::accepted_client(int server_socket, st } -bool read_request(not_null ctx, std::vector &url) +bool read_request(NotNull ctx, std::vector &url) { // url.resize(gemini::MAX_REQUEST_LENGTH); @@ -168,12 +175,12 @@ bool read_request(not_null ctx, std::vec } } - error::occurred("Parse request", error::none{}); + error::occurred("Parse request", error::None{}); return false; } -bool send_response(not_null ctx, gemini::status_t status, span meta) +bool send_response(NotNull ctx, gemini::status_t status, Span meta) { // > std::array buff; @@ -184,11 +191,11 @@ bool send_response(not_null ctx, gemini: current = std::copy(meta.begin(), meta.end(), current); current = std::copy(gemini::CRLF.cbegin(), gemini::CRLF.cend(), current); - return send(ctx, span{&buff[0], static_cast(current - buff.begin())}); + return send(ctx, Span{&buff[0], static_cast(current - buff.begin())}); } -bool send(not_null ctx, span buff) +bool send(NotNull ctx, Span buff) { ssize_t ret{0}; while (buff.size() > 0) blob - 22a608f8129218141bf49b9cc068aad1526d29c9 blob + 4f66289ff2a7033b98164f09f8534ffe59199dba --- vostok/transport.h +++ vostok/transport.h @@ -16,7 +16,12 @@ namespace transport /** `struct tls *` smart pointer */ -typedef std::unique_ptr context_t; +using ContextPtrBase = std::unique_ptr; +class ContextPtr : public ContextPtrBase +{ +public: + explicit ContextPtr(struct tls *p=nullptr) : ContextPtrBase{p, tls_free} {} +}; /** Per-process initialization */ @@ -24,36 +29,36 @@ bool init(); /* Create new TLS context for gemini server */ -void create_server(not_null cert_file, not_null key_file, context_t &ctx); +void create_server(NotNull cert_file, NotNull key_file, ContextPtr &server_ctx); /** Accept new client */ -class accepted_client +class AcceptedClient { public: - accepted_client(int server_socket, struct tls *server_ctx); + AcceptedClient(int server_socket, struct tls *server_ctx); bool is_accepted() const { return m_is_accepted; } struct tls *get_ctx() const { return m_ctx.get(); } int get_fd() const { return m_fd.get(); } private: - unique_fd m_fd; - context_t m_ctx{nullptr, tls_free}; + UniqueFd m_fd; + ContextPtr m_ctx; bool m_is_accepted{false}; }; /** Read genimi request and return url */ -bool read_request(not_null ctx, std::vector &url); +bool read_request(NotNull ctx, std::vector &url); /** Write gemini response */ -bool send_response(not_null ctx, gemini::status_t status, span meta); +bool send_response(NotNull ctx, gemini::status_t status, Span meta); /** Send raw bytes */ -bool send(not_null ctx, span buff); +bool send(NotNull ctx, Span buff); } // namespace transport blob - 987e0eb0db0d3606adc002559eeaca9834890165 blob + fece1ef2319fa1e4f2a37f7d1bf7542634df0c27 --- vostok/utils.h +++ vostok/utils.h @@ -19,20 +19,20 @@ using czstring=const char *; /** Non-copiable objects */ -struct non_copiable +struct NonCopiable { - non_copiable() = default; - non_copiable(const non_copiable &) = delete; - non_copiable &operator =(const non_copiable &) = delete; + NonCopiable() = default; + NonCopiable(const NonCopiable &) = delete; + NonCopiable &operator =(const NonCopiable &) = delete; }; /** Restricts a pointer to only hold non-null values. */ template -class not_null +class NotNull { public: - not_null(T p) :m_p{p} { assert(m_p != nullptr); } + NotNull(T p) :m_p{p} { assert(m_p != nullptr); } T get() const {return m_p;} operator T() const {return m_p;} @@ -40,16 +40,16 @@ class not_null (public) // prevents compilation when someone attempts to assign a null pointer constant - not_null(std::nullptr_t) = delete; - not_null& operator=(std::nullptr_t) = delete; + NotNull(std::nullptr_t) = delete; + NotNull& operator=(std::nullptr_t) = delete; // unwanted operators...pointers only point to single objects! - not_null& operator++() = delete; - not_null& operator--() = delete; - not_null operator++(int) = delete; - not_null operator--(int) = delete; - not_null& operator+=(std::ptrdiff_t) = delete; - not_null& operator-=(std::ptrdiff_t) = delete; + NotNull& operator++() = delete; + NotNull& operator--() = delete; + NotNull operator++(int) = delete; + NotNull operator--(int) = delete; + NotNull& operator+=(std::ptrdiff_t) = delete; + NotNull& operator-=(std::ptrdiff_t) = delete; void operator[](std::ptrdiff_t) const = delete; private: @@ -57,36 +57,36 @@ class not_null (public) }; -/** Simply std::span implementation for C++11 */ +/** Simply std::Span implementation for C++11 */ template -class span +class Span { public: using element_type = ElemT; using iterator = ElemT *; - constexpr span() : m_p{nullptr}, m_count{0} {} - constexpr span(element_type *p, std::size_t count) : m_p{count ? p : nullptr}, m_count{count} {} + constexpr Span() : m_p{nullptr}, m_count{0} {} + constexpr Span(element_type *p, std::size_t count) : m_p{count ? p : nullptr}, m_count{count} {} template - constexpr span(element_type (&arr)[N]) : m_p{arr}, m_count{N} {} + constexpr Span(element_type (&arr)[N]) : m_p{arr}, m_count{N} {} template - constexpr span(std::array &arr) : m_p{arr.data()}, m_count{N} {} + constexpr Span(std::array &arr) : m_p{arr.data()}, m_count{N} {} constexpr std::size_t size() const {return m_count;} constexpr iterator begin() const noexcept { return m_p; } constexpr iterator end() const noexcept { return m_p + size(); } - span first(std::size_t count) const + Span first(std::size_t count) const { assert(count <= m_count); - return span{m_p, count}; + return Span{m_p, count}; } - span subspan(std::size_t offset) const + Span subspan(std::size_t offset) const { assert(offset <= m_count); - return (offset < m_count) ? span{m_p + offset, m_count - offset} : span{}; + return (offset < m_count) ? Span{m_p + offset, m_count - offset} : Span{}; } element_type &operator[](std::size_t idx) const @@ -101,21 +101,21 @@ class span (private) }; -/** Remove null terminating character and return as span */ +/** Remove null terminating character and return as Span */ template -constexpr span cut_null(const char (&arr)[N]) +constexpr Span cut_null(const char (&arr)[N]) { static_assert(N > 0, "!(N > 0)"); - return span{arr, N - 1}; + return Span{arr, N - 1}; } /** Smart pointer for file descriptor */ -class unique_fd : non_copiable +class UniqueFd : public NonCopiable { public: - explicit unique_fd(int fd = -1) : m_fd(fd) {} - ~unique_fd(){ reset();} + explicit UniqueFd(int fd = -1) : m_fd(fd) {} + ~UniqueFd(){ reset();} operator bool() const {return m_fd != -1;} blob - a5bb489e81d2f77428c326766a2fdcc78353f61a blob + 1522a58db3d9e2729758ad19dda6d5cf73e2ba8f --- vostok/vostok.cc +++ vostok/vostok.cc @@ -41,10 +41,10 @@ const auto temporary_failure = cut_null(sz_temporary_f } // namespace meta -void client_thread(const transport::accepted_client *accepted_client, int directory_fd) +void client_thread(const transport::AcceptedClient *accepted_client, int directory_fd) { assert(accepted_client); - std::unique_ptr accepted_client_deleter{accepted_client}; + std::unique_ptr accepted_client_deleter{accepted_client}; std::vector url; if (!transport::read_request(accepted_client->get_ctx(), url)) @@ -74,7 +74,7 @@ void client_thread(const transport::accepted_client *a break; } - unique_fd opened_file{}; + UniqueFd opened_file{}; const auto open_file_result = open_file(directory_fd, url.data(), opened_file); switch (open_file_result) { @@ -105,14 +105,14 @@ void client_thread(const transport::accepted_client *a { error::g_log << "Read file \"" << url.data() << "\""; }, - error::print{} + error::Print{} ); return; } const auto readed = static_cast(ret); if (readed) { - if (!transport::send(accepted_client->get_ctx(), span{buffer.data(), readed})) + if (!transport::send(accepted_client->get_ctx(), Span{buffer.data(), readed})) return; } if (readed < buffer.size()) @@ -122,26 +122,25 @@ void client_thread(const transport::accepted_client *a } -bool server_loop(int server_socket, not_nullctx, int directory_fd) +bool server_loop(int server_socket, NotNullserver_ctx, int directory_fd) { error::g_log << "🚀 Vostok server listening..." << std::endl; for (; ; ) { if (listen(server_socket, 1024) == -1) { - error::occurred("Listen socket", error::print{}); + error::occurred("Listen socket", error::Print{}); return false; } - std::unique_ptr accepted_client{ - new transport::accepted_client(server_socket, ctx) + std::unique_ptr accepted_client{ + new transport::AcceptedClient(server_socket, server_ctx) }; if (accepted_client->is_accepted()) { try { std::thread{client_thread, accepted_client.get(), directory_fd}.detach(); - accepted_client.release(); // move ownership to thread } catch (const std::system_error &e) { @@ -154,6 +153,7 @@ bool server_loop(int server_socket, not_null