Example #1
0
        public async override Task <bool> SendMsg(params object[] _params)
        {
            try
            {
                byte[]          data         = (byte[])_params[0];
                EnumInstallType type         = (EnumInstallType)_params[1];
                EnumPluginType  content      = (EnumPluginType)_params[2];
                string          version      = ((string)_params[3]).Split('.')[0].PadLeft(2, '0') + ((string)_params[3]).Split('.')[1].PadRight(2, '0');
                byte[]          byte_version = ThinNeo.Helper.HexString2Bytes(version);

                var    hash     = NeoDun.SignTool.ComputeSHA256(data, 0, data.Length);
                string str_hash = NeoDun.SignTool.Bytes2HexString(hash, 0, hash.Length);
                int    num      = data.Length / (1024 * 50) + 1;

                List <UInt32> remoteids = new List <UInt32>();

                for (var i = 0; i < num; i++)
                {
                    byte[]           _data     = data.Skip(1024 * 50 * i).Take(1024 * 50).ToArray();
                    var              _hash     = NeoDun.SignTool.ComputeSHA256(_data, 0, _data.Length);
                    string           _str_hash = NeoDun.SignTool.Bytes2HexString(_hash, 0, _hash.Length);
                    NeoDun.DataBlock block     = signer.dataTable.newOrGet(_str_hash, (UInt32)_data.Length, NeoDun.DataBlockFrom.FromDriver);
                    block.data = _data;
                    signer.SendDataBlock(block);

                    var  __block  = signer.dataTable.getBlockBySha256(_str_hash);
                    uint remoteid = await __block.GetRemoteid();

                    if (remoteid == 0)
                    {
                        result.msgCode   = EnumMsgCode.InstallFailed;
                        result.errorCode = EnumErrorCode.IncorrectHash;
                    }
                    remoteids.Add((uint)remoteid);
                    __block.dataidRemote = 0;
                }
                NeoDun.Message signMsg = new NeoDun.Message();
                signMsg.tag1  = 0x03;
                signMsg.tag2  = 0x01;
                signMsg.msgid = NeoDun.SignTool.RandomShort();
                signMsg.writeUInt16(0, (UInt16)type);
                signMsg.writeUInt16(2, (UInt16)content);
                Array.Copy(hash, 0, signMsg.data, 4, hash.Length);
                signMsg.data[36] = byte_version[0];
                signMsg.data[37] = byte_version[1];
                for (var i = 0; i < remoteids.Count; i++)
                {
                    signMsg.writeUInt32(38, remoteids[i]);
                }

                signer.SendMessage(signMsg, true);
                return(true);
            }
            catch (Exception e)
            {
                result.msgCode   = EnumMsgCode.InstallFailed;
                result.errorCode = EnumErrorCode.IncorrectHash;
                return(false);
            }
        }
Example #2
0
        public async override Task <bool> SendMsg(params object[] _params)
        {
            var data    = ECDH.Ins.pubKey;
            var hash    = NeoDun.SignTool.ComputeSHA256(data, 0, data.Length);
            var hashstr = NeoDun.SignTool.Bytes2HexString(hash, 0, hash.Length);

            //发送数据块
            var block = signer.dataTable.newOrGet(hashstr, (UInt32)data.Length, NeoDun.DataBlockFrom.FromDriver);

            block.data = data;
            signer.SendDataBlock(block);
            var __block = signer.dataTable.getBlockBySha256(hashstr);

            uint remoteid = await __block.GetRemoteid();

            NeoDun.Message signMsg = new NeoDun.Message();

            {
                signMsg.tag1  = 0x04;
                signMsg.tag2  = 0x01;
                signMsg.msgid = NeoDun.SignTool.RandomShort();

                //这个dataid 要上一个block 传送完毕了才知道
                signMsg.writeUInt32(42, (uint)remoteid);
                signer.SendMessage(signMsg, true);
            }
            return(true);
        }
