private int ReadInt(IO.Stream stream) { int p1 = stream.ReadByte(); int p2 = stream.ReadByte(); int p3 = stream.ReadByte(); int p4 = stream.ReadByte(); int final = 0; final |= (p1 << 24); final |= (p2 << 16); final |= (p3 << 8); final |= (p4); return final; // byte[] b = new byte[4]; // stream.Read(b, 0, 4); // return (int)System.BitConverter.ToUInt32(b, 0); }
public void ReadMessageHeader(IO.Stream stream, out int length, out PeerMessage type) { length = ReadInt(stream); if (length > 0) type = (PeerMessage)stream.ReadByte(); else type = PeerMessage.KeepAlive; }
/// <summary>Constructs a string from the bEncoded stream</summary> /// <param name="istream">Stream to construct string from</param> public String(IO.Stream istream, int firstchar) { if (istream.CanSeek) this.position = (int)istream.Position - 1; string numstr = new string((char)firstchar, 1); while (true) { int c = istream.ReadByte(); if (c != ':') numstr += (char)c; else break; } int length = System.Int32.Parse(numstr); if (length < 0) throw new System.Exception("Invalid string length"); this.data = new byte[length]; istream.Read(this.data, 0, length); if (istream.CanSeek) this.length = (int)(istream.Position - this.position); // System.Diagnostics.Debugger.Log(0, "BEncode", "String: " + this.ToString() + "\n"); }
/// <summary> /// Returns the next element in the stream. /// </summary> /// <param name="istream">bEncoded stream to extract element from</param> /// <returns>bEncode element</returns> public static Element NextElement(IO.Stream istream) { return NextElement(istream, istream.ReadByte()); }
/// <summary> /// Constructs a dictionary /// </summary> /// <param name="istream">Stream to construct dictionary from</param> public Dictionary(IO.Stream istream) { if (istream.CanSeek) this.position = (int)istream.Position - 1; int c; while ((c = istream.ReadByte()) != 'e') { BEncode.String key = new BEncode.String(istream, c); // keys are always strings string strkey = key; Element element = BEncode.NextElement(istream); if (!this.map.Contains(key)) this.map.Add(key, element); } if (istream.CanSeek) this.length = (int)istream.Position - this.position; }
/// <summary>Constructs an integer from the bEncoded stream</summary> /// <param name="istream">Stream to construct integer from</param> public List(IO.Stream istream) { if (istream.CanSeek) this.position = (int)istream.Position - 1; int c; while ((c = istream.ReadByte()) != 'e') { BEncode.Element element = BEncode.NextElement(istream, c); string el = element.ToString(); this.list.Add(element); } if (istream.CanSeek) this.length = (int)istream.Position - this.position; }
/// <summary>Constructs an integer from the bEncoded stream</summary> /// <param name="istream">Stream to construct integer from</param> internal Integer(IO.Stream istream) { if (istream.CanSeek) this.position = (int)istream.Position - 1; string numstr = ""; char c; while ((c = (char)istream.ReadByte()) != 'e') { numstr += c; } this.integer = System.Int32.Parse(numstr); if (istream.CanSeek) this.length = (int)istream.Position - this.position; }