コード例 #1
0
        protected override Object getReplyTail(byte[] package)
        {
            ProtocalHead head = (ProtocalHead)getReplyHead(package);

            byte[] tailBytes = ByteArray.CopyTo(package, Marshal.SizeOf(typeof(ProtocalHead)) + head.size, Marshal.SizeOf(typeof(ProtocalTail)));
            return(Struct.BytesToStuct(tailBytes, typeof(ProtocalTail)));
        }
コード例 #2
0
        private bool checkout(byte[] buffer)
        {
            byte[]       headBytes = ByteArray.CopyTo(buffer, 0, Marshal.SizeOf(typeof(ProtocalHead)));
            ProtocalHead head      = (ProtocalHead)Struct.BytesToStuct(headBytes, typeof(ProtocalHead));

            byte[]       bodyBytes = ByteArray.CopyTo(buffer, Marshal.SizeOf(typeof(ProtocalHead)), head.size);
            byte[]       tailBytes = ByteArray.CopyTo(buffer, Marshal.SizeOf(typeof(ProtocalHead)) + head.size, Marshal.SizeOf(typeof(ProtocalTail)));
            ProtocalTail tail      = (ProtocalTail)Struct.BytesToStuct(tailBytes, typeof(ProtocalTail));
            UInt16       crc       = Utils.getCRC16(0, ByteArray.CombomByteArray(headBytes, bodyBytes), Marshal.SizeOf(typeof(ProtocalHead)) + head.size);

            return(crc == tail.crc16);
        }
コード例 #3
0
        private bool autoUpgrade()
        {
            Console.WriteLine("Try auto upgrade...");
            udpCommu.remoteEP = new IPEndPoint(IPAddress.Broadcast, prmPort);
            Protocal.CmdTxUpgrade outPackage;
            outPackage.cmd    = Protocal.CMD_FIRMWARE_UPDATE;
            outPackage.code   = (byte)Protocal.UpgradeCode.AskAllowUpgrade;
            outPackage.passwd = Protocal.UpgradePasswd;
            byte[] outBytes = Struct.StructToBytes(outPackage);
            byte[] inBytes;
            try
            {
                Console.WriteLine("Shake hands...");
                udpCommu.sendCmd(outBytes, outBytes.Length, out inBytes, Marshal.SizeOf(typeof(Protocal.CmdRxUpgrade)));
                Protocal.CmdRxUpgrade upgress = (Protocal.CmdRxUpgrade)Struct.BytesToStuct(inBytes, typeof(Protocal.CmdRxUpgrade));
                if (upgress.replyCode != (byte)Protocal.UpgradeStatus.Allow)
                {
                    Console.WriteLine("Quit auto upgrade, because of " + (Protocal.UpgradeStatus)upgress.replyCode);
                    return(false);
                }
                Console.WriteLine("OK!");

                Console.WriteLine("Start...");
                outPackage.code = (byte)Protocal.UpgradeCode.StartUpgrade;
                outBytes        = Struct.StructToBytes(outPackage);
                udpCommu.sendCmd(outBytes, outBytes.Length, out inBytes, Marshal.SizeOf(typeof(Protocal.CmdRxUpgrade)));
                upgress = (Protocal.CmdRxUpgrade)Struct.BytesToStuct(inBytes, typeof(Protocal.CmdRxUpgrade));
                if (upgress.replyCode != (byte)Protocal.UpgradeStatus.Allow)
                {
                    Console.WriteLine("Quit auto upgrade, because of " + (Protocal.UpgradeStatus)upgress.replyCode);
                    return(false);
                }
                Console.WriteLine("OK!");
            }
            catch (SendCmdException e)
            {
                Console.WriteLine("Quit auto upgrade, because of " + e.errorStatus);
                return(false);
            }
            remoteEP      = udpCommu.remoteEP;
            remoteEP.Port = bootPort;
            refreshIP();

            return(true);
        }
コード例 #4
0
        private bool writeReq(UdpClient client, string filePath)
        {
            byte[] byteArray  = BitConverter.GetBytes(Utils.htons((UInt16)TftpCode.Wrq));
            byte[] byteArray1 = Encoding.Default.GetBytes(Path.GetFileName(filePath));
            byteArray = ByteArray.CombomByteArray(byteArray, byteArray1);

            byteArray1    = new byte[1];
            byteArray1[0] = 0;
            byteArray     = ByteArray.CombomByteArray(byteArray, byteArray1);

            byteArray1 = Encoding.Default.GetBytes("octet");
            byteArray  = ByteArray.CombomByteArray(byteArray, byteArray1);

            byteArray1    = new byte[1];
            byteArray1[0] = 0;
            byteArray     = ByteArray.CombomByteArray(byteArray, byteArray1);

            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse(remoteIP), remotePort);

            client.Send(byteArray, byteArray.Length, remoteIpEndPoint);

            IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

            byte[] receiveBytes = client.Receive(ref remoteEP);
            Reply  reply        = (Reply)Struct.BytesToStuct(receiveBytes, typeof(Reply));

            reply.opCode = Utils.htons(reply.opCode);
            reply.block  = Utils.htons(reply.block);
            if ((reply.opCode != (UInt16)TftpCode.Ack) || (reply.block != 0))
            {
                return(false);
            }
            remoteIP   = remoteEP.Address.ToString();
            remotePort = remoteEP.Port;
            Console.WriteLine("IP: {0}, Port: {1}", remoteIP, remotePort);
            return(true);
        }
コード例 #5
0
 protected override ushort getReplyCmd(byte[] package)
 {
     byte[] bodyBytes            = ByteArray.CopyTo(package, Marshal.SizeOf(typeof(ProtocalHead)), Marshal.SizeOf(typeof(Protocal.CmdReplyVoid)));
     Protocal.CmdReplyVoid reply = (Protocal.CmdReplyVoid)Struct.BytesToStuct(bodyBytes, typeof(Protocal.CmdReplyVoid));
     return(reply.cmd);
 }
コード例 #6
0
 protected override Object getReplyHead(byte[] package)
 {
     byte[] headBytes = ByteArray.CopyTo(package, 0, Marshal.SizeOf(typeof(ProtocalHead)));
     return(Struct.BytesToStuct(headBytes, typeof(ProtocalHead)));
 }
コード例 #7
0
        public bool writeData(UdpClient client, string filePath, byte xor)
        {
            bool     ret             = true;
            FileInfo fileInfo        = new FileInfo(filePath);
            long     fileTotalLength = fileInfo.Length;
            long     fileWriteLenght = 0;

            try
            {
                FileStream fileStreamIn = File.OpenRead(filePath);
                byte[]     buffer       = new byte[TFTP_MAX_DATE_SIZE];
                int        readLenth    = 0;
                UInt16     nextBlock    = 0;
                do
                {
                    readLenth = fileStreamIn.Read(buffer, 0, buffer.Length);
                    for (int i = 0; i < readLenth; ++i)
                    {
                        buffer[i] ^= xor;
                    }
                    nextBlock++;

                    WriteData outData;
                    outData.opCode = Utils.htons((UInt16)TftpCode.Data);
                    outData.block  = Utils.htons(nextBlock);
                    outData.data   = buffer;
                    byte[] bytes = Struct.StructToBytes(outData);

                    int  count   = 0;
                    bool replyOK = false;
                    do
                    {
                        try
                        {
                            IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse(remoteIP), remotePort);
                            client.Send(bytes, Marshal.SizeOf(outData.opCode)
                                        + Marshal.SizeOf(outData.block) + readLenth, remoteIpEndPoint);

                            Reply reply;
                            do
                            {
                                IPEndPoint remoteEP     = new IPEndPoint(IPAddress.Any, 0);
                                byte[]     receiveBytes = client.Receive(ref remoteEP);
                                reply        = (Reply)Struct.BytesToStuct(receiveBytes, typeof(Reply));
                                reply.opCode = Utils.htons(reply.opCode);
                                reply.block  = Utils.htons(reply.block);
                            } while ((reply.opCode != (UInt16)TftpCode.Ack) || (reply.block != nextBlock));
                            replyOK = true;
                        }
                        catch
                        {
                            count++;
                            msg_CallBack?.Invoke(TftpStatus.Err_Retry, count.ToString());
                        }
                        finally
                        {
                            if (count >= retryCnt)
                            {
                                ret = false;
                            }
                        }
                    } while ((replyOK == false) && (ret == true));
                    /* 发送进度 */
                    if (replyOK == true)
                    {
                        fileWriteLenght += readLenth;
                        int progress = (int)((float)fileWriteLenght / fileTotalLength * 100);
                        msg_CallBack?.Invoke(TftpStatus.WriteProgress, progress.ToString());
                    }
                } while ((ret == true) && (readLenth >= TFTP_MAX_DATE_SIZE)); /* 最后的数据包长度为0时,仍要发送给服务器 */
                if (ret == true)
                {
                    msg_CallBack?.Invoke(TftpStatus.Success, "发送 [ " + fileWriteLenght.ToString() + "/" +
                                         fileTotalLength.ToString() + " ] Bytes!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }
            return(ret);
        }