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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FObjBase
{
/// <summary>
/// 协议包
/// </summary>
public class Pack_Proto : IPack
{
/// <summary>
/// 协议包长度
/// </summary>
private UInt16 plen;
/// <summary>
/// 目标obj id
/// </summary>
public UInt32 destid;
/// <summary>
/// 源obj id
/// </summary>
public UInt32 srcid;
/// <summary>
/// 交易号
/// </summary>
public UInt32 magic;
/// <summary>
/// 动作码
/// </summary>
public UInt16 info;
/// <summary>
/// 数据
/// </summary>
public byte[] buf;
/// <summary>
/// 转为 字节s
/// </summary>
/// <returns></returns>
public byte[] ToBytes()
{
List<byte> buf = new List<byte>();
plen = (UInt16)(2 + 4 * 3 + 2);
if(this.buf!=null)
plen = (UInt16)(plen + this.buf.Length);
buf.AddRange(BitConverter.GetBytes(plen));
buf.AddRange(BitConverter.GetBytes(destid));
buf.AddRange(BitConverter.GetBytes(srcid));
buf.AddRange(BitConverter.GetBytes(magic));
buf.AddRange(BitConverter.GetBytes(info));
if(this.buf!=null)
buf.AddRange(this.buf);
return buf.ToArray();
}
/// <summary>
/// 解析 字节s
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool TryParse(byte[] value)
{
if(value.Length<(2 + 4 * 3 + 2))
return false;
int index = 0;
plen = BitConverter.ToUInt16(value, index);
index += 2;
if(value.Length!=plen)
return false;
destid = BitConverter.ToUInt32(value, index);
index += 4;
srcid = BitConverter.ToUInt32(value, index);
index += 4;
magic = BitConverter.ToUInt32(value, index);
index += 4;
info = BitConverter.ToUInt16(value, index);
index += 2;
int len = value.Length - index;
if (len <= 0)
return true;
buf = new byte[len];
Array.Copy(value, index, buf, 0, len);
return true;
}
}
/// <summary>
/// 协议包的动作码
/// </summary>
public class Proto
{
#region client->server
/// <summary>
/// client->server;
/// setvalue
/// fobj 基本动作码 不能改!!!
/// </summary>
public const UInt16 INFO_SET_VALUE = 0x0002;
/// <summary>
/// client->server;
/// getvalue
/// fobj 基本动作码 不能改!!!
/// </summary>
public const UInt16 INFO_GET_VALUE = 0x0003;
/// <summary>
/// client->server;
/// callfunction
/// fobj 基本动作码 不能改!!!
/// </summary>
public const UInt16 INFO_CALL_FUNCTION = 0x0004;
/// <summary>
/// client->server;
/// config_sense
/// fobj 基本动作码 不能改!!!
/// </summary>
public const UInt16 INFO_CONFIG_SENSE_OBJ = 0x0005;
/// <summary>
/// client->server
/// obj 已经不在了, 释放资源
/// </summary>
public const UInt16 INFO_OBJ_DISPOSE = 0x0009;
#endregion
#region 大包
/// <summary>
/// push, set, get, call 数据包太大, 被分拆多次发送
/// 每次发送大包
/// </summary>
public const UInt16 INFO_PUSH_BIGSIZE = 0x0006;
/// <summary>
/// 请求继续发送大包
/// </summary>
public const UInt16 INFO_REQUEST_BIGSIZE = 0x0406;
#endregion
#region server->client
/// <summary>
/// server->client;
/// getvalue 返回
/// fobj 基本动作码 不能改!!!
/// </summary>
public const UInt16 INFO_PUSH_GET_VALUE = 0x0403;
/// <summary>
/// server->client;
/// callfunction 返回
/// fobj 基本动作码 不能改!!!
/// </summary>
public const UInt16 INFO_PUSH_CALL_FUNCTION = 0x0404;
/// <summary>
/// server->client;
/// pushinfo
/// fobj 基本动作码 不能改!!!
/// </summary>
public const UInt16 INFO_PUSH_EVENT=0x0410;
#endregion
}
/// <summary>
/// 大包
/// </summary>
public class Pack_BigSize : IPack
{
/// <summary>
/// 动作 Get, Set, Push, Call, 都允许大包模式
/// </summary>
public UInt16 infoid;
/// <summary>
/// 功能号
/// </summary>
public UInt16 funcid;
/// <summary>
/// 总数据量大小
/// </summary>
public int size;
/// <summary>
/// 当前数据量在总数据量的位置
/// </summary>
public int position;
/// <summary>
/// 当前数据量
/// </summary>
public byte[] buf;
/// <summary>
/// 转为 字节s
/// </summary>
/// <returns></returns>
public byte[] ToBytes()
{
List<byte> buf = new List<byte>();
buf.AddRange(BitConverter.GetBytes(infoid));
buf.AddRange(BitConverter.GetBytes(funcid));
buf.AddRange(BitConverter.GetBytes(size));
buf.AddRange(BitConverter.GetBytes(position));
buf.AddRange(BitConverter.GetBytes(this.buf.Length));
buf.AddRange(this.buf);
return buf.ToArray();
}
/// <summary>
/// 解析 字节s
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool TryParse(byte[] value)
{
int cnt = 2 + 2 + 4 + 4 + 4;
if (value.Length < cnt)
return false;
int idx = 0;
infoid = BitConverter.ToUInt16(value, idx);
idx += 2;
funcid = BitConverter.ToUInt16(value, idx);
idx += 2;
size = BitConverter.ToInt32(value, idx);
idx += 4;
position = BitConverter.ToInt32(value, idx);
idx += 4;
int len = BitConverter.ToInt32(value, idx);
idx += 4;
cnt += len;
if (value.Length < cnt)
return false;
buf = new byte[len];
Array.Copy(value, idx, buf, 0, buf.Length);
return true;
}
}
/// <summary>
/// Get Set Push CallFunction 数据包
/// </summary>
public class Pack_GetSetPushCall : IPack
{
/// <summary>
/// 功能号
/// </summary>
public UInt16 infoid;
/// <summary>
/// 数据
/// </summary>
public byte[] infodata = null;
/// <summary>
/// 转为 字节s
/// </summary>
/// <returns></returns>
public byte[] ToBytes()
{
List<byte> buf = new List<byte>();
buf.AddRange(BitConverter.GetBytes(infoid));
if (infodata != null)
{
buf.AddRange(infodata);
}
return buf.ToArray();
}
/// <summary>
/// 解析 字节s
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool TryParse(byte[] value)
{
if (value.Length < 2)
return false;
infoid = BitConverter.ToUInt16(value, 0);
int info_len = value.Length - 2;
if (info_len > 0)
{
infodata = new byte[info_len];
Buffer.BlockCopy(value, 2, infodata, 0, info_len);
}
else
infodata = null;
return true;
}
}
}