Example #3
0
        void OnMsg(Message msg)
        {
            Message srcmsg = null;

            if (msg.tag1 == 0x00 || msg.tag2 >= 0xa0)//如果是一条回复消息,找原始消息
            {
                if (this.needBackMessage.ContainsKey(msg.msgid))
                {
                    srcmsg = this.needBackMessage[msg.msgid];
                }
                else
                {
                    return;
                }
            }


            System.Threading.ThreadPool.QueueUserWorkItem(async(_state) =>
            {
                #region 0x01
                if (msg.tag1 == 0x01 && msg.tag2 == 0x01)
                {
                    //recv a file
                    var len   = msg.readUInt32(0);
                    var hash  = msg.readHash256(4);
                    var block = dataTable.newOrGet(hash, len, NeoDun.DataBlockFrom.FromSigner);

                    var piececount = len / 50;
                    if (len % 50 > 0)
                    {
                        piececount++;
                    }

                    await System.Threading.Tasks.Task.Delay(50);//add bu hkh 1216

                    System.Threading.ThreadPool.QueueUserWorkItem((__state) =>
                    {
                        NeoDun.Message _msg = new NeoDun.Message();
                        _msg.tag1           = 0x01;
                        _msg.tag2           = 0x10;
                        _msg.msgid          = NeoDun.SignTool.RandomShort();
                        _msg.writeUInt16(0, 0);
                        _msg.writeUInt16(2, (UInt16)(piececount - 1));
                        _msg.writeHash256(4, hash);
                        SendMessage(_msg, true);
                    });
                }
                if (msg.tag1 == 0x01 && msg.tag2 == 0x10)//请求数据
                {
                    int blockbegin = BitConverter.ToUInt16(msg.data, 0);
                    int blockend   = BitConverter.ToUInt16(msg.data, 2);
                    string hash    = SignTool.Bytes2HexString(msg.data, 4, 32);
                    var block      = dataTable.getBlockBySha256(hash);
                    if (block == null)
                    {
                        //发送错误回应,下一步再说
                    }
                    else
                    {
                        for (var i = blockbegin; i < blockend + 1; i++)
                        {
                            Message msg1 = new Message();
                            block.FillPieceMessage(msg1, msg.msgid, (UInt16)i);
                            SendMessage(msg1, false);
                        }
                        //Send0103
                        Message msg2 = new Message();
                        msg2.tag1    = 0x01;
                        msg2.tag2    = 0xa3;
                        msg2.msgid   = msg.msgid;//这是一条回复消息
                        SendMessage(msg2, false);
                    }
                }
                if (msg.tag1 == 0x01 && msg.tag2 == 0x11)//给数据分配编号
                {
                    var remoteid       = msg.readUInt32(0);
                    var hash           = msg.readHash256(4);
                    var block          = dataTable.getBlockBySha256(hash);
                    block.dataidRemote = (uint)remoteid;
                }
                if (msg.tag1 == 0x01 && msg.tag2 == 0x12)
                {
                    var remoteid       = msg.readUInt32(0);
                    var hash           = msg.readHash256(4);
                    var block          = dataTable.getBlockBySha256(hash);
                    block.dataidRemote = (uint)remoteid;
                }
                if (msg.tag1 == 0x01 && msg.tag2 == 0xa2)//收到一个分片
                {
                    var hash = srcmsg.readHash256(4);
                    var data = dataTable.getBlockBySha256(hash);
                    data.FromPieceMsg(msg);
                }
                if (msg.tag1 == 0x01 && msg.tag2 == 0xa3)//接收完毕
                {
                    var hash    = srcmsg.readHash256(4);
                    var data    = dataTable.getBlockBySha256(hash);
                    bool bcheck = data.Check();
                    if (bcheck)
                    {//数据接收完整
                        System.Threading.ThreadPool.QueueUserWorkItem((__state) =>
                        {
                            NeoDun.Message _msg = new NeoDun.Message();
                            _msg.tag1           = 0x01;
                            _msg.tag2           = 0x11;
                            _msg.msgid          = NeoDun.SignTool.RandomShort();
                            _msg.writeUInt32(0, data.dataid);
                            _msg.writeHash256(4, hash);
                            SendMessage(_msg, false);
                        });
                    }
                    else
                    {//数据接收完毕,但是hash256 不匹配
                        System.Threading.ThreadPool.QueueUserWorkItem((__state) =>
                        {
                            NeoDun.Message _msg = new NeoDun.Message();
                            _msg.tag1           = 0x01;
                            _msg.tag2           = 0x12;
                            _msg.msgid          = NeoDun.SignTool.RandomShort();
                            _msg.writeUInt32(0, data.dataid);
                            _msg.writeHash256(4, hash);
                            SendMessage(_msg, false);
                        });
                    }
                }
                #endregion

                //设置地址名称失败
                if (msg.tag1 == 0x02 && msg.tag2 == 0xe2)
                {
                    EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0);
                    eventHandler(EnumControl.SetName, EnumMsgCode.SetNameFailed, errorCode);
                }
                //删除地址失败
                if (msg.tag1 == 0x02 && msg.tag2 == 0xe3)
                {
                    EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0);
                    eventHandler(EnumControl.DelAddress, EnumMsgCode.DeleteNameFailed, errorCode);
                }
                //增加地址失败
                if (msg.tag1 == 0x02 && msg.tag2 == 0xe4)
                {
                    EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0);
                    eventHandler(EnumControl.AddAddress, EnumMsgCode.AddAddressFailed, errorCode);
                }
                //签名失败
                if (msg.tag1 == 0x02 && msg.tag2 == 0xe5)
                {
                    EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0);
                    eventHandler(EnumControl.SignData, EnumMsgCode.SignFailed, errorCode);
                }
                //安装失败
                if (msg.tag1 == 0x03 && msg.tag2 == 0xe1)
                {
                    EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0);
                    eventHandler(EnumControl.Common, EnumMsgCode.InstallFailed, errorCode);
                }
                //拒绝更新固件
                if (msg.tag1 == 0x03 && msg.tag2 == 0xe2)
                {
                    eventHandler(EnumControl.ApplyInstallFramework, EnumMsgCode.RefuseUpdate, EnumErrorCode.NoError);
                }
                //卸载失败
                if (msg.tag1 == 0x03 && msg.tag2 == 0xe3)
                {
                    EnumErrorCode errorCode = (EnumErrorCode)msg.readUInt16(0);
                    eventHandler(EnumControl.UninstallPlugin, EnumMsgCode.UninstallFailed, errorCode);
                }
                //收到地址,//加进地址池子里
                if (msg.tag1 == 0x02 && msg.tag2 == 0xa0)
                {
                    var epoch       = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
                    var pos         = msg.readUInt16(0);
                    var count       = msg.readUInt16(2);
                    var type        = (NeoDun.AddressType)msg.readUInt16(4);
                    var add         = new NeoDun.Address();
                    add.type        = type;
                    add.AddressText = SignTool.EncodeBase58(msg.data, 6, 25);
                    add.name        = System.Text.Encoding.UTF8.GetString(msg.data, 32, 6).Replace("\0", "");
                    addressPool.addresses.Add(add);
                }
                //地址接受完毕
                if (msg.tag1 == 0x02 && msg.tag2 == 0xa1)
                {
                    eventHandler(EnumControl.GetAddress, EnumMsgCode.GetAddressSuc, EnumErrorCode.NoError);
                }
                //设置地址名称成功
                if (msg.tag1 == 0x02 && msg.tag2 == 0xa2)
                {
                    eventHandler(EnumControl.SetName, EnumMsgCode.SetNameSuc, EnumErrorCode.NoError);
                }
                //删除地址成功
                if (msg.tag1 == 0x02 && msg.tag2 == 0xa3)
                {
                    eventHandler(EnumControl.DelAddress, EnumMsgCode.DeleteNameSuc, EnumErrorCode.NoError);
                }
                //增加地址成功
                if (msg.tag1 == 0x02 && msg.tag2 == 0xa4)
                {
                    eventHandler(EnumControl.AddAddress, EnumMsgCode.AddAddressSuc, EnumErrorCode.NoError);
                }
                //签名成功
                if (msg.tag1 == 0x02 && msg.tag2 == 0xa5)
                {
                    byte[] outdata     = null;
                    string outdatahash = null;
                    outdatahash        = msg.readHash256(4);
                    //轮询直到reciveid的数据被收到
                    while (true)
                    {
                        await Task.Delay(5);
                        var __block = dataTable.getBlockBySha256(outdatahash);
                        if (__block != null && __block.Check())
                        {
                            outdata = __block.data;
                            break;
                        }
                    }
                    eventHandler(EnumControl.SignData, EnumMsgCode.SignSuc, EnumErrorCode.NoError, outdata);
                }
                //安装成功
                if (msg.tag1 == 0x03 && msg.tag2 == 0xa1)
                {
                    eventHandler(EnumControl.Common, EnumMsgCode.InstallSuc, EnumErrorCode.NoError);
                }
                //同意更新固件
                if (msg.tag1 == 0x03 && msg.tag2 == 0xa2)
                {
                    eventHandler(EnumControl.ApplyInstallFramework, EnumMsgCode.AgreeUpdate, EnumErrorCode.NoError);
                }
                //卸载成功
                if (msg.tag1 == 0x03 && msg.tag2 == 0xa3)
                {
                    eventHandler(EnumControl.UninstallPlugin, EnumMsgCode.UninstallSuc, EnumErrorCode.NoError);
                }
                //查询固件插件版本回复
                if (msg.tag1 == 0x03 && msg.tag2 == 0xa4)
                {
                    byte[] outdata = null;
                    outdata        = msg.data;
                    eventHandler(EnumControl.GetPackage, EnumMsgCode.GetMessageSuc, EnumErrorCode.NoError, outdata);
                }
                //下位机请求更新固件
                if (msg.tag1 == 0x03 && msg.tag2 == 0x11)
                {
                    deleInstallFramework();
                }
                //安全通道回复
                if (msg.tag1 == 0x04 && msg.tag2 == 0xa1)
                {
                    byte[] outdata     = null;
                    string outdatahash = null;
                    outdatahash        = msg.readHash256(4);
                    //轮询直到reciveid的数据被收到
                    while (true)
                    {
                        await Task.Delay(5);
                        var __block = dataTable.getBlockBySha256(outdatahash);
                        if (__block != null && __block.Check())
                        {
                            outdata = __block.data;
                            break;
                        }
                    }
                    eventHandler(EnumControl.SecurityChannel, EnumMsgCode.BuildSecurityChannelSuc, EnumErrorCode.NoError, outdata);
                }
            });
            watcherColl.OnRecv(msg, srcmsg);
            //if (userHandleRecv != null)
            //{
            //    userHandleRecv(msg, srcmsg);
            //}
        }