Пример #1
0
        /// <summary>
        /// Generates a byte array out of the given dataFormat and
        /// the given data.
        /// </summary>
        /// <returns>The byte array.</returns>
        public byte[] ToByteArray()
        {
            MemoryStream memory = new MemoryStream();
            PacketStream stream = new PacketStream(memory);
            int reallength = DataLength + 4;
            byte[] array = new byte[reallength];

            // Data length
            stream.Write(DataLength);
            // Ooops... Forgot the type over here :)
            stream.Write((Int32)type);
            foreach (Object obj in data)
            {
                if (obj is String)
                { // Write string
                    stream.Write((String)obj);
                }
                else if ( obj is Int32 )
                { // Write the object
                    stream.Write((Int32)obj);
                }
                else if (obj is Byte)
                { // Write byte.
                    stream.Write((Byte)obj);
                }
                else if (obj is String[])
                { // Write the string array.
                    stream.Write((String[])obj);
                }
                else if (obj is Int32[])
                { // Write each item.
                    stream.Write((Int32[])obj);
                }
            }
            // Copy to our array, including the four needed
            // Since we do not count the "Length" field in our
            // calculation.
            Array.Copy(memory.GetBuffer(), array, reallength);
            // And return it.
            return array;
        }
        /// <summary>
        /// Parses the data format
        /// </summary>
        /// <param name="array"></param>
        public override void Parse(byte[] array)
        {
            MemoryStream memory = new MemoryStream(array);
            PacketStream stream = new PacketStream(memory);

            try
            {
                Int32 statcount = 0;
                String[] users;
                Int32[] status;
                List<Int32> avgspeed = new List<Int32>();
                List<Int32> something = new List<Int32>();
                List<Int32> cntfiles = new List<Int32>();
                List<Int32> cntdirs = new List<Int32>();
                Int32[] cntslotsfull;

                // The first thing is the room we have joined.
                room = stream.ReadString();
                // Second: Users in this room.
                users = stream.ReadStrings();
                // Third: Status of each user
                status = stream.ReadInts();
                // Fourth: Read number of statistics
                statcount = stream.ReadInt();
                // Read each
                while (statcount > 0)
                {
                    Int32 read = 0;

                    // Read average speed.
                    read = stream.ReadInt();
                    avgspeed.Add(read);
                    // Read something
                    read = stream.ReadInt();
                    something.Add(read);
                    // Read number of files.
                    read = stream.ReadInt();
                    cntfiles.Add(read);
                    // Read Number of directories
                    read = stream.ReadInt();
                    cntdirs.Add(read);
                    --statcount;
                }
                // Last but not least: read states.
                cntslotsfull = stream.ReadInts();
                // Now assemble the users
                for (int i = 0; i < users.Length; ++i)
                {
                    User user = new User(users[i]);
                    // Now apply the rest of the data.
                    if (i < status.Length)
                    { // Status of the user
                        user.Status = (UserStatus)status[i];
                    }
                    if (i < avgspeed.Count)
                    { // Average speed
                        user.AverageSpeed = avgspeed[i];
                    }
                    if (i < something.Count)
                    { // Unkown
                        user.Unkown = something[i];
                    }
                    if (i < cntfiles.Count)
                    { // Number of files.
                        user.Files = cntfiles[i];
                    }
                    if (i < cntdirs.Count)
                    { // Number of directories.
                        user.Directories = cntdirs[i];
                    }
                    if (i < cntslotsfull.Length)
                    { // Number of full slots.
                        user.FullSlots = cntslotsfull[i];
                    }
                    // And finally add him to our list
                    this.users.Add(user);
                }
            }
            catch (Exception)
            { // There was an error.
            }
        }
Пример #3
0
        /// <summary>
        /// Parses the input data and extracts all needed
        /// information out of it.
        /// </summary>
        /// <param name="array">Array to read from</param>
        public virtual void Parse(byte[] array)
        {
            MemoryStream stream = new MemoryStream(array);
            PacketStream reader = new PacketStream(stream);

            try
            {
                Int32 index = 0;
                foreach (Type type in dataFormat)
                { // Read all information.
                    Object value = null;
                    if (type.Equals(typeof(Byte)))
                    { // Read a byte
                        value = reader.ReadByte();
                    }
                    else if (type.Equals(typeof(Int32)))
                    { // Read an int
                        value = reader.ReadInt();
                    }
                    else if (type.Equals(typeof(String)))
                    { // Read the string
                        value = reader.ReadString();
                    }
                    else if (type.Equals(typeof(String[])))
                    { // String array... where can I find the count?
                        value = reader.ReadStrings();
                    }
                    else if (type.Equals(typeof(Int32[])))
                    { // Read all integers.
                        value = reader.ReadInts();
                    }
                    if (value != null)
                    { // Set value
                        data[index] = value;
                    }
                    ++index;
                }
            }
            catch (Exception e)
            { // Throw some nasty exception here.
                new InvalidPacketException(e.Message);
            }
        }