示例#1
0
        public ProtoMessage(byte[] protoMessageData)
        {
            this.Values = new Dictionary<String, List<Object>>();

            BitStream bitStream = new BitStream(protoMessageData);

            while ((bitStream.PositionInBytes) < bitStream.LengthInBytes)
            {
                int key = bitStream.ReadVarint32();

                int fieldNumber = key >> 3;
                int wireType = key & 7;

                object value = null;

                switch (wireType)
                {
                    case 0:
                        {
                            value = bitStream.ReadVarint32();

                            break;
                        }
                    case 1:
                        {
                            Console.WriteLine("ProtoMessage WireType 1");

                            bitStream.SeekBytes(0, SeekOrigin.End);

                            break;
                        }
                    case 2:
                        {
                            int length = bitStream.ReadVarint32();

                            byte[] buffer = bitStream.ReadBytes(length);

                            value = buffer;

                            break;
                        }
                    case 5:
                        {
                            value = bitStream.ReadFloat();

                            break;
                        }

                    default:
                        {
                            Console.WriteLine("Unknown ProtoMessage WireType: {0}", wireType);

                            bitStream.SeekBytes(0, SeekOrigin.End);

                            return;
                        }
                }

                if (value != null)
                {
                    List<Object> fieldValues;

                    if (this.Values.TryGetValue(fieldNumber.ToString(), out fieldValues))
                    {
                        fieldValues.Add(value);
                    }
                    else
                    {
                        fieldValues = new List<Object>();

                        fieldValues.Add(value);

                        this.Values.Add(fieldNumber.ToString(), fieldValues);
                    }
                }
            }
        }