예제 #1
0
        private int DecodeNonQuery(byte[] content)
        {
            DictNode rootNode = (DictNode)BEncode.Decode(content);
            IntNode  codeNode = (IntNode)rootNode["code"];

            switch (codeNode.Value)
            {
            case 1:
                IntNode lineNode = (IntNode)rootNode["linenum"];
                if (rootNode.ContainsKey("parmlist"))
                {
                    HandleParmsNode((rootNode)["parmlist"]);
                }
                return(lineNode.Value);

            case 2:
            case 3:
                throw new DbProxyException("数据集收到的数据包出错!");

            case 4:
                BytesNode messageNode = (BytesNode)rootNode["message"];
                throw new DbProxyException(messageNode.StringText);

            case 5:
                throw new DbProxyException("校验出错!");

            default:
                throw new DbProxyException(string.Format("无效消息代码:{0}!", codeNode.Value));
            }
        }
예제 #2
0
        /// <summary>
        /// 处理打开数据包
        /// </summary>
        /// <param name="packet">打开数据包</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="error">错误信息</param>
        /// <returns>返回是否成功打开连接</returns>
        private bool HandleOpenPacket(byte[] packet, string ipAddress, out string error)
        {
            string   deviceId = string.Empty;
            DictNode openNode = (DictNode)BEncode.Decode(packet);

            if (openNode.ContainsKey("deviceid"))
            {
                deviceId = ((BytesNode)openNode["deviceid"]).StringText;
            }

            else
            {
                error = "不包含机构号";
                return(false);
            }

            if (openNode.ContainsKey("priority"))
            {
                int priority = (byte)((IntNode)openNode["priority"]).Value;
                if (priority > 4 || priority < 0)
                {
                    error = "优先级错误";
                    return(false);
                }
            }
            else
            {
                error = "不包含优先级";
                return(false);
            }

            bool result = Validate(deviceId, ipAddress, out error);

            return(result);
        }
예제 #3
0
        public void TestDecodeByteArray1()
        {
            //Test1
            BytesNode bah1 = (BytesNode)BEncode.Decode("10:0123456789");

            Assert.AreEqual(bah1.ByteArray, Encoding.Default.GetBytes("0123456789"));
            Assert.AreEqual(bah1.StringText, "0123456789");

            //Test2
            BytesNode bah2 = (BytesNode)BEncode.Decode("26:abcdefghijklmnopqrstuvwxyz");

            Assert.AreEqual(bah2.ByteArray, Encoding.Default.GetBytes("abcdefghijklmnopqrstuvwxyz"));
            Assert.AreEqual(bah2.StringText, "abcdefghijklmnopqrstuvwxyz");

            //Test3
            BytesNode bah3 = (BytesNode)BEncode.Decode("124:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

            Assert.AreEqual(Encoding.Default.GetBytes("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), bah3.ByteArray);
            Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", bah3.StringText);

            //Test4
            BytesNode bah4 = (BytesNode)BEncode.Decode("0:");

            Assert.AreEqual(bah4.ByteArray, Encoding.Default.GetBytes(string.Empty));
            Assert.AreEqual(bah4.StringText, string.Empty);
        }
예제 #4
0
        public void TestDecodeDictionary1()
        {
            //Test1整数
            DictNode dh1 = (DictNode)BEncode.Decode("[3:agei25e]");

            Assert.AreEqual(((IntNode)dh1["age"]).Value, 25);

            //Test2字节数组
            DictNode dh2 = (DictNode)BEncode.Decode("[3:agei25e5:color4:blue]");

            Assert.AreEqual(((IntNode)dh2["age"]).Value, 25);

            Assert.AreEqual((dh2["color"] as BytesNode).ByteArray, Encoding.Default.GetBytes("blue"));
            Assert.AreEqual((dh2["color"] as BytesNode).StringText, "blue");

            //Test3字节数组与整数
            DictNode dh3        = (DictNode)BEncode.Decode("[8:spam.mp3[6:author5:Alice6:lengthi1048576e]]");
            DictNode dHandler31 = (DictNode)dh3["spam.mp3"];

            Assert.AreEqual((dHandler31["author"] as BytesNode).ByteArray, Encoding.Default.GetBytes("Alice"));
            Assert.AreEqual((dHandler31["author"] as BytesNode).StringText, "Alice");
            Assert.AreEqual(((IntNode)dHandler31["length"]).Value, 1048576);

            //Test4空字典
            DictNode dh4 = (DictNode)BEncode.Decode("[]");

            Assert.AreEqual(dh4.Count, 0);
        }
예제 #5
0
        private object DecodeScalar(byte[] content)
        {
            DictNode rootNode = (DictNode)BEncode.Decode(content);
            IntNode  codeNode = (IntNode)rootNode["code"];

            switch (codeNode.Value)
            {
            case 1:
            case 2:
                throw new DbProxyException("数据集收到的数据包出错!");

            case 3:
                DictNode itemNode = (DictNode)rootNode["item"];
                string   typeName = ((BytesNode)itemNode["type"]).StringText;
                string   value    = ((BytesNode)itemNode["value"]).StringText;
                if (rootNode.ContainsKey("parmlist"))
                {
                    HandleParmsNode((rootNode)["parmlist"]);
                }
                return(NodeEncoder.GetObject(typeName, value));

            case 4:
                BytesNode messageNode = (BytesNode)rootNode["message"];
                throw new DbProxyException(messageNode.StringText);

            case 5:
                throw new DbProxyException("校验出错!");

            default:
                throw new DbProxyException(string.Format("无效消息代码:{0}!", codeNode.Value));
            }
        }
예제 #6
0
        /// <summary>
        /// 验证握手数据包
        /// </summary>
        /// <param name="handshake">握手数据包</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">初始化向量</param>
        /// <returns>如果验证成功返回true;否则返回false</returns>
        public static bool VerifyHandshake(byte[] handshake, byte[] key, byte[] iv)
        {
            DictNode node = (DictNode)BEncode.Decode(handshake);

            byte[] handshakeKey = ((BytesNode)node["key"]).ByteArray;
            byte[] handshakeIV  = ((BytesNode)node["iv"]).ByteArray;
            return(BytesComparer.Compare(handshakeKey, key) && BytesComparer.Compare(handshakeIV, iv));
        }
예제 #7
0
        public void TestDecodeList1()
        {
            //Test1整数
            ListNode lh1 = (ListNode)BEncode.Decode("{i0ei1ei2e}");

            Assert.AreEqual(((IntNode)lh1[0]).Value, 0);
            Assert.AreEqual(((IntNode)lh1[1]).Value, 1);
            Assert.AreEqual(((IntNode)lh1[2]).Value, 2);

            //Test2字节数组
            ListNode lh2 = (ListNode)BEncode.Decode("{3:abc2:xy}");

            Assert.AreEqual((lh2[0] as BytesNode).ByteArray, Encoding.Default.GetBytes("abc"));
            Assert.AreEqual((lh2[0] as BytesNode).StringText, "abc");

            Assert.AreEqual((lh2[1] as BytesNode).ByteArray, Encoding.Default.GetBytes("xy"));
            Assert.AreEqual((lh2[1] as BytesNode).StringText, "xy");

            //Test3空字节数组
            ListNode lh3 = (ListNode)BEncode.Decode("{0:0:0:}");

            Assert.AreEqual((lh3[0] as BytesNode).ByteArray, Encoding.Default.GetBytes(string.Empty));
            Assert.AreEqual((lh3[0] as BytesNode).StringText, string.Empty);

            Assert.AreEqual((lh3[1] as BytesNode).ByteArray, Encoding.Default.GetBytes(string.Empty));
            Assert.AreEqual((lh3[1] as BytesNode).StringText, string.Empty);

            Assert.AreEqual((lh3[2] as BytesNode).ByteArray, Encoding.Default.GetBytes(string.Empty));
            Assert.AreEqual((lh3[2] as BytesNode).StringText, string.Empty);

            //Test4字节数组与整数
            ListNode lh4        = (ListNode)BEncode.Decode("{{5:Alice3:Bob}{i2ei3e}}");
            ListNode lHandler40 = (ListNode)lh4[0];
            ListNode lHandler41 = (ListNode)lh4[1];

            Assert.AreEqual((lHandler40[0] as BytesNode).ByteArray, Encoding.Default.GetBytes("Alice"));
            Assert.AreEqual((lHandler40[0] as BytesNode).StringText, "Alice");

            Assert.AreEqual((lHandler40[1] as BytesNode).ByteArray, Encoding.Default.GetBytes("Bob"));
            Assert.AreEqual((lHandler40[1] as BytesNode).StringText, "Bob");

            Assert.AreEqual(((IntNode)lHandler41[0]).Value, 2);

            Assert.AreEqual(((IntNode)lHandler41[1]).Value, 3);

            //Test5空列表
            ListNode lh5 = (ListNode)BEncode.Decode("{}");

            Assert.AreEqual(lh5.Count, 0);
        }
예제 #8
0
        /// <summary>
        /// 检验打开数据包
        /// </summary>
        /// <param name="packet">打开数据包</param>
        /// <returns>如果打开成功,返回ture,否则返回false</returns>
        private bool CheckOpenPacket(byte[] packet)
        {
            DictNode openPacket = (DictNode)BEncode.Decode(packet);
            int      code       = ((IntNode)openPacket["code"]).Value;

            switch (code)
            {
            case 1:
                byte[] key = ((BytesNode)openPacket["key"]).ByteArray;
                byte[] iv  = ((BytesNode)openPacket["iv"]).ByteArray;
                handler.SetKeyIV(key, iv);
                return(true);

            case 6:
                string messag = ((BytesNode)openPacket["message"]).StringText;
                throw new DbProxyException(messag);

            default:
                throw new DbProxyException("错误的消息代码");
            }
        }
예제 #9
0
        public void TestDecodeInteger1()
        {
            //Test1正整数
            IntNode ih1 = (IntNode)BEncode.Decode("i10e");

            Assert.AreEqual(ih1.Value, 10);

            //Test2零
            IntNode ih2 = (IntNode)BEncode.Decode("i0e");

            Assert.AreEqual(ih2.Value, 0);

            //Test3负整数
            IntNode ih3 = (IntNode)BEncode.Decode("i-55e");

            Assert.AreEqual(ih3.Value, -55);

            //Test4所有的数字
            IntNode ih4 = (IntNode)BEncode.Decode("i1234567890e");

            Assert.AreEqual(ih4.Value, 1234567890);
        }
예제 #10
0
 /// <summary>
 /// 检验打开数据包
 /// </summary>
 /// <param name="packet">打开数据包</param>
 /// <param name="key">密钥</param>
 /// <param name="iv">初始化向量</param>
 /// <param name="error">错误信息</param>
 /// <returns>如果打开成功,返回ture,否则返回false</returns>
 private bool CheckOpenPacket(byte[] packet, out byte[] key, out byte[] iv, out string error)
 {
     key   = null;
     iv    = null;
     port  = -1;
     error = string.Empty;
     try
     {
         DictNode node = (DictNode)BEncode.Decode(packet);
         if (node.ContainsKey("key") && node.ContainsKey("iv"))
         {
             key = ((BytesNode)node["key"]).ByteArray;
             iv  = ((BytesNode)node["iv"]).ByteArray;
             return(true);
         }
         error = "数据包不包含密钥或者初始化向量或者端口信息!";
     }
     catch (BEncodingException bee)
     {
         error = bee.Message;
     }
     return(false);
 }
예제 #11
0
 public void TestDecodeInteger4()
 {
     BEncode.Decode("i-0e");
 }
예제 #12
0
 public void TestDecodeInteger2()
 {
     BEncode.Decode("ie");
 }
예제 #13
0
 public void TestDecodeInteger3()
 {
     BEncode.Decode("i341foo382e");
 }
예제 #14
0
 public void TestDecodeDictionary10()
 {
     BEncode.Decode("[i1ei0e]");
 }
예제 #15
0
 public void TestDecodeList3()
 {
     BEncode.Decode("{");
 }
예제 #16
0
 public void TestDecodeByteArray5()
 {
     BEncode.Decode("9:abc");
 }
예제 #17
0
 public void TestDecodeList4()
 {
     BEncode.Decode("{0:");
 }
예제 #18
0
 public void TestDecodeHandler3()
 {
     BEncode.Decode("35208734823ljdahflajhdf");
 }
예제 #19
0
 public void TestDecodeDictionary7()
 {
     BEncode.Decode("[0:1:a]");
 }
예제 #20
0
 public void TestDecodeHandler2()
 {
     BEncode.Decode("");
 }
예제 #21
0
 public void TestDecodeDictionary6()
 {
     BEncode.Decode("[i1e0:]");
 }
예제 #22
0
 public void TestDecodeDictionary4()
 {
     BEncode.Decode("[]0564adf");
 }
예제 #23
0
 public void TestDecodeDictionary3()
 {
     BEncode.Decode("[");
 }
예제 #24
0
 public void TestDecodeDictionary2()
 {
     BEncode.Decode("[3:agei25e3:agei50e]");
 }
예제 #25
0
 public void TestDecodeList2()
 {
     BEncode.Decode("{}zeral");
 }
예제 #26
0
 public void TestDecodeDictionary8()
 {
     BEncode.Decode("[0:");
 }
예제 #27
0
        private void DecodeReader(byte[] content, DataTable table)
        {
            DictNode rootNode = (DictNode)BEncode.Decode(content);
            IntNode  codeNode = (IntNode)rootNode["code"];

            switch (codeNode.Value)
            {
            case 1:
            case 3:
                throw new DbProxyException("数据集收到的数据包出错!");

            case 4:
                BytesNode messageNode = (BytesNode)rootNode["message"];
                throw new DbProxyException(messageNode.StringText);

            case 5:
                throw new DbProxyException("校验出错!");
            }

            //解码类型表
            ListNode typelistNode = (ListNode)rootNode["typelist"];

            table.BeginInit();
            table.BeginLoadData();
            foreach (BEncodedNode node in typelistNode)
            {
                BytesNode  nameNode = (BytesNode)(((DictNode)node)["name"]);
                BytesNode  typeNode = (BytesNode)(((DictNode)node)["type"]);
                DataColumn column   = new DataColumn(nameNode.StringText);
                column.DataType = types[typeNode.StringText];
                table.Columns.Add(column);
            }

            //解码数据集
            ListNode itemsNode = (ListNode)rootNode["items"];

            for (int i = 0; i < itemsNode.Count; i++)
            {
                ListNode itemNode = (ListNode)itemsNode[i];
                DataRow  row      = table.NewRow();
                for (int j = 0; j < itemNode.Count; j++)
                {
                    Type   type     = table.Columns[j].DataType;
                    string valueStr = ((BytesNode)itemNode[j]).StringText;
                    if (valueStr == null)
                    {
                        row[j] = DBNull.Value;
                    }

                    else if (type == typeof(string))
                    {
                        row[j] = valueStr;
                    }

                    else if (type == typeof(byte))
                    {
                        row[j] = byte.Parse(valueStr);
                    }

                    else if (type == typeof(sbyte))
                    {
                        row[j] = sbyte.Parse(valueStr);
                    }

                    else if (type == typeof(ushort))
                    {
                        row[j] = ushort.Parse(valueStr);
                    }

                    else if (type == typeof(short))
                    {
                        row[j] = short.Parse(valueStr);
                    }

                    else if (type == typeof(uint))
                    {
                        row[j] = uint.Parse(valueStr);
                    }

                    else if (type == typeof(int))
                    {
                        row[j] = int.Parse(valueStr);
                    }

                    else if (type == typeof(ulong))
                    {
                        row[j] = ulong.Parse(valueStr);
                    }

                    else if (type == typeof(long))
                    {
                        row[j] = long.Parse(valueStr);
                    }

                    else if (type == typeof(byte[]))
                    {
                        row[j] = ((BytesNode)itemNode[j]).ByteArray;
                    }

                    else if (type == typeof(DateTime))
                    {
                        row[j] = DateTime.Parse(valueStr);
                    }

                    else if (type == typeof(float))
                    {
                        row[j] = float.Parse(valueStr);
                    }

                    else if (type == typeof(double))
                    {
                        row[j] = double.Parse(valueStr);
                    }

                    else if (type == typeof(decimal))
                    {
                        row[j] = decimal.Parse(valueStr);
                    }
                }
                table.Rows.Add(row);
            }
            if (rootNode.ContainsKey("parmlist"))
            {
                HandleParmsNode((rootNode)["parmlist"]);
            }
            table.EndLoadData();
            table.EndInit();
        }
예제 #28
0
 public void TestDecodeDictionary9()
 {
     BEncode.Decode("[01:x0:]");
 }
예제 #29
0
 public void TestDecodeDictionary5()
 {
     BEncode.Decode("[3:foo]");
 }
예제 #30
0
 public void TestDecodeList5()
 {
     BEncode.Decode("{01:x}");
 }