diff options
| author | Dmytro Ivanov <[email protected]> | 2024-10-02 15:07:03 +0200 |
|---|---|---|
| committer | Dmytro Ivanov <[email protected]> | 2024-10-02 15:46:41 +0200 |
| commit | 634d01ac6993a63f31eadb26ac49128ae01f9860 (patch) | |
| tree | bd406b0957d3910d42f14f5ce3febcd68d192466 | |
| parent | add cmake build config to be able to use clion (diff) | |
| download | zen-di/plugin-name-wip.tar.xz zen-di/plugin-name-wip.zip | |
Add plugin name command line parameterdi/plugin-name-wip
Make it possible to load commandline provided plugins
| -rw-r--r-- | src/zenhttp/httpserver.cpp | 4 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpserver.h | 1 | ||||
| -rw-r--r-- | src/zenhttp/servers/httpplugin.cpp | 5 | ||||
| -rw-r--r-- | src/zenhttp/transports/dlltransport.cpp | 44 | ||||
| -rw-r--r-- | src/zenhttp/transports/dlltransport.h | 1 | ||||
| -rw-r--r-- | src/zenserver/config.cpp | 8 |
6 files changed, 62 insertions, 1 deletions
diff --git a/src/zenhttp/httpserver.cpp b/src/zenhttp/httpserver.cpp index a0d4ef3f3..8e12f3ec7 100644 --- a/src/zenhttp/httpserver.cpp +++ b/src/zenhttp/httpserver.cpp @@ -843,6 +843,10 @@ CreateHttpServerClass(HttpServerClass Class, const HttpServerConfig& Config) Server->AddPlugin(DllPlugin); # endif +# if 1 + DllPlugin->LoadDll(Config.PluginName); +#endif + return Server; } #endif diff --git a/src/zenhttp/include/zenhttp/httpserver.h b/src/zenhttp/include/zenhttp/httpserver.h index 7b87cb84b..d680f8057 100644 --- a/src/zenhttp/include/zenhttp/httpserver.h +++ b/src/zenhttp/include/zenhttp/httpserver.h @@ -190,6 +190,7 @@ struct HttpServerConfig std::string ServerClass; // Choice of HTTP server implementation bool ForceLoopback = false; unsigned int ThreadCount = 0; + std::string PluginName; struct { diff --git a/src/zenhttp/servers/httpplugin.cpp b/src/zenhttp/servers/httpplugin.cpp index 09cd76f3e..84cd71a48 100644 --- a/src/zenhttp/servers/httpplugin.cpp +++ b/src/zenhttp/servers/httpplugin.cpp @@ -300,7 +300,10 @@ HttpPluginConnectionHandler::OnBytesRead(const void* Buffer, size_t AvailableByt { ZEN_ASSERT(m_Server); - ZEN_LOG_TRACE(m_Server->m_RequestLog, "connection #{} OnBytesRead: {}", m_ConnectionId, AvailableBytes); + if (AvailableBytes > 0) + { + ZEN_LOG_TRACE(m_Server->m_RequestLog, "connection #{} OnBytesRead: {}", m_ConnectionId, AvailableBytes); + } while (AvailableBytes) { diff --git a/src/zenhttp/transports/dlltransport.cpp b/src/zenhttp/transports/dlltransport.cpp index e09e62ec5..91d27a49b 100644 --- a/src/zenhttp/transports/dlltransport.cpp +++ b/src/zenhttp/transports/dlltransport.cpp @@ -43,6 +43,7 @@ public: virtual bool IsAvailable() override; virtual void LoadDll(std::string_view Name) override; + virtual void LoadDll(std::string_view Name, std::string_view Path) override; virtual void ConfigureDll(std::string_view Name, const char* OptionTag, const char* OptionValue) override; private: @@ -181,6 +182,49 @@ DllTransportPluginImpl::LoadDll(std::string_view Name) m_Transports.emplace_back(std::move(NewDll)); } +void +DllTransportPluginImpl::LoadDll(std::string_view Name, std::string_view Path) +{ + RwLock::ExclusiveLockScope _(m_Lock); + + ExtendableStringBuilder<256> DllPath; + DllPath << Path; + if (!Path.ends_with('\\')) + { + DllPath << '\\'; + } + DllPath << Name << ".dll"; + + HMODULE DllHandle = LoadLibraryA(DllPath.c_str()); + + if (!DllHandle) + { + std::error_code Ec = MakeErrorCodeFromLastError(); + + throw std::system_error(Ec, fmt::format("failed to load transport DLL from '{}'", DllPath)); + } + + TransportPlugin* CreateTransportPlugin(); + + PfnCreateTransportPlugin CreatePlugin = (PfnCreateTransportPlugin)GetProcAddress(DllHandle, "CreateTransportPlugin"); + + if (!CreatePlugin) + { + std::error_code Ec = MakeErrorCodeFromLastError(); + + FreeLibrary(DllHandle); + + throw std::system_error(Ec, fmt::format("API mismatch detected in transport DLL loaded from '{}'", DllPath)); + } + + LoadedDll NewDll; + NewDll.Name = Name; + NewDll.LoadedFromPath = DllPath.c_str(); + NewDll.Plugin = CreatePlugin(); + + m_Transports.emplace_back(std::move(NewDll)); +} + DllTransportPlugin* CreateDllTransportPlugin() { diff --git a/src/zenhttp/transports/dlltransport.h b/src/zenhttp/transports/dlltransport.h index 9346a10ce..21985bac8 100644 --- a/src/zenhttp/transports/dlltransport.h +++ b/src/zenhttp/transports/dlltransport.h @@ -16,6 +16,7 @@ class DllTransportPlugin : public TransportPlugin { public: virtual void LoadDll(std::string_view Name) = 0; + virtual void LoadDll(std::string_view Name, std::string_view Path) = 0; virtual void ConfigureDll(std::string_view Name, const char* OptionTag, const char* OptionValue) = 0; }; diff --git a/src/zenserver/config.cpp b/src/zenserver/config.cpp index 7466255a9..e817458a1 100644 --- a/src/zenserver/config.cpp +++ b/src/zenserver/config.cpp @@ -568,6 +568,7 @@ void ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) { const char* DefaultHttp = "asio"; + const char* DefaultPlugin = ""; #if ZEN_WITH_HTTPSYS if (!zen::windows::IsRunningOnWine()) @@ -710,6 +711,13 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) options.add_option("network", "", + "plugin", + "Select http plugin", + cxxopts::value<std::string>(ServerOptions.HttpServerConfig.PluginName)->default_value(DefaultPlugin), + "<plugin name>"); + + options.add_option("network", + "", "http-threads", "Number of http server connection threads", cxxopts::value<unsigned int>(ServerOptions.HttpServerConfig.ThreadCount), |