예제 #1
0
        public void EqualityList()
        {
            // Make initial list
            BList testList = new BList();
            BString testString = new BString("Hello World");
            BInt testInt = new BInt(5);

            testList.Add(testString);
            testList.Add(testInt);

            // Make test list
            BList testList2 = new BList();
            BString testString2 = new BString("Hello World");
            BInt testInt2 = new BInt(5);

            testList2.Add(testString2);
            testList2.Add(testInt2);

            // Test equality recursive
            Assert.AreEqual(testList, testList2);

            // Test null list
            BList nullList = null;
            Assert.IsFalse(testList.Equals(nullList));

            // Test different counts
            testList2.Add(new BInt(10));
            Assert.IsFalse(testList.Equals(testList2));

            // Test different values
            testList.Add(new BInt(9));

            Assert.IsFalse(testList.Equals(testList2));
        }
예제 #2
0
        public void EncodeInteger()
        {
            BInt testInt = new BInt(5);
            string test = BencodingUtils.EncodeString(testInt);

            Assert.AreEqual("i5e", test);
        }
예제 #3
0
        public void BIntCompareNull()
        {
            BInt a = new BInt(10);
            BInt other = null;

            int result = a.CompareTo(other);
        }
        private void ProcessExtended(IPeerWireClient client, int commandLength, byte[] payload)
        {
            Int32 msgId = payload[0];

            byte[] buffer = payload.GetBytes(1, commandLength - 1);

            if (msgId == 0)
            {
                BDict extendedHandshake = (BDict)BencodingUtils.Decode(buffer);

                BDict mDict = (BDict)extendedHandshake["m"];
                foreach (KeyValuePair <string, IBencodingType> pair in mDict)
                {
                    BInt i = (BInt)pair.Value;
                    this._extIncoming.Add(new ClientProtocolIDMap(client, pair.Key, (byte)i));

                    IBTExtension ext = this.FindIBTExtensionByProtocol(pair.Key);

                    if (ext != null)
                    {
                        ext.OnHandshake(client, buffer);
                    }
                }
            }
            else
            {
                string       protocol = this.FindIBTProtocolByMessageID(msgId);
                IBTExtension ext      = this.FindIBTExtensionByProtocol(protocol);

                if (ext != null)
                {
                    ext.OnExtendedMessage(client, buffer);
                }
            }
        }
예제 #5
0
파일: BPrinter.cs 프로젝트: jlefever/Bitty
 public object VisitBInt(BInt node)
 {
     _bytes.Add((byte)BLiteral.StartInt);
     _bytes.AddRange(ToAscii(node.Value));
     _bytes.Add((byte)BLiteral.End);
     return(null);
 }
예제 #6
0
        public void EqualityInteger()
        {
            BInt testInt = new BInt(5);
            BInt testInt2 = new BInt(5);

            Assert.AreEqual(testInt, testInt2);
            Assert.IsTrue(testInt.Equals(testInt2.Value));
            Assert.IsTrue(testInt.Equals(testInt));
        }
예제 #7
0
        public void DecodeInteger()
        {
            const string testString = "i5e";
            BInt testExpected = new BInt(5);

            IBencodingType testResult = BencodingUtils.Decode(testString);

            Assert.AreEqual(testExpected, testResult);
        }
예제 #8
0
            public B()
            {
                Console.WriteLine("B::B()");

                objArr        = new BInt[2];
                objArr[0]     = new BInt();
                objArr[0].str = "b objArr 1";
                objArr[1]     = new BInt();
                objArr[1].str = "b objArr 2";
            }
예제 #9
0
        public void BDictSetValue()
        {
            BDict dict = new BDict();

            dict.Add("a", new BInt(0));
            BInt newInt = new BInt(1);
            dict["a"] = newInt;

            Assert.AreEqual(newInt, dict["a"]);
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="B" /> class.
        /// </summary>
        public B()
        {
            Console.WriteLine("Source::Source()");

            ObjArr    = new BInt[2];
            ObjArr[0] = new BInt {
                Str = "b objArr 1"
            };
            ObjArr[1] = new BInt {
                Str = "b objArr 2"
            };
        }
예제 #11
0
        public void EncodeDictionary()
        {
            BDict testDict = new BDict();
            BString testString = new BString("Hello World");
            BInt testInt = new BInt(5);

            testDict.Add("a", testString);
            testDict.Add("b", testInt);

            string test = BencodingUtils.EncodeString(testDict);

            Assert.AreEqual("d1:a11:Hello World1:bi5ee", test);
        }
예제 #12
0
        public void EncodeList()
        {
            BList testList = new BList();
            BString testString = new BString("Hello World");
            BInt testInt = new BInt(5);

            testList.Add(testString);
            testList.Add(testInt);

            string test = BencodingUtils.EncodeString(testList);

            Assert.AreEqual("l11:Hello Worldi5ee", test);
        }
예제 #13
0
        public void OnHandshake(IPeerWireClient peerWireClient, byte[] handshake)
        {
            BDict dict = (BDict)BencodingUtils.Decode(handshake);

            if (dict.ContainsKey("metadata_size"))
            {
                BInt size = (BInt)dict["metadata_size"];
                this._metadataSize = size;
                this._pieceCount   = (Int64)Math.Ceiling((double)this._metadataSize / 16384);
            }

            this.RequestMetaData(peerWireClient);
        }
예제 #14
0
        public void BIntCompare()
        {
            BInt a = new BInt(10);

            BInt b = new BInt(11);
            Assert.AreEqual(-1, a.CompareTo(b));
            Assert.AreEqual(-1, a.CompareTo(b.Value));

            b.Value = 9;
            Assert.AreEqual(1, a.CompareTo(b));
            Assert.AreEqual(1, a.CompareTo(b.Value));

            b.Value = 10;
            Assert.AreEqual(0, a.CompareTo(b));
            Assert.AreEqual(0, a.CompareTo(b.Value));
        }
예제 #15
0
        public void EqualityDict()
        {
            // Make initial dict
            BDict testDict = new BDict();
            BString testString = new BString("Hello World");
            BInt testInt = new BInt(5);

            testDict.Add("a", testString);
            testDict.Add("b", testInt);

            // Make test dict
            BDict testDict2 = new BDict();
            BString testString2 = new BString("Hello World");
            BInt testInt2 = new BInt(5);

            testDict2.Add("a", testString2);
            testDict2.Add("b", testInt2);

            // Test equality recursive
            Assert.AreEqual(testDict, testDict2);

            // Test null dict
            BDict nullDict = null;
            Assert.IsFalse(testDict.Equals(nullDict));

            // Test different counts
            testDict2.Add("c", new BInt(10));
            Assert.IsFalse(testDict.Equals(testDict2));

            // Test different values
            testDict.Add("c", new BInt(9));
            Assert.IsFalse(testDict.Equals(testDict2));

            // Test missing keys
            testDict2.Remove("c");
            testDict2.Add("d", new BInt(9));

            Assert.IsFalse(testDict.Equals(testDict2));
        }
예제 #16
0
        public bool Load(Stream stream)
        {
            _root = BencodingUtils.Decode(stream);
            if (_root == null)
            {
                return(false);
            }

            BDict dictRoot = (_root as BDict);

            if (dictRoot == null)
            {
                return(false);
            }

            if (dictRoot.ContainsKey("announce"))
            {
                Announce = (BString)dictRoot["announce"];
            }

            if (dictRoot.ContainsKey("announce-list"))
            {
                BList announceList = (BList)dictRoot["announce-list"];
                foreach (IBencodingType type in announceList)
                {
                    if (type is BString)
                    {
                        AnnounceList.Add(type as BString);
                    }
                    else
                    {
                        BList list = type as BList;
                        if (list == null)
                        {
                            continue;
                        }

                        BList listType = list;
                        foreach (IBencodingType bencodingType in listType)
                        {
                            BString s = (BString)bencodingType;
                            AnnounceList.Add(s);
                        }
                    }
                }
            }

            if (dictRoot.ContainsKey("comment"))
            {
                Comment = (BString)dictRoot["comment"];
            }

            if (dictRoot.ContainsKey("created by"))
            {
                CreatedBy = (BString)dictRoot["created by"];
            }

            if (dictRoot.ContainsKey("creation date"))
            {
                long ts = (BInt)dictRoot["creation date"];
                CreationDate = new DateTime(1970, 1, 1).AddSeconds(ts);
            }

            if (dictRoot.ContainsKey("info"))
            {
                BDict infoDict = (BDict)dictRoot["info"];

                using (SHA1Managed sha1 = new SHA1Managed())
                {
                    byte[] str = BencodingUtils.EncodeBytes(infoDict);
                    Hash = sha1.ComputeHash(str);
                }

                if (infoDict.ContainsKey("files"))
                {
                    //multi file mode
                    BList fileList = (BList)infoDict["files"];
                    foreach (IBencodingType bencodingType in fileList)
                    {
                        BDict fileDict = (BDict)bencodingType;

                        String filename = string.Empty;
                        Int64  filesize = default(Int64);

                        if (fileDict.ContainsKey("path"))
                        {
                            BList filenameList = (BList)fileDict["path"];
                            foreach (IBencodingType type in filenameList)
                            {
                                filename += (BString)type;
                                filename += "\\";
                            }
                            filename = filename.Trim('\\');
                        }

                        if (fileDict.ContainsKey("length"))
                        {
                            filesize = (BInt)fileDict["length"];
                        }

                        Files.Add(filename, filesize);
                    }
                }

                if (infoDict.ContainsKey("name"))
                {
                    Name = (BString)infoDict["name"];
                    if (Files.Count == 0 && infoDict.ContainsKey("length"))
                    {
                        Files.Add(Name, (BInt)infoDict["length"]);
                    }
                }

                if (infoDict.ContainsKey("private"))
                {
                    BInt isPrivate = (BInt)infoDict["private"];
                    Private = isPrivate != 0;
                }

                if (infoDict.ContainsKey("pieces"))
                {
                    BString pieces = (BString)infoDict["pieces"];
                    for (int x = 0; x < pieces.ByteValue.Length; x += 20)
                    {
                        byte[] hash = pieces.ByteValue.GetBytes(x, 20);
                        PieceHashes.Add(hash);
                    }
                }

                if (infoDict.ContainsKey("piece length"))
                {
                    PieceSize = (BInt)infoDict["piece length"];
                }
            }

            return(true);
        }
예제 #17
0
        public void BIntHashcode()
        {
            BInt a = new BInt(10);

            Assert.AreEqual((10L).GetHashCode(), a.GetHashCode());
        }
예제 #18
0
        public void BListSetValue()
        {
            BList list = new BList();

            list.Add(new BInt(0));
            BInt newInt = new BInt(1);
            list[0] = newInt;

            Assert.AreEqual(newInt, list[0]);
        }
예제 #19
0
            public B()
            {
                Console.WriteLine("B::B()");

                objArr = new BInt[2];
                objArr[0] = new BInt();
                objArr[0].str = "b objArr 1";
                objArr[1] = new BInt();
                objArr[1].str = "b objArr 2";
            }
예제 #20
0
        public void OnGet(string we = "")
        {
            AMessage = "got";

            if (we != "")
            {
                SqlCommand cmd = new SqlCommand();
                whichEntity = we;
                Debug.WriteLine("Entity: " + whichEntity);
                database.connect();
                dbCommand = "SELECT * FROM " + whichEntity;

                cmd.Connection  = database.Connection;
                cmd.CommandText = dbCommand;
                reader          = cmd.ExecuteReader();
                //IDataRecord record;
                AInt = reader.FieldCount;
                string output = "";
                for (int i = 0; i < AInt; i++)
                {
                    ColumnNames.Add(reader.GetName(i));
                    output += reader.GetName(i);
                }
                Debug.WriteLine(output);
                output = "";
                int j = 0;
                while (reader.Read())
                {
                    Results.Add(new List <string>());
                    for (int i = 0; i < AInt; i++)
                    {
                        Results[j].Add(reader[i].ToString());
                    }
                    j++;
                    output += '\n';
                }
                Debug.WriteLine("Results count: " + Results.Count());
                database.disconnect();
                cmd.Dispose();
                if (whichEntity != "PURCHASE_INFO")
                {
                    database.connect();
                    if (whichEntity != "PURCHASE")
                    {
                        dbCommand = "SELECT MAX(ID) FROM " + whichEntity;
                    }
                    else
                    {
                        dbCommand = "SELECT MAX(Receipt) FROM " + whichEntity;
                    }
                    cmd             = new SqlCommand();
                    cmd.Connection  = database.Connection;
                    cmd.CommandText = dbCommand;
                    Debug.WriteLine(dbCommand);
                    newID = (int)cmd.ExecuteScalar() + 1;
                    Debug.WriteLine(newID.ToString());
                    database.disconnect();
                }
            }
            if (we == "PURCHASE")
            {
                // load purchase_info and pay_type tables
                //dbCommand = "SELECT * FROM PURCHASE_INFO";
                // do some stuff
                //dbCommand = "SELECT * FROM PAY_TYPE";
                // do some stuff
            }
            else if (we == "PURCHASE_INFO")
            {
                SqlCommand cmd = new SqlCommand();
                database.connect();
                cmd.Connection  = database.Connection;
                dbCommand       = "SELECT * FROM PURCHASE";
                cmd.CommandText = dbCommand;
                reader          = cmd.ExecuteReader();
                BInt            = reader.FieldCount;
                Debug.WriteLine("BInt: " + BInt.ToString());
                for (int i = 0; i < BInt; i++)
                {
                    ColumnNames2.Add(reader.GetName(i));
                }
                int j = 0;
                while (reader.Read())
                {
                    Results2.Add(new List <string>());
                    for (int i = 0; i < BInt; i++)
                    {
                        Results2[j].Add(reader[i].ToString());
                    }
                    j++;
                }
                Debug.WriteLine("Results2 count: " + Results2.Count().ToString() + " j: " + j.ToString());
            }
            else if (we == "EMPLOYEE")
            {
                // load title type table
                //dbCommand = "SELECT * FROM TITLE_TYPE";
            }
            else if (we == "STORE")
            {
                // load store type table
                //dbCommand = "SELECT * FROM STORE_TYPE";
            }
        }
예제 #21
0
        public void EqualityNulls()
        {
            object testObjectNull = null;

            BInt testInt = new BInt(5);
            BInt testIntNull = null;

            Assert.IsFalse(testInt.Equals(testIntNull));
            Assert.IsFalse(testInt.Equals(testObjectNull));

            BString testString = new BString("");
            BString testStringNull = null;
            string testStringNull2 = null;

            Assert.IsFalse(testString.Equals(testStringNull));
            Assert.IsFalse(testString.Equals(testStringNull2));
            Assert.IsFalse(testString.Equals(testObjectNull));
        }
예제 #22
0
        public static IBencodingType Decode(BinaryReader inputStream, ref int bytesConsumed)
        {
            IBencodingType returnValue = null;

            char next = (char)inputStream.PeekChar();

            switch (next)
            {
                case 'i':
                    // Integer
                    returnValue = new BInt(0);
                    break;

                case 'l':
                    // List
                    returnValue = new BList();
                    break;

                case 'd':
                    // Dictionary
                    returnValue = new BDict();
                    break;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    // String
                    returnValue = new BString();
                    break;
            }

            return returnValue.Decode(inputStream, ref bytesConsumed);
        }