-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataFrame.cpp
177 lines (155 loc) · 4.24 KB
/
DataFrame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "DataFrame.h"
DataFrameHeader::DataFrameHeader(byte buffer[], int nBufLen)
{
if(nBufLen<2)
{
return;
//throw new Exception("无效的数据头.");
}
//第一个字节
_fin = (buffer[0] & 0x80) == 0x80;
_rsv1 = (buffer[0] & 0x40) == 0x40;
_rsv2 = (buffer[0] & 0x20) == 0x20;
_rsv3 = (buffer[0] & 0x10) == 0x10;
_opcode = (sbyte)(buffer[0] & 0x0f);
//第二个字节
_maskcode = (buffer[1] & 0x80) == 0x80;
_payloadlength = (sbyte)(buffer[1] & 0x7f);
}
//发送封装数据
DataFrameHeader::DataFrameHeader(bool fin,bool rsv1,bool rsv2,bool rsv3,sbyte opcode,bool hasmask,int length)
{
_fin = fin;
_rsv1 = rsv1;
_rsv2 = rsv2;
_rsv3 = rsv3;
_opcode = opcode;
//第二个字节
_maskcode = hasmask;
_payloadlength = (sbyte)length;
}
//返回帧头字节
byte* DataFrameHeader::GetBytes()
{
byte* buffer = new byte[2];
memset(buffer, 0, 2);
if (_fin) buffer[0] ^= 0x80;
if (_rsv1) buffer[0] ^= 0x40;
if (_rsv2) buffer[0] ^= 0x20;
if (_rsv3) buffer[0] ^= 0x10;
buffer[0] ^= (byte)_opcode;
if (_maskcode) buffer[1] ^= 0x80;
buffer[1] ^= (byte)_payloadlength;
return buffer;
}
//DataFrame::DataFrame(byte* buffer, int nBufLen)
//{
// _extend = new byte[0];
// _mask = new byte[0];
// _content = new byte[0];
// //帧头
// _header = DataFrameHeader(buffer, nBufLen);
//
// //扩展长度
// if (nBufLen == 126)
// {
// _extend = new byte[2];
// Buffer.BlockCopy(buffer, 2, _extend, 0, 2);
// }
// else if (nBufLen == 127)
// {
// _extend = new byte[8];
// Buffer.BlockCopy(buffer, 2, _extend, 0, 8);
// }
//
// //是否有掩码
// if (_header.HasMask)
// {
// _mask = new byte[4];
// Buffer.BlockCopy(buffer, _extend.Length + 2, _mask, 0, 4);
// }
//
// //消息体
// if (_extend.Length == 0)
// {
// _content = new byte[_header.Length];
// Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2 , _content, 0, _content.Length);
// }
// else if (_extend.Length == 2)
// {
// int contentLength = (int)_extend[0] * 256 + (int)_extend[1];
// _content = new byte[contentLength];
// Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, contentLength > 1024 * 100 ? 1024 * 100 : contentLength);
// }
// else
// {
// long len = 0;
// int n = 1;
// for (int i = 7; i >= 0; i--)
// {
// len += (int)_extend[i] * n;
// n *= 256;
// }
// _content = new byte[len];
// Buffer.BlockCopy(buffer, _extend.Length + _mask.Length + 2, _content, 0, _content.Length);
// }
//
// if (_header.HasMask) _content = Mask(_content, _mask);
//
//}
DataFrame::DataFrame(char* content, int nContentSize)
{
_mask = new byte[0];
nMaskLen = 0;
_content = content;
int length = nContentSize;
nContentLen = nContentSize;
if (length < 126)
{
_extend = new byte[0];
nExtendLen = 0;
_header = DataFrameHeader(true, false, false, false, 1, false, length);
}
else if (length < 65536)
{
_extend = new byte[2];
nExtendLen = 2;
_header = DataFrameHeader(true, false, false, false, 1, false, 126);
_extend[0] = (byte)(length / 256);
_extend[1] = (byte)(length % 256);
}
else
{
_extend = new byte[8];
nExtendLen = 8;
_header = DataFrameHeader(true, false, false, false, 1, false, 127);
int left = length;
int unit = 256;
for (int i = 7; i > 1; i--)
{
_extend[i] = (byte)(left % unit);
left = left / unit;
if (left == 0)
break;
}
}
}
byte* DataFrame::GetBytes(int& nTotalSize)
{
nTotalSize = 2 + nExtendLen + nMaskLen + nContentLen;
byte* buffer = new byte[nTotalSize];
memset(buffer, 0, nTotalSize);
memcpy(buffer, _header.GetBytes(), 2);
memcpy(buffer + 2, _extend, nExtendLen);
memcpy(buffer + 2 + nExtendLen, _mask, nMaskLen);
memcpy(buffer + 2 + nExtendLen + nMaskLen, _content, nContentLen);
return buffer;
}
byte* DataFrame::Mask(byte* data, int nDataLen, byte* mask)
{
for (int i = 0; i < nDataLen; i++)
{
data[i] = (byte)(data[i] ^ mask[i % 4]);
}
return data;
}