将对象传递给网络数据包处理程序?

发布于 2024-08-31 09:47:48 字数 384 浏览 7 评论 0原文

我已经用 C++ 编写网络服务器有一段时间了,现在已经开始寻找一种正确、轻松地处理所有数据包的方法。

到目前为止,我可以弄清楚它是什么类型的数据包,但现在我需要弄清楚如何将所需的数据获取到处理程序函数。

我想到了以下内容:

  • 有一个函数指针映射,其中操作码作为键,函数指针作为值
  • 所有这些函数都有 2 个参数,数据包和 ObjectAccessor
  • ObjectAccessor 类包含各种函数来获取各种项目,例如用户等
  • 也许也传递用户的 guid,以便我们可以从 objectaccessor 获取它。

我想知道其他人提出的各种实现,所以请评论这个想法并回复您自己的实现。

谢谢,Xeross

I've been writing a networking server for a while now in C++ and have come to the stage to start looking for a way to properly and easily handle all packets.

I am so far that I can figure out what kind of packet it is, but now I need to figure out how to get the needed data to the handler functions.

I had the following in mind:

  • Have a map of function pointers with the opcode as key and the function pointer as value
  • Have all these functions have 2 arguments, packet and ObjectAccessor
  • ObjectAccessor class contains various functions to fetch various items such as users and alike
  • Perhaps pass the user's guid too so we can fetch it from the objectaccessor

I'd like to know the various implementations others have come up with, so please comment on this idea and reply with your own implementations.

Thanks, Xeross

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可遇━不可求 2024-09-07 09:47:48

过去我创建了一条 XML 消息,并在消息中提供了一个命令字段。
由于我只有很少的命令,所以我只使用了一堆 if 语句来查看命令是什么,然后根据命令调用一个函数,传入 XML 的有效负载部分。

In the past I had created an XML message and in the Message provided a command field.
Since I only had few commands I just used a bunch of if statements to see what the command was and then called a function based on the command, passing in the payload part of the XML.

与君绝 2024-09-07 09:47:48

您可能需要看看一些现有的解决方案。 YAMI 是一个您可能想了解的简单消息系统。 (要么作为您当前拥有的替代品,要么了解如何注册/调用处理程序函数)

我还没有看过 YAMI4,所以我无法评论它,但 YAMI 3.x 很容易启动并快速运行,文档应该让您了解如何访问处理程序函数。

You might what to take a look at some existing solutions. YAMI is a simple messaging system that you might want to look at. (either as a replacement to what you currently have or to get an idea of how handler functions are registered/called)

I have not looked at YAMI4 yet so I can't comment on it, but YAMI 3.x quite easy to get up and running quickly and the documentation should give you an idea of how to access your handler functions.

小伙你站住 2024-09-07 09:47:48

我已经实现了一个与函数指针数组结合的处理程序类,工作得很好。

I have implemented a handler class in conjunction with an array of function pointers, works beautifully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文