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;
            }
        }
    }
}