这对于数据的服务器(速度)转换是最优化的吗?
我可以更改它以获得更好的性能吗?
这在数据包解析器中用于设置/获取数据包数据。
void Packet::setChar(char val, unsigned int offset)
{
raw[offset + 8] = val;
}
short Packet::getChar(unsigned int offset)
{
return raw[offset + 8];
}
void Packet::setShort(short val, unsigned int offset)
{
raw[offset + 8] = val & 0xff;
raw[offset + 9] = (val >> 8) & 0xff;
}
short Packet::getShort(unsigned int offset)
{
return (short)((raw[offset + 9]&0xff) << 8) | (raw[offset + 8]&0xff);
}
void Packet::setInt(int val, unsigned int offset)
{
raw[offset + 8] = val & 0xff;
raw[offset + 9] = (val >> 8) & 0xff;
raw[offset + 10] = (val >> 16) & 0xff;
raw[offset + 11] = (val >> 24) & 0xff;
}
int Packet::getInt(unsigned int offset)
{
return (int)((raw[offset + 11]&0xff) << 24) | ((raw[offset + 10]&0xff) << 16) | ((raw[offset + 9]&0xff) << 8) | (raw[offset + 8]&0xff);
}
类定义:
class Packet
{
public:
Packet(unsigned int length);
Packet(char * raw);
///header
void setChar(char val, unsigned int offset);
short getChar(unsigned int offset);
void setShort(short val, unsigned int offset);
short getShort(unsigned int offset);
void setInt(int val, unsigned int offset);
int getInt(unsigned int offset);
void setLong(long long val, unsigned int offset);
long getLong(unsigned int offset);
char * getRaw();
~Packet();
protected:
private:
char * raw;
};
@EDIT 添加了类定义 Char raw 用数据包(新字符)初始化。