Proto.cs 8.09 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FObjBase
{
8 9 10
    /// <summary>
    /// 协议包
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
11 12
    public class Pack_Proto : IPack 
    {
13 14 15
        /// <summary>
        /// 协议包长度
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
16
        private UInt16 plen;
17 18 19
        /// <summary>
        /// 目标obj id
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
20
        public UInt32 destid;
21 22 23
        /// <summary>
        /// 源obj id
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
24
        public UInt32 srcid;
25 26 27
        /// <summary>
        /// 交易号
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
28
        public UInt32 magic;
29 30 31
        /// <summary>
        /// 动作码
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
32
        public UInt16 info;
33 34 35
        /// <summary>
        /// 数据
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
36 37
        public byte[] buf;

38 39 40 41
        /// <summary>
        /// 转为 字节s
        /// </summary>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        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();
        }

61 62 63 64 65
        /// <summary>
        /// 解析 字节s 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
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
        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;
        }
    }
93 94 95
    /// <summary>
    /// 协议包的动作码
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
96 97 98 99 100
    public class Proto
    { 
        #region client->server
        /// <summary>
        /// client->server;
101 102
        /// setvalue 
        /// fobj 基本动作码 不能改!!!
潘栩锋's avatar
潘栩锋 committed
103 104 105 106
        /// </summary>
        public const  UInt16 INFO_SET_VALUE = 0x0002;
        /// <summary>
        /// client->server;
107 108
        /// getvalue 
        /// fobj 基本动作码 不能改!!!
潘栩锋's avatar
潘栩锋 committed
109 110 111 112
        /// </summary>
        public const  UInt16 INFO_GET_VALUE = 0x0003;
        /// <summary>
        /// client->server;
113 114
        /// callfunction 
        /// fobj 基本动作码 不能改!!!
潘栩锋's avatar
潘栩锋 committed
115 116 117 118
        /// </summary>
        public const  UInt16 INFO_CALL_FUNCTION = 0x0004;
        /// <summary>
        /// client->server;
119 120
        /// config_sense 
        /// fobj 基本动作码 不能改!!!
潘栩锋's avatar
潘栩锋 committed
121 122
        /// </summary>
        public const  UInt16 INFO_CONFIG_SENSE_OBJ = 0x0005;
123

潘栩锋's avatar
潘栩锋 committed
124 125
        /// <summary>
        /// client->server
126
        /// obj 已经不在了, 释放资源
潘栩锋's avatar
潘栩锋 committed
127
        /// </summary>
128 129 130
        public const UInt16 INFO_OBJ_DISPOSE = 0x0009;
        #endregion
        #region 大包
潘栩锋's avatar
潘栩锋 committed
131
        /// <summary>
132 133
        /// push, set, get, call 数据包太大, 被分拆多次发送
        /// 每次发送大包
潘栩锋's avatar
潘栩锋 committed
134
        /// </summary>
135
        public const UInt16 INFO_PUSH_BIGSIZE = 0x0006;
潘栩锋's avatar
潘栩锋 committed
136
        /// <summary>
137
        /// 请求继续发送大包
潘栩锋's avatar
潘栩锋 committed
138
        /// </summary>
139
        public const UInt16 INFO_REQUEST_BIGSIZE = 0x0406;
潘栩锋's avatar
潘栩锋 committed
140 141 142 143 144
        #endregion

        #region server->client
        /// <summary>
        /// server->client;
145 146
        /// getvalue 返回 
        /// fobj 基本动作码 不能改!!!
潘栩锋's avatar
潘栩锋 committed
147 148 149 150
        /// </summary>
        public const UInt16 INFO_PUSH_GET_VALUE = 0x0403;
        /// <summary>
        /// server->client;
151 152
        /// callfunction 返回 
        /// fobj 基本动作码 不能改!!!
潘栩锋's avatar
潘栩锋 committed
153 154 155 156
        /// </summary>
        public const UInt16 INFO_PUSH_CALL_FUNCTION = 0x0404;
        /// <summary>
        /// server->client;
157 158
        /// pushinfo 
        /// fobj 基本动作码 不能改!!!
潘栩锋's avatar
潘栩锋 committed
159 160
        /// </summary>
        public const UInt16 INFO_PUSH_EVENT=0x0410;
161

潘栩锋's avatar
潘栩锋 committed
162 163
        #endregion
    }
164 165 166
    /// <summary>
    /// 大包
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
167 168
    public class Pack_BigSize : IPack 
    {
169 170 171 172 173 174 175
        /// <summary>
        /// 动作 Get, Set, Push, Call, 都允许大包模式
        /// </summary>
        public UInt16 infoid;
        /// <summary>
        /// 功能号
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
176
        public UInt16 funcid;
177 178 179
        /// <summary>
        /// 总数据量大小
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
180
        public int size;
181 182 183
        /// <summary>
        /// 当前数据量在总数据量的位置
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
184
        public int position;
185 186 187
        /// <summary>
        /// 当前数据量
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
188
        public byte[] buf;
189 190 191 192
        /// <summary>
        /// 转为 字节s
        /// </summary>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
193 194 195
        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
196
            buf.AddRange(BitConverter.GetBytes(infoid));
潘栩锋's avatar
潘栩锋 committed
197 198 199 200 201 202 203
            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();
        }
204 205 206 207 208
        /// <summary>
        /// 解析 字节s 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
209 210
        public bool TryParse(byte[] value)
        {
211
            int cnt = 2 + 2 + 4 + 4 + 4;
潘栩锋's avatar
潘栩锋 committed
212 213 214
            if (value.Length < cnt)
                return false;
            int idx = 0;
215 216 217

            infoid = BitConverter.ToUInt16(value, idx);
            idx += 2;
潘栩锋's avatar
潘栩锋 committed
218 219
            funcid = BitConverter.ToUInt16(value, idx);
            idx += 2;
220

潘栩锋's avatar
潘栩锋 committed
221 222 223 224 225 226
            size = BitConverter.ToInt32(value, idx);
            idx += 4;
            position = BitConverter.ToInt32(value, idx);
            idx += 4;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
227

潘栩锋's avatar
潘栩锋 committed
228 229 230 231 232 233 234 235
            cnt += len;
            if (value.Length < cnt)
                return false;
            buf = new byte[len];
            Array.Copy(value, idx, buf, 0, buf.Length);
            return true;
        }
    }
236 237 238 239

    /// <summary>
    /// Get Set Push CallFunction 数据包
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
240 241
    public class Pack_GetSetPushCall : IPack 
    {
242 243 244
        /// <summary>
        /// 功能号
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
245
        public UInt16 infoid;
246 247 248
        /// <summary>
        /// 数据
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
249 250
        public byte[] infodata = null;

251 252 253 254
        /// <summary>
        /// 转为 字节s
        /// </summary>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
255 256 257 258 259 260 261 262 263 264 265
        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            buf.AddRange(BitConverter.GetBytes(infoid));
            if (infodata != null)
            {
                buf.AddRange(infodata);
            }
            return buf.ToArray();
        }

266 267 268 269 270
        /// <summary>
        /// 解析 字节s 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        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;
        }
    }
}