在发送数据之前压缩数据 - 控制字符? [英] Compressing data before sending it - controll characters?

查看:34
本文介绍了在发送数据之前压缩数据 - 控制字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多人游戏设置中,我打算在发送之前使用 zlib 压缩较大的字符串.我将结果数据放回字符串中,这些字符串将使用 TCP 作为字节流发送.

In a multiplayer game setting, I was going to use zlib to compress larger strings before sending them. I placed the resulting data back into strings, which are to be sent as byte streams using TCP.

我的问题是,我还需要将控制字符放入字符串中.例如,我需要将原始字符串长度(以纯文本形式)添加到压缩字符串的前面,并使用诸如|"之类的符号将其与压缩数据分开.

My problem is, that I need to place control characters into the string as well. For example, I need to add the original string length (in plain text) to the front of the compressed string, and seperate it from the compressed data using some symbol, like "|".

但我找不到一种方法来知道哪些字节是实际内容,哪些字节是控制字符.是否有 zlib 压缩字符串永远不会包含的任何字符(除了 0,我不能使用它,因为它标志着 c 字符串的结尾),我可以用它来分隔元数据"和压缩数据"?

But I can't find a way of knowing which bytes are actual content and which bytes are control characters. Are there any characters that a zlib-compressed-string will never contain (besides 0, which I can't use since it marks the end of a c-string) which I can use to seperate "metadata" and "compressed data"?

推荐答案

如果您同时控制压缩 blob 的创建和解压缩,您可以将大小预先添加到压缩数据中(可能通过在开头保留一些字节)缓冲区),然后在解压缩时跳过大小并将指向压缩数据的指针传递给解压缩实用程序.这样您就不必担心会因大小而弄乱您的压缩数据:解压代码永远不会看到携带大小信息的字节.

If you control both creation of compressed blobs and decompression of them, you can prepend the size to the compressed data (prrobably by reserving some bytes at the beginning of the buffer), and then when decompressing skip the size and pass the pointer to compressed data to the decompression utility. This way you don't have to worry about messing up your compressed data with the size: the decompression code never sees the bytes that carry the size information.

这篇关于在发送数据之前压缩数据 - 控制字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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