面向对象的网络 [英] Object-oriented networking

查看:50
本文介绍了面向对象的网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了许多网络系统,并对网络的工作原理有一个很好的了解.但是,我总是最终拥有一个数据包接收功能,这是一个巨大的switch语句.这开始传给我.我本来是一个很好的优雅的面向对象的方式来处理接收数据包,但是每次我尝试提出一个好的解决方案时,我总是以失败告终.

I've written a number of networking systems and have a good idea of how networking works. However I always end up having a packet receive function which is a giant switch statement. This is beginning to get to me. I'd far rather a nice elegant object-oriented way to handle receiving packets but every time I try to come up with a good solution I always end up coming up short.

例如,假设您有一台网络服务器.它只是在那里等待响应.数据包进入,服务器需要验证数据包,然后需要决定如何处理它.

For example lets say you have a network server. It is simply waiting there for responses. A packet comes in and the server needs to validate the packet and then it needs to decide how to handle it.

目前,我通过打开报头中的数据包ID进行此操作,然后进行了大量的函数调用来处理每种数据包类型.对于复杂的网络系统,这将导致单片的switch语句,我真的不喜欢以这种方式处理它.我考虑过的一种方法是使用处理程序类的映射.然后,我可以将数据包传递给相关的类并处理传入的数据.我遇到的问题是,我需要某种方式来向映射注册"每个数据包处理程序.通常,这意味着我需要创建该类的静态副本,然后在构造函数中向中央数据包处理程序注册它.尽管此方法有效,但实际上似乎是一种轻巧而巧妙的处理方式.

At the moment I have been doing this by switching on the packet id in the header and then having a huge bunch of function calls that handle each packet type. With complicated networking systems this results in a monolithic switch statement and I really don't like handling it this way. One way I've considered is to use a map of handler classes. I can then pass the packet to the relevant class and handle the incoming data. The problem I have with this is that I need some way to "register" each packet handler with the map. This means, generally, I need to create a static copy of the class and then in the constructor register it with the central packet handler. While this works it really seems like an inelegant and fiddly way of handling it.

编辑:同样,拥有一个可以双向运行的好系统也是理想的.即,一种类结构,可以轻松地处理与接收相同数据包类型的发送(显然是通过不同的功能).

Edit: Equally it would be ideal to have a nice system that works both ways. ie a class structure that easily handles sending the same packet types as receiving them (through different functions obviously).

任何人都可以指出我一种更好的处理传入数据包的方法吗?链接和有用的信息非常感谢!

Can anyone point me towards a better way to handle incoming packets? Links and useful information are much appreciated!

很抱歉,我没有很好地描述问题,因为我无法很好地描述问题,这也是我从未设法提出解决方案的原因.

Apologies if I haven't described my problem well as my inability to describe it well is also the reason I've never managed to come up with a solution.

推荐答案

关于处理数据包类型的方法:对我而言,地图是最好的.但是,我将使用普通数组(或向量)而不是地图.如果从0开始顺序枚举数据包类型,它将使访问时间恒定.

About the way to handle the packet type: for me the map is the best. However I'd use a plain array (or a vector) instead of a map. It would make access time constant if you enumerate your packet types sequentially from 0.

关于类结构.已经有一些库可以完成此工作:可用游戏网络协议定义语言和代码生成.例如. Google的协议缓冲区似乎很有希望.它为协议描述中的每条消息生成一个带有getter,setter,序列化和反序列化例程的存储类.协议描述语言或多或少看起来很丰富.

As to the class structure. There are libraries that already do this job: Available Game network protocol definition languages and code generation. E.g. Google's Protocol Buffer seems to be promising. It generates a storage class with getters, setters, serialization and deserialization routines for every message in the protocol description. The protocol description language looks more or less rich.

这篇关于面向对象的网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