Transaction.cs 3.45 KB
Newer Older
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FObjBase
{
    class Transaction
    {
        #region 成员变量
        /// <summary>
        /// 客户端发起的 源ID
        /// </summary>
        public UInt32 SrcObjID;

        /// <summary>
        /// 通讯模块
        /// </summary>
        public IFConn Conn;

        /// <summary>
        /// 包总大小, -1 ,大小未知
        /// </summary>
        public int Size = -1;

        /// <summary>
        /// 数据游标:每次吐出大包后, 剩下数据的开始位置
        /// </summary>
        public int Position = 0;

        /// <summary>
        /// 交易号, 服务器分配的
        /// </summary>
        public UInt32 Magic;

        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Buf;

        /// <summary>
        /// callfunction 返回时, 异步回调函数
        /// </summary>
        public object AsyncDelegate;

        /// <summary>
        /// callfunction 返回时, 异步状态
        /// </summary>
        public object AsyncState;

        /// <summary>
        /// 动作号
        /// </summary>
        public UInt16 InfoId;

        /// <summary>
        /// 功能号
        /// </summary>
        public UInt16 FuncID;

        /// <summary>
        /// 大包 最大体积
        /// </summary>
        public const UInt16 PackSize = 0x4000;


        #endregion
        public Transaction()
        {
            //Size = 0;
            //Position = 0;
            //Magic = 0;
            //Buf = null;
        }

        byte[] GetBuf()
        {
            int len = Buf.Length - Position;
            if (len > PackSize)
                len = PackSize;
            byte[] buf = new byte[len];
            Array.Copy(Buf, Position, buf, 0, len);
            return buf;
        }

        public bool GetBigSize(out Pack_BigSize p)
        {
            p = new Pack_BigSize();
            p.infoid = InfoId;
            p.funcid = FuncID;
            p.size = Size;
            p.position = Position;
            p.buf = GetBuf();

            Position += p.buf.Length;
            if (Position == Size)
                return true;
            else

                return false;
        }
        public bool Reset()
        {
            Buf = null;
            Size = -1;
            Position = 0;
            if ((AsyncDelegate == null) && (AsyncState == null))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 把大包解析放入数据缓存, 全部接收完,返回 true
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool ToBuf(Pack_BigSize p)
        {
            if (Buf == null)
            {
                InfoId = p.infoid;
                FuncID = p.funcid;
                Size = p.size;
                Buf = new byte[Size];
                Array.Copy(p.buf, 0, Buf, p.position, p.buf.Length);
                Position = p.position + p.buf.Length;
                return false;
            }
            else
            {
                Array.Copy(p.buf, 0, Buf, p.position, p.buf.Length);
                Position = p.position + p.buf.Length;
                if (Position == Size)
                {
                    return true;
                }
                return false;
            }
        }
    }
}