Ejemplo n.º 1
0
        /// <inheritdoc />
        public void Deserialize(Stream inputStream)
        {
            longItems   = new Dictionary <PacketHeaderLongItems, long>();
            stringItems = new Dictionary <PacketHeaderStringItems, string>();

            byte[] longItemsLengthData = new byte[sizeof(int)]; inputStream.Read(longItemsLengthData, 0, sizeof(int));
            int    longItemsLength     = BitConverter.ToInt32(longItemsLengthData, 0);

            if (longItemsLength * (sizeof(int) + sizeof(long)) > inputStream.Length)
            {
                throw new SerialisationException("Error deserializing packet header. Number of long items was too large to be present in the input stream." +
                                                 " This error is typically thrown because a non NetworkComms.Net peer attempted to communicate. If this is desirable please consider using an unmanaged connection.");
            }

            for (int i = 0; i < longItemsLength; i++)
            {
                byte[] keyData            = new byte[sizeof(int)]; inputStream.Read(keyData, 0, sizeof(int));
                PacketHeaderLongItems key = (PacketHeaderLongItems)BitConverter.ToInt32(keyData, 0);

                byte[] valData = new byte[sizeof(long)]; inputStream.Read(valData, 0, sizeof(long));
                long   val     = BitConverter.ToInt64(valData, 0);

                longItems.Add(key, val);
            }

            byte[] stringItemsLengthData = new byte[sizeof(int)]; inputStream.Read(stringItemsLengthData, 0, sizeof(int));
            int    stringItemsLength     = BitConverter.ToInt32(stringItemsLengthData, 0);

            if (stringItemsLength * (2 * sizeof(int)) > inputStream.Length)
            {
                throw new SerialisationException("Error deserializing packet header. Number of string items was too large to be present in the input stream." +
                                                 " This error is typically thrown because a non NetworkComms.Net peer attempted to communicate. If this is desirable please consider using an unmanaged connection.");
            }

            for (int i = 0; i < stringItemsLength; i++)
            {
                byte[] keyData = new byte[sizeof(int)]; inputStream.Read(keyData, 0, sizeof(int));
                PacketHeaderStringItems key = (PacketHeaderStringItems)BitConverter.ToInt32(keyData, 0);

                byte[] valLengthData = new byte[sizeof(int)]; inputStream.Read(valLengthData, 0, sizeof(int));
                int    valLength     = BitConverter.ToInt32(valLengthData, 0);

                if (valLength > inputStream.Length)
                {
                    throw new SerialisationException("Error deserializing packet header. Length string item was too large to be present in the input stream." +
                                                     " This error is typically thrown because a non NetworkComms.Net peer attempted to communicate. If this is desirable please consider using an unmanaged connection.");
                }

                byte[] valData = new byte[valLength]; inputStream.Read(valData, 0, valData.Length);
                string val     = new String(Encoding.UTF8.GetChars(valData));

                stringItems.Add(key, val);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Set a long option with the provided value.
 /// </summary>
 /// <param name="option">The option to set</param>
 /// <param name="Value">The option value</param>
 public void SetOption(PacketHeaderLongItems option, long Value)
 {
     longItems[option] = Value;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Get a long option.
 /// </summary>
 /// <param name="option">The option to get</param>
 /// <returns>The requested long option</returns>
 public long GetOption(PacketHeaderLongItems option)
 {
     return(longItems[option]);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Check if a long option has been set.
 /// </summary>
 /// <param name="option">The long option to be checked.</param>
 /// <returns>Returns true if the provided long option has been set.</returns>
 public bool ContainsOption(PacketHeaderLongItems option)
 {
     return(longItems.ContainsKey(option));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Set a long option with the provided value.
 /// </summary>
 /// <param name="option">The option to set</param>
 /// <param name="Value">The option value</param>
 public void SetOption(PacketHeaderLongItems option, long Value)
 {
     longItems[option] = Value;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Get a long option.
 /// </summary>
 /// <param name="option">The option to get</param>
 /// <returns>The requested long option</returns>
 public long GetOption(PacketHeaderLongItems option)
 {
     return longItems[option];
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Check if a long option has been set.
 /// </summary>
 /// <param name="option">The long option to be checked.</param>
 /// <returns>Returns true if the provided long option has been set.</returns>
 public bool ContainsOption(PacketHeaderLongItems option)
 {
     return longItems.ContainsKey(option);
 }