objective-c - 构造位掩码 ?按位数据包

标签 objective-c c bit-manipulation bit-shift

我一直想尝试这个项目Axon与通过 TCP 连接进行连接的 iOS 应用程序。在文档末尾,协议(protocol)的解释如下

The wire protocol is simple and very much zeromq-like, where is a BE 24 bit unsigned integer representing a maximum length of roughly ~16mb. The data byte is currently only used to store the codec, for example "json" is simply 1, in turn JSON messages received on the client end will then be automatically decoded for you by selecting this same codec.

附图

 octet:     0      1      2      3      <length>
         +------+------+------+------+------------------...
         | meta | <length>           | data ...
         +------+------+------+------+------------------...

我有使用二进制协议(protocol)创建数据包的经验,例如:

NSUInteger INT_32_LENGTH = sizeof(uint32_t);

uint32_t length = [data length]; // data is an NSData object

NSMutableData *packetData = [NSMutableData dataWithCapacity:length + (INT_32_LENGTH * 2)];
[packetData appendBytes:&requestType length:INT_32_LENGTH]; 
[packetData appendBytes:&length length:INT_32_LENGTH];                  
[packetData appendData:data];                                           

所以我的问题是如何为 Axon 请求创建数据包,我会假设一些位移,我对此不太了解。

最佳答案

分配 1 个 char 或 unsigned char 数组,其大小 == packet_size; 贴标常量:

const int metaFieldPos = 0;
const int sizeofMetaField = sizeof(char);
const int lengthPos = metaFieldPos + sizeofMetaField;
const int sizeofLengthField = sizeof(char) * 3;
const int dataPos = lengthPos + sizeofLengthField;

如果你得到了数据并且可以识别数据包的开头,你可以使用上面的常量来 通过指针导航。

也许这些函数会对您有所帮助(它们使用 Qt,但您可以轻松地将它们转换为您使用的库)

quint32 Convert::uint32_to_uint24(const quint32 value){
    return value & (quint32)(0x00FFFFFFu);
}

qint32 Convert::int32_to_uint24(const qint32 value){
    return value & (qint32)(0x00FFFFFF);
}

quint32 Convert::bytes_to_uint24(const char* from){
    quint32 result = 0;
    quint8 shift = 0;
    for (int i = 0; i < bytesIn24Bits; i++) {
        result |= static_cast<quint32>(*reinterpret_cast<const quint8 *>(from + i)) << shift;
        shift+=bitsInByte;
    }
    return result;
}

void Convert::uint32_to_uint24Bytes(const quint32 value, char* from){
    quint8 shift = 0;
    for (int i = 0; i < bytesIn24Bits; i++) {
        const quint32 buf = (value >> shift) & 0xFFu;
        *(from + i) = *reinterpret_cast<const char *>(&buf);
        shift+=bitsInByte;
    }
}

QByteArray Convert::uint32_to_uint24QByteArray (const quint32 value){
    QByteArray bytes;
    bytes.resize(sizeof(value));
    *reinterpret_cast<quint32 *>(bytes.data()) = value;
    bytes.chop(1);
    return bytes;
}

关于objective-c - 构造位掩码 ?按位数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19586453/

相关文章:

c - 将floats打包成long long

c - 用于创建循环日志文件的开源解决方案?

c - 在 “encapsulation” 的上下文中,短语 “protecting objects from unwanted accesses by a client” 是什么意思

c++ - 在 C/C++ 中,左移或右移零实际上会生成一条指令吗?

iphone - 在 NSPropertyListSerialization 中处理 CFNull 对象

c - 3.3 书 k&r 中的上下文问题

MySQL 按位与 256 位二进制值

ios - 如何将 UIView 对象添加到 ios7 中的 self.view.window?

iphone - 从 NSMutableArray 中删除对象时崩溃

iOS 保存文件到非私有(private)目录