#pragma once #include "IPTable.h" #include #include "FileReceiver.h" #include "ServerLogger.h" #include "ServerProcessor.h" #include "SyncFileReceiver.h" #include "HTTPObjectsBridge.h" // 确保能看到 Request::Impl 结构体 #include "UNSResponseBuilder.h" namespace uns { class IBlockedIpUpdatable { public: virtual ~IBlockedIpUpdatable() = default; // 统一的库内更新接口 virtual void ApplyIpUpdate(IPTablePtr blocked_ips) = 0; }; // 隐藏在动态库内部的适配器,对外部不可见 class ServerProcessorAdapter : public webcc::View, public IBlockedIpUpdatable { private: std::shared_ptr user_processor; public: explicit ServerProcessorAdapter(std::shared_ptr processor) : user_processor(processor) { } // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 webcc::ResponsePtr Handle(webcc::RequestPtr request) override final { try { auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); // 调用用户的业务类 uns::ResponsePtr uns_res = user_processor->Handle(uns_req); return uns_res->GetImpl()->webcc_res; } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(Handle): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(Handle)"); } return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500 } bool Stream(const std::string& method) override final { try { return user_processor->Stream(method); } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(Stream): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(Stream)"); } return false; //Default false } bool ValidateHeader(webcc::RequestPtr request) override final { try { auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); return user_processor->IsHeaderValid(uns_req); } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(ValidateHeader): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(ValidateHeader)"); } return true; //Default true } void ApplyIpUpdate(IPTablePtr blocked_ips) override final { try { user_processor->UpdateBlockedIPList(blocked_ips); } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(ApplyIpUpdate): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(ApplyIpUpdate)"); } } }; class FileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable { private: std::shared_ptr<::FileReceiver> user_reciver; // 持有用户的派生类对象 public: explicit FileReceiverAdapter(std::shared_ptr reciver) : user_reciver(reciver) { } // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 webcc::ResponsePtr Handle(webcc::RequestPtr request) override final { try { auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); // 调用用户的业务类 uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); return uns_res->GetImpl()->webcc_res; } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in FileReceiverAdapter(Handle): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in FileReceiverAdapter(Handle)"); } return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500 } bool Stream(const std::string& method) override final { // 所有数据都不能由webcc进行串流,否则将无法从request中获取文件 return false; } bool ValidateHeader(webcc::RequestPtr request) override final { try { auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); return user_reciver->IsHeaderValid(uns_req); } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in FileReceiverAdapter(ValidateHeader): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in FileReceiverAdapter(ValidateHeader)"); } return true; //Default true } void ApplyIpUpdate(IPTablePtr blocked_ips) override final { try { user_reciver->UpdateBlockedIPs(blocked_ips); } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in FileReceiverAdapter(ApplyIpUpdate): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in FileReceiverAdapter(ApplyIpUpdate)"); } } }; class SyncFileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable { private: std::shared_ptr user_reciver; // 持有用户的派生类对象 public: explicit SyncFileReceiverAdapter(std::shared_ptr reciver) : user_reciver(reciver) { } // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 webcc::ResponsePtr Handle(webcc::RequestPtr request) override final { try { auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); // 调用用户的业务类 uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); return uns_res->GetImpl()->webcc_res; } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in SyncFileReceiverAdapter(Handle): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in SyncFileReceiverAdapter(Handle)"); } return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500 } bool Stream(const std::string& method) override final { // 所有数据都不能由webcc进行串流,否则将无法从request中获取文件 return false; } bool ValidateHeader(webcc::RequestPtr request) override final { try { auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); return user_reciver->IsHeaderValid(uns_req); } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in SyncFileReceiverAdapter(ValidateHeader): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in SyncFileReceiverAdapter(ValidateHeader)"); } return true; //Default true } void ApplyIpUpdate(IPTablePtr blocked_ips) override final { try { user_reciver->UpdateBlockedIPs(blocked_ips); } catch (const std::exception& e) { SCLOGF_ERROR("Unhandled Exception in SyncFileReceiverAdapter(ApplyIpUpdate): {}", e.what()); } catch (...) { SCLOGF_FATAL("Unhandled Unknown Exception in SyncFileReceiverAdapter(ApplyIpUpdate)"); } } }; }