在 c++ 中动态添加过滤功能
我有以下(简化的)架构:
客户端 -->保镖-->服务器
客户端向服务器发送命令。 “保镖”对客户端发出的命令执行健全性和其他检查,并防止错误命令到达服务器。例如,bouncer可能有如下代码:
bool Bouncer::someCommand(const someCommandArg& arg) {
if (arg.x < 100) {
return false;
}
if (arg.y > 10) {
return false;
}
// more checks ...
return server->someCommand(arg);
}
这种方式的问题是bouncer条件必须一一硬编码,不够灵活。我正在寻找一种在某些配置文件中定义这些条件的方法,保镖将在创建时加载该配置文件,并将循环遍历所有配置文件 调用 someCommand
时的条件。此外,测试循环本身必须很快。
如果是 C#,我想我会使用它的即时编译功能,并在配置文件中用纯代码编写我的 if
子句。您对 C++ 有什么建议?
I have the following (simplified) architecture:
client(s) --> bouncer --> server
The clients send commands to the server. The 'bouncer' performs sanity and other checks on the commands issued by the client, and prevents faulty commands from reaching the server. For example, the bouncer may have the following code:
bool Bouncer::someCommand(const someCommandArg& arg) {
if (arg.x < 100) {
return false;
}
if (arg.y > 10) {
return false;
}
// more checks ...
return server->someCommand(arg);
}
The problem with this approach is that the bouncer conditions have to be hard-coded one by one, and is not flexible enough. I'm looking for a way to define these conditions in some configuration file, which the bouncer will load when created, and will loop through all the
conditions when someCommand
is called. Moreover, the test loop itself has to be fast.
Were it C#, I guess I would have used its compile-on-the-fly capabilities, and have my if
clauses written in plain code in the configuration file. What do you suggest for C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择包括:
什么是最好的,很大程度上取决于有用谓词的复杂性、性能要求、部署实践等..
Choices include:
What's best depends a lot on the complexity of useful predicates, performance requirements, deployment practices etc..
您可以使用插件方法。让保镖成为一个插件,然后使用管道/责任链模式通过多个保镖传递命令?不同的保镖插件可以检查不同的条件,并通过链接它们,您可以定义应用于命令的最终规则。您可以使用配置文件来指定应加载哪些插件以及如何设置链。
这仍然意味着必须先编译插件,然后才能加载它们,但如果确实有要求,您可以在程序运行时动态加载它们。至少添加新插件不需要重新编译您的应用程序。
You can use a plugin approach. Make the bouncer a plugin and then use a pipeline/chain-of-responsibility pattern to pass a command through multiple bouncers? Different bouncer plugins can check for different conditions and by chaining them, you define the final rule applied to the command. You can use a config file to specify what plugins should be loaded and how to setup the chain.
This still implies that the plugins have to be compiled before you can load them, but you can dynamically load them while your program is running if this is really a requirement. At least adding new plugins doesn't require your application to be recompiled.