有人可以更好地解释解码器/编码器吗? [英] Can someone better explain Decoders/Encoders?

查看:86
本文介绍了有人可以更好地解释解码器/编码器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修订后的问题:

好,所以我试图将其合并到我自己的定制游戏中.我了解Netty服务器和客户端之间如何连接的过程.我也了解解码器和编码器在理论上是如何工作的.但是,这就是我仍然想了解的内容.

Ok so I am trying to incorporate this in my own custom made game. I understand the process of how Netty Servers and Clients connect. I also understand how the decoders and encoders work in theory. But here's what I still would like to understand.

我的服务器进程:

Server boots up -> Client starts
Client requests connection -> Server accepts
Server instructs client connection is good -> Client continues to the login screen
(Ignoring any type of security protocol)
Client sends username and password over Channel
Server gets username and password checks it in the database or file
Server pushes -> yes or no
if yes Server sends player stats
if no Server creates new player

在此过程之后,我知道我需要一个世界处理程序,以便每个人都可以近乎实时地看到更新.现在我不知道如何为这些东西实现解码器.

After that process I know I need to have a world handler so that everyone is seeing updates in near real time. Now I don't know how to implement decoders for this stuff.

我真的很想看看一些示例,其中对它们的实现方式进行了一些解释. 最好有一些说明... 注意:我并不是说要为我解决此问题,而是要向我展示如何处理不同的信息.请遵循最佳做法和标准....

I really would like to see some examples with some explanations of how they are implemented. with preferably some instructions.... Note: I am not saying solve this issue for me but show me how to handle the different information. Best practices and standards please....

推荐答案

人们写自己的编码器/解码器(编解码器),因为Netty不会强加或定义应用程序级协议,因此您可以自由地编写自己的协议.您定义的编解码器集是一种协议,可以在基于String的格式和Protobuf等二进制格式之间的任何值. Netty为您提供方便的编解码器(您使用的是示例).

People write their own encoders/decoders (codecs) because Netty doesn't impose nor define an application level protocol, so that you are free to write your own protocol. The set of codecs you define is a protocol that can be anything between String based and some binary format as Protobuf, for example. Netty provides codecs for your convenience (the ones you used are examples).

我认为这是为了防止流被提早中断?

I would assume this is to keep the stream from being cutoff early?

通常,在发送/接收流时,需要将其分解为固定长度的块(称为帧).自Internet诞生以来一直使用的一种流行方法是使用块的长度(通常为4字节int)作为从流中读取的第一个字段.因此,如果第一个int的值为20,则您知道接下来的20个字节是有效负载(数据),而第21个字节是另一个长度的第一个字节.这种方法的优点是它允许可变长度的块.但是,您不仅限于此.例如,如果您打算编写使用具有预定义长度的字符串(带有填充)的协议,那么您将编写,或者甚至最好使用适合于该协议的Netty当前编解码器.

Usually, when you are sending/receiving streams, you need to decompose that on a fixed length chunks (called frames). A popular approach, that has been used since the dawn of the Internet, is to use the length of the chunk (usually a 4 bytes int) as the first field read from the stream. So if the value of the first int is 20 then you know that the following 20 bytes are the payload (the data), and the 21th byte is the first byte of another length. The advantage of this approach is that it allows chunks of variable length. But you are not limited to this. For example, if you plan to write a protocol that uses Strings with predefined length (with padding) then you'll write or, even better, use Netty current codecs appropriate to it.

一次,我实现了带有三个解码器的协议,它们将按以下顺序执行:

Once, I implemented a protocol with three decoders that would perform in this order:

  1. 接收流并将其分解为带有长度前缀的帧;
  2. 将每个帧转换为字符串;
  3. 使用Jackson图书馆将字符串转换为预定义的Java对象.

编码器将执行相同的操作,但会反向执行.不幸的是,我已经丢失了原始代码,但是我会尽快对其进行重写.

The encoders would just do the same operations, but backwards. Unfortunately, I've lost the original code, but I will rewrite it soon.

但是流如何知道该流是String还是一系列int或一系列double?您如何分辨问题所在?

But how does the stream know that the stream is a String or a series of int's or a series of doubles? How do you tell it the difference is the question?

简短的回答:不知道.您必须在编解码器中对此信息进行编码.例如,您可以将操作码用作有效负载中的第一个字段,该字段表示有效负载是字符串,双精度数,整数或两者的任意组合.

Short answer: it doesn't know. You have to encode this info in the codecs. For example, you can use a opcode as the first field in the payload that says that payload are Strings, doubles, ints or any combination of both.

基本上,Netty提供了一个流,您可以随意解码.例如,如果您正在读取一系列long(8个字节),那么您将要编写一个编解码器,该编解码器一次从流中读取64个字节,因为每个编解码器都代表一个long. Netty提供了开箱即用的编解码器,因此您无需每次都重新发明轮子.

Basically, Netty provides a stream and you are free to decode as you like. For example, if you are reading a series of longs (8 bytes) then you are going to write a codec that reads 64 bytes at a time from the stream because each one represents a single long. Netty provides codecs out-of-box so that you don't need to reinvent the wheel every time.

这篇关于有人可以更好地解释解码器/编码器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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