From 9b3c8a80361de2c8bb87ca0a10da937dfe4df353 Mon Sep 17 00:00:00 2001 From: LIU Hao Date: Mon, 22 Jan 2024 09:45:13 +0800 Subject: [PATCH] third: Add forward declarations --- poseidon/fwd.hpp | 10 +++ poseidon/third/openssl_fwd.hpp | 8 +- poseidon/third/zlib_fwd.hpp | 137 +++++++++++---------------------- 3 files changed, 60 insertions(+), 95 deletions(-) diff --git a/poseidon/fwd.hpp b/poseidon/fwd.hpp index 5689d9b8..a356cac0 100644 --- a/poseidon/fwd.hpp +++ b/poseidon/fwd.hpp @@ -583,6 +583,16 @@ class Easy_WS_Client; class Easy_WSS_Server; class Easy_WSS_Client; +// Helper types for third-party libraries +class BIO_ptr; +class X509_ptr; +class SSL_CTX_ptr; +class SSL_ptr; +class zlib_Stream_base; +class deflate_Stream; +class inflate_Stream; +class MySQL_Client; + // Singletons extern atomic_relaxed exit_signal; extern class Main_Config& main_config; diff --git a/poseidon/third/openssl_fwd.hpp b/poseidon/third/openssl_fwd.hpp index 5f988291..6234ea9e 100644 --- a/poseidon/third/openssl_fwd.hpp +++ b/poseidon/third/openssl_fwd.hpp @@ -114,10 +114,10 @@ namespace poseidon { \ class OBJ##_ptr // no semicolon -POSEIDON_OPENSSL_FWD_DEFINE_PTR_(BIO); // class BIO_ptr -POSEIDON_OPENSSL_FWD_DEFINE_PTR_(X509); // class X509_ptr -POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL_CTX); // class SSL_CTX_ptr -POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL); // class SSL_ptr +POSEIDON_OPENSSL_FWD_DEFINE_PTR_(BIO); // class BIO_ptr; +POSEIDON_OPENSSL_FWD_DEFINE_PTR_(X509); // class X509_ptr; +POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL_CTX); // class SSL_CTX_ptr; +POSEIDON_OPENSSL_FWD_DEFINE_PTR_(SSL); // class SSL_ptr; } // namespace poseidon #endif diff --git a/poseidon/third/zlib_fwd.hpp b/poseidon/third/zlib_fwd.hpp index 8a453d0a..1e015c5f 100644 --- a/poseidon/third/zlib_fwd.hpp +++ b/poseidon/third/zlib_fwd.hpp @@ -10,53 +10,24 @@ #include namespace poseidon { -class deflate_Stream +class zlib_Stream_base { - private: - ::z_stream m_zstrm[1]; + protected: + mutable ::z_stream m_zstrm[1]; - public: - explicit - deflate_Stream(zlib_Format fmt, uint8_t wbits, int level) + protected: + zlib_Stream_base() noexcept { this->m_zstrm->zalloc = nullptr; this->m_zstrm->zfree = nullptr; this->m_zstrm->opaque = nullptr; - this->m_zstrm->next_in = (const ::Bytef*) ""; + this->m_zstrm->next_in = nullptr; this->m_zstrm->avail_in = 0; - - if((wbits < 9) || (wbits > 15)) - ::rocket::sprintf_and_throw<::std::invalid_argument>( - "deflate_Stream: window bits `%d` not valid", - wbits); - - int fmt_wbits; - if(fmt == zlib_deflate) - fmt_wbits = wbits; - else if(fmt == zlib_raw) - fmt_wbits = -wbits; - else if(fmt == zlib_gzip) - fmt_wbits = wbits + 16; - else - ::rocket::sprintf_and_throw<::std::invalid_argument>( - "deflate_Stream: format `%d` not valid", - fmt); - - if((level < -1) || (level > 9)) - ::rocket::sprintf_and_throw<::std::invalid_argument>( - "deflate_Stream: compression level `%d` not valid", - level); - - int err = ::deflateInit2(this->m_zstrm, level, Z_DEFLATED, fmt_wbits, 9, Z_DEFAULT_STRATEGY); - if(err != Z_OK) - this->throw_exception(err, "deflateInit2"); } - ASTERIA_NONCOPYABLE_DESTRUCTOR(deflate_Stream) - { - ::deflateEnd(this->m_zstrm); - } + ASTERIA_NONCOPYABLE_DESTRUCTOR(zlib_Stream_base) = default; + public: constexpr operator const ::z_stream*() const noexcept { return this->m_zstrm; } @@ -81,7 +52,7 @@ class deflate_Stream msg = "no error message"; ::rocket::sprintf_and_throw<::std::runtime_error>( - "deflate_Stream: zlib error: %s\n[`%s()` returned `%d`]", + "zlib error: %s\n[`%s()` returned `%d`]", msg, func, err); } @@ -102,24 +73,14 @@ class deflate_Stream } }; -class inflate_Stream +struct deflate_Stream : zlib_Stream_base { - private: - ::z_stream m_zstrm[1]; - - public: explicit - inflate_Stream(zlib_Format fmt, uint8_t wbits) + deflate_Stream(zlib_Format fmt, uint8_t wbits, int level) { - this->m_zstrm->zalloc = nullptr; - this->m_zstrm->zfree = nullptr; - this->m_zstrm->opaque = nullptr; - this->m_zstrm->next_in = (const ::Bytef*) ""; - this->m_zstrm->avail_in = 0; - if((wbits < 9) || (wbits > 15)) ::rocket::sprintf_and_throw<::std::invalid_argument>( - "inflate_Stream: window bits `%d` not valid", + "deflate_Stream: window bits `%d` not valid", wbits); int fmt_wbits; @@ -131,61 +92,55 @@ class inflate_Stream fmt_wbits = wbits + 16; else ::rocket::sprintf_and_throw<::std::invalid_argument>( - "inflate_Stream: format `%d` not valid", + "deflate_Stream: format `%d` not valid", fmt); - int err = ::inflateInit2(this->m_zstrm, fmt_wbits); + if((level < -1) || (level > 9)) + ::rocket::sprintf_and_throw<::std::invalid_argument>( + "deflate_Stream: compression level `%d` not valid", + level); + + int err = ::deflateInit2(this->m_zstrm, level, Z_DEFLATED, fmt_wbits, 9, Z_DEFAULT_STRATEGY); if(err != Z_OK) - this->throw_exception(err, "inflateInit2"); + this->throw_exception(err, "deflateInit2"); } - ASTERIA_NONCOPYABLE_DESTRUCTOR(inflate_Stream) + ASTERIA_NONCOPYABLE_DESTRUCTOR(deflate_Stream) { - ::inflateEnd(this->m_zstrm); + ::deflateEnd(this->m_zstrm); } + }; - constexpr operator - const ::z_stream*() const noexcept - { return this->m_zstrm; } - - operator - ::z_stream*() noexcept - { return this->m_zstrm; } - - [[noreturn]] - void - throw_exception(int err, const char* func) const +struct inflate_Stream : zlib_Stream_base + { + explicit + inflate_Stream(zlib_Format fmt, uint8_t wbits) { - const char* msg; + if((wbits < 9) || (wbits > 15)) + ::rocket::sprintf_and_throw<::std::invalid_argument>( + "inflate_Stream: window bits `%d` not valid", + wbits); - if(err == Z_VERSION_ERROR) - msg = "zlib library version mismatch"; - else if(err == Z_MEM_ERROR) - msg = "memory allocation failure"; - else if(this->m_zstrm->msg != nullptr) - msg = this->m_zstrm->msg; + int fmt_wbits; + if(fmt == zlib_deflate) + fmt_wbits = wbits; + else if(fmt == zlib_raw) + fmt_wbits = -wbits; + else if(fmt == zlib_gzip) + fmt_wbits = wbits + 16; else - msg = "no error message"; - - ::rocket::sprintf_and_throw<::std::runtime_error>( - "inflate_Stream: zlib error: %s\n[`%s()` returned `%d`]", - msg, func, err); - } + ::rocket::sprintf_and_throw<::std::invalid_argument>( + "inflate_Stream: format `%d` not valid", + fmt); - void - get_buffers(char*& ocur, const char*& icur) const noexcept - { - ocur = reinterpret_cast(this->m_zstrm->next_out); - icur = reinterpret_cast(this->m_zstrm->next_in); + int err = ::inflateInit2(this->m_zstrm, fmt_wbits); + if(err != Z_OK) + this->throw_exception(err, "inflateInit2"); } - void - set_buffers(char* ocur, char* oend, const char* icur, const char* iend) noexcept + ASTERIA_NONCOPYABLE_DESTRUCTOR(inflate_Stream) { - this->m_zstrm->next_out = reinterpret_cast<::Bytef*>(ocur); - this->m_zstrm->avail_out = ::rocket::clamp_cast<::uInt>(oend - ocur, 0, INT_MAX); - this->m_zstrm->next_in = reinterpret_cast(icur); - this->m_zstrm->avail_in = ::rocket::clamp_cast(iend - icur, 0, INT_MAX); + ::inflateEnd(this->m_zstrm); } };