Exemplo n.º 1
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="rcvBytes"></param>
        /// <param name="rspBytes"></param>
        /// <returns></returns>
        private bool FileUpload(byte[] rcvBytes, out byte[] rspBytes)
        {
            rspBytes = null;
            byte childcode = rcvBytes[2];
            byte datalen   = rcvBytes[3];
            byte filetype  = rcvBytes[4];

            //判断长度
            if (rcvBytes.Length != datalen + 6)
            {
                return(false);
            }
            //启动加载
            if (childcode == 0x05)
            {
                if (!mFiles.ContainsKey(filetype))
                {
                    rspBytes    = new byte[3];
                    rspBytes[0] = rcvBytes[0];
                    rspBytes[1] = 0xC1;
                    rspBytes[2] = 0x08;
                    return(true);
                }
                FileData file    = mFiles[filetype];
                int      filelen = file.Data.Length;
                //返回数据
                rspBytes    = new byte[10];
                rspBytes[0] = rcvBytes[0];
                rspBytes[1] = 0x41;
                rspBytes[2] = 0x05;
                rspBytes[3] = 0x06;
                rspBytes[4] = rcvBytes[4];
                rspBytes[5] = (byte)(filelen >> 24);
                rspBytes[6] = (byte)(filelen >> 16);
                rspBytes[7] = (byte)(filelen >> 8);
                rspBytes[8] = (byte)(filelen % 256);
                rspBytes[9] = (byte)(file.uFrameLen);
                mUState     = FileUpState.Started;
                return(true);
            }
            //状态循环
            switch (mUState)
            {
            case FileUpState.Started:
                if (childcode == 0x06)
                {
                    if (!mFiles.ContainsKey(filetype))
                    {
                        return(false);
                    }
                    if (datalen != 3)
                    {
                        return(false);
                    }
                    mFrameId = rcvBytes[5] * 256 + rcvBytes[6];
                    if (mFrameId != 0)
                    {
                        return(false);
                    }
                    //是否只有一帧
                    FileData file = mFiles[filetype];
                    int      len;
                    if (file.uFrameLen >= file.Data.Length)
                    {
                        len     = file.Data.Length;
                        mUState = FileUpState.Loaded;
                    }
                    else
                    {
                        len     = file.uFrameLen;
                        mUState = FileUpState.Loading;
                    }
                    //返回数据
                    rspBytes    = new byte[7 + len];
                    rspBytes[0] = rcvBytes[0];
                    rspBytes[1] = rcvBytes[1];
                    rspBytes[2] = rcvBytes[2];
                    rspBytes[3] = (byte)(3 + len);
                    rspBytes[4] = rcvBytes[4];
                    rspBytes[5] = rcvBytes[5];
                    rspBytes[6] = rcvBytes[6];
                    for (int j = 0; j < len; j++)
                    {
                        rspBytes[7 + j] = file.Data[j];
                    }
                    return(true);
                }
                break;

            case FileUpState.Loading:
                if (childcode == 0x06)
                {
                    if (!mFiles.ContainsKey(filetype))
                    {
                        return(false);
                    }
                    if (datalen != 3)
                    {
                        return(false);
                    }
                    mFrameId = rcvBytes[5] * 256 + rcvBytes[6];
                    if (mFrameId < 1)
                    {
                        return(false);
                    }
                    //是否只有一帧
                    FileData file = mFiles[filetype];
                    int      len;
                    int      gettedlen = file.uFrameLen * mFrameId;
                    if (file.uFrameLen >= (file.Data.Length - gettedlen))
                    {
                        len     = file.Data.Length - gettedlen;
                        mUState = FileUpState.Loaded;
                    }
                    else
                    {
                        len     = file.uFrameLen;
                        mUState = FileUpState.Loading;
                    }
                    //返回数据
                    rspBytes    = new byte[7 + len];
                    rspBytes[0] = rcvBytes[0];
                    rspBytes[1] = rcvBytes[1];
                    rspBytes[2] = rcvBytes[2];
                    rspBytes[3] = (byte)(3 + len);
                    rspBytes[4] = rcvBytes[4];
                    rspBytes[5] = rcvBytes[5];
                    rspBytes[6] = rcvBytes[6];
                    for (int j = 0; j < len; j++)
                    {
                        rspBytes[7 + j] = file.Data[j + gettedlen];
                    }
                    return(true);
                }
                break;

            case FileUpState.Loaded:
                if (childcode == 0x0c)
                {
                    if (datalen != 1)
                    {
                        return(false);
                    }
                    if (!mFiles.ContainsKey(filetype))
                    {
                        return(false);
                    }
                    FileData file  = mFiles[filetype];
                    byte[]   crc16 = CRC.CRC16(file.Data);
                    //返回数据
                    rspBytes    = new byte[7];
                    rspBytes[0] = rcvBytes[0];
                    rspBytes[1] = rcvBytes[1];
                    rspBytes[2] = rcvBytes[2];
                    rspBytes[3] = 0x03;
                    rspBytes[4] = rcvBytes[4];
                    rspBytes[5] = crc16[1];
                    rspBytes[6] = crc16[0];
                    mUState     = FileUpState.Finish;
                    return(true);
                }
                break;

            default:
                break;
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 文件加载
        /// </summary>
        /// <param name="rcvBytes"></param>
        /// <param name="rspBytes"></param>
        /// <returns></returns>
        private bool FileDownload(byte[] rcvBytes, out byte[] rspBytes)
        {
            rspBytes = null;
            byte childcode = rcvBytes[2];
            byte datalen   = rcvBytes[3];
            byte filetype  = rcvBytes[4];

            //判断长度
            if (rcvBytes.Length != datalen + 6)
            {
                return(false);
            }
            //启动加载
            if (childcode == 0x01)
            {
                if (datalen != 6)
                {
                    return(false);
                }
                int      filelen = rcvBytes[6] * 65536 + rcvBytes[7] * 256 + rcvBytes[8];
                FileData file    = new FileData(filetype, filelen);
                file.dFrameLen = (rcvBytes[9] > 224)? 224: rcvBytes[9];
                if (mFiles.ContainsKey(filetype))
                {
                    mFiles[filetype] = file;
                }
                else
                {
                    mFiles.Add(filetype, file);
                }
                //返回数据
                rspBytes = new byte[10];
                for (int i = 0; i < 9; i++)
                {
                    rspBytes[i] = rcvBytes[i];
                }
                rspBytes[9] = (byte)(file.dFrameLen);
                mDState     = FileDownState.Started;
                return(true);
            }
            //状态循环
            switch (mDState)
            {
            case FileDownState.Started:
                if (childcode == 0x02)
                {
                    if (!mFiles.ContainsKey(filetype))
                    {
                        return(false);
                    }
                    if (datalen < 4)
                    {
                        return(false);
                    }
                    if ((rcvBytes[5] != 0) || (rcvBytes[6] != 0))
                    {
                        return(false);
                    }
                    //长度是否为多帧
                    int      framelen = rcvBytes[7];
                    FileData file     = mFiles[filetype];
                    int      len;
                    if (framelen >= file.Data.Length)
                    {
                        len     = file.Data.Length;
                        mDState = FileDownState.Loaded;
                    }
                    else
                    {
                        len     = framelen;
                        mDState = FileDownState.Loading;
                    }
                    //赋值给文件
                    for (int j = 0; j < len; j++)
                    {
                        file.Data[j] = rcvBytes[8 + j];
                    }
                    //返回数据
                    rspBytes = new byte[8];
                    for (int i = 0; i < 7; i++)
                    {
                        rspBytes[i] = rcvBytes[i];
                    }
                    rspBytes[3] = 4;
                    rspBytes[7] = (byte)(len);
                    return(true);
                }
                break;

            case FileDownState.Loading:
                if (childcode == 0x02)
                {
                    if (!mFiles.ContainsKey(filetype))
                    {
                        return(false);
                    }
                    if (datalen < 4)
                    {
                        return(false);
                    }
                    mFrameId = rcvBytes[5] * 256 + rcvBytes[6];
                    if (mFrameId < 1)
                    {
                        return(false);
                    }
                    //长度
                    int      framelen = rcvBytes[7];
                    FileData file     = mFiles[filetype];
                    int      len;
                    if (framelen >= file.dFrameLen)
                    {
                        len = file.dFrameLen;
                    }
                    else
                    {
                        len = framelen;
                    }
                    //赋值给文件
                    for (int j = 0; j < len; j++)
                    {
                        file.Data[mFrameId * file.dFrameLen + j] = rcvBytes[8 + j];
                    }
                    //返回数据
                    rspBytes = new byte[8];
                    for (int i = 0; i < 7; i++)
                    {
                        rspBytes[i] = rcvBytes[i];
                    }
                    rspBytes[3] = 4;
                    rspBytes[7] = (byte)(len);
                    if (mFrameId * file.dFrameLen + len == file.Data.Length)
                    {
                        mDState = FileDownState.Loaded;
                    }
                    return(true);
                }
                break;

            case FileDownState.Loaded:
                if (childcode == 0x03)
                {
                    if (datalen != 3)
                    {
                        return(false);
                    }
                    if (!mFiles.ContainsKey(filetype))
                    {
                        return(false);
                    }
                    FileData file  = mFiles[filetype];
                    byte[]   crc16 = CRC.CRC16(file.Data);
                    //CRC校验
                    if (rcvBytes[5] != crc16[1] || rcvBytes[6] != crc16[0])
                    {
                        rspBytes    = new byte[3];
                        rspBytes[0] = rcvBytes[0];
                        rspBytes[1] = 0xC1;
                        rspBytes[2] = 0x08;
                        mDState     = FileDownState.Error;
                        return(true);
                    }
                    //返回数据
                    rspBytes = new byte[7];
                    for (int i = 0; i < 7; i++)
                    {
                        rspBytes[i] = rcvBytes[i];
                    }
                    mDState = FileDownState.Finish;
                    return(true);
                }
                break;

            case FileDownState.Finish:
                if (childcode == 0x04)
                {
                    if (datalen != 1)
                    {
                        return(false);
                    }
                    if (!mFiles.ContainsKey(filetype))
                    {
                        return(false);
                    }
                    //返回数据
                    rspBytes    = new byte[6];
                    rspBytes[0] = rcvBytes[0];
                    rspBytes[1] = rcvBytes[1];
                    rspBytes[2] = rcvBytes[2];
                    rspBytes[3] = 2;
                    rspBytes[4] = rcvBytes[4];
                    rspBytes[5] = 0;
                    mDState     = FileDownState.Activating;
                    return(true);
                }
                break;

            case FileDownState.Activating:
                mDState = FileDownState.Activated;
                break;

            default:
                break;
            }
            return(false);
        }