需求:根据接口规范,实现与服务端的数据交互
服务端结构体分包头、包体、包尾
包头C++结构体示例如下
typedef struct head
{
BYTE string1;
BYTE string2; //包类型
BYTE string3; //版本号,目前为0
char string4[];
int string5;
int string6;
unsigned int string7; //包头校验和,以上所有字段的crc32校验和
char string8;
char string9;
}protocol_head;
包体C++结构体示例如下
typedef struct body
{
char sessid[];
int datalen;
BYTE data[];
};
包尾C++结构体示例如下
// 包尾 - 粘包分割
typedef struct tag_protocol_tail
{
BYTE tail[]
}protocol_tail;
根据包头结构体的要求,需要使用CRC32校验
unsigned int GetCRC32(const unsigned char *pbData, int nSize)
{
unsigned long Table[]={};
unsigned long ulPolynomial = 0xEDB88320; unsigned long dwCrc;
int i,j;
for(i = ; i < ; i++)
{
dwCrc = i;
for(j = ; j > ; j--)
{
if(dwCrc & )
dwCrc = (dwCrc >> ) ^ ulPolynomial;
else
dwCrc >>= ;
}
Table[i] = dwCrc;
} unsigned long dwCrc32 = 0xFFFFFFFF;
int idx=;
while(nSize--)
{
dwCrc32 = ((dwCrc32) >> ) ^ Table[(pbData[idx]) ^ ((dwCrc32) & 0x000000FF)];
idx++;
}
return ~dw
python 实现内容
struct中支持的格式如下表:
Format |
C Type |
Python |
字节数 |
x |
pad byte |
no value |
1 |
c |
char |
string of length 1 |
1 |
b |
signed char |
integer |
1 |
B |
unsigned char |
integer |
1 |
? |
_Bool |
bool |
1 |
h |
short |
integer |
2 |
H |
unsigned short |
integer |
2 |
i |
int |
integer |
4 |
I |
unsigned int |
integer or long |
4 |
l |
long |
integer |
4 |
L |
unsigned long |
long |
4 |
q |
long long |
long |
8 |
Q |
unsigned long long |
long |
8 |
f |
float |
float |
4 |
d |
double |
float |
8 |
s |
char[] |
string |
1 |
p |
char[] |
string |
1 |
P |
void * |
long |
举例C中常用:
int16=short --> h
uint32=unsigned int --> I
UInt64=unsigned long long --> Q
byte表示一个字节,对应C的unsigned char --> B
python中CRC32校验
def mycrc32(szString):
# 校验码
m_pdwCrc32Table = [0 for x in range(0, 256)]
dwPolynomial = 0xEDB88320
dwCrc = 0
for i in range(0, 255):
dwCrc = i
for j in [8, 7, 6, 5, 4, 3, 2, 1]:
if dwCrc & 1:
dwCrc = (dwCrc >> 1) ^ dwPolynomial
else:
dwCrc >>= 1
m_pdwCrc32Table[i] = dwCrc
dwCrc32 = 0xFFFFFFFFL
for i in szString:
b = ord(i)
dwCrc32 = ((dwCrc32) >> 8) ^ m_pdwCrc32Table[(b) ^ ((dwCrc32) & 0x000000FF)]
dwCrc32 = dwCrc32 ^ 0xFFFFFFFFL
return dwCrc32
C++和python关于CRC32代码传入参数如何理解?
根据上面C++代码内容,可以看到传入2位参数,它的第一位参数是整个包头+包体+包尾内容,第二位参数是整个包前多少位的长度需要校验
对于python代码,实际上只要传入需要校验的内容。
本文中需要传入的内容实际上是包头的的前6个字段,也就是包头的string1+string2+string3+string4+string5+string6
包头代码
# 包头, data为传入的包体,body_len是包体长度
def qzj_head(data, body_len):
string1 = 100
string2 = 1
string3 = 0
string4 = ""
string5 = body_len
string6 = body_len string1 = struct.pack('B', string1 )
string2 = struct.pack('B', string2 )
string3 = struct.pack('B', string3)
string4 = struct.pack('33s', string4 )
string5 = struct.pack('i', string5 )
string6 = struct.pack('i', string6 ) string7_struct = string1+string2+string3+string4+string5+string6 # 前面的6个字段内容
string7_crc32 = des_key.mycrc32(headcrc_str) # crc32校验
string7 = struct.pack('I', string7_crc32)
string8 = 0
string9 = 0
string8 = struct.pack('B', string8)
string9 = struct.pack('B', string9) request_head = string1+string2+string3+string4+string5+string6+string7+string8+string9+data
return request_head
包尾代码
# 包尾,data是传入的包头+包体
def qzj_tail(data):
tail1 = '\0'
tail2 = '\0'
tail3 = '\r'
tail4 = '\n'
tail1 = struct.pack('s', tail1)
tail2 = struct.pack('s', tail2)
tail3 = struct.pack('s', tail3)
tail4 = struct.pack('s', tail4)
request_tail = data+tail1+tail2+tail3+tail4
return request_tail
包体代码
# 包体, data是要发送的json数据
def body_100(data):
string1 = ''
string2 = len(data)
string3 = data string1 = struct.pack('33s', string1)
string2 = struct.pack('i', string2)
string3 = struct.pack('%ds' % datalen_num, string3)
request_body = string1+string2+string3
return request_body, len(request_body)