示例#1
0
        public void WriteFromDict <TKey, TValue> (IDictionary <TKey, TValue> val)
        {
            long origPos = stream.Position;

            Write((uint)0);

            WritePad(8);

            long startPos = stream.Position;

            TypeWriter <TKey>   keyWriter   = TypeImplementer.GetTypeWriter <TKey> ();
            TypeWriter <TValue> valueWriter = TypeImplementer.GetTypeWriter <TValue> ();

            foreach (KeyValuePair <TKey, TValue> entry in val)
            {
                WritePad(8);
                keyWriter(this, entry.Key);
                valueWriter(this, entry.Value);
            }

            long endPos = stream.Position;
            uint ln     = (uint)(endPos - startPos);

            stream.Position = origPos;

            if (ln > Protocol.MaxArrayLength)
            {
                throw new Exception("Dict length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
            }

            Write(ln);
            stream.Position = endPos;
        }
示例#2
0
        public void WriteStructure <T> (T value)
        {
            TypeWriter <T> tWriter = TypeImplementer.GetTypeWriter <T> ();

            tWriter(this, value);
        }
示例#3
0
        //this requires a seekable stream for now
        public unsafe void WriteArray <T> (T[] val)
        {
            Type elemType = typeof(T);

            if (elemType == typeof(byte))
            {
                if (val.Length > Protocol.MaxArrayLength)
                {
                    throw new Exception("Array length " + val.Length + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
                }

                Write((uint)val.Length);
                stream.Write((byte[])(object)val, 0, val.Length);
                return;
            }

            if (elemType.IsEnum)
            {
                elemType = Enum.GetUnderlyingType(elemType);
            }

            Signature sigElem   = Signature.GetSig(elemType);
            int       fixedSize = 0;

            if (endianness == Connection.NativeEndianness && elemType.IsValueType && !sigElem.IsStruct && sigElem.GetFixedSize(ref fixedSize))
            {
                int byteLength = fixedSize * val.Length;
                if (byteLength > Protocol.MaxArrayLength)
                {
                    throw new Exception("Array length " + byteLength + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
                }
                Write((uint)byteLength);
                WritePad(sigElem.Alignment);

                GCHandle valHandle = GCHandle.Alloc(val, GCHandleType.Pinned);
                IntPtr   p         = valHandle.AddrOfPinnedObject();
                byte[]   data      = new byte[byteLength];
                byte *   bp        = (byte *)p;
                for (int i = 0; i != byteLength; i++)
                {
                    data[i] = bp[i];
                }
                stream.Write(data, 0, data.Length);
                valHandle.Free();
                return;
            }

            long origPos = stream.Position;

            Write((uint)0);

            //advance to the alignment of the element
            WritePad(sigElem.Alignment);

            long startPos = stream.Position;

            TypeWriter <T> tWriter = TypeImplementer.GetTypeWriter <T> ();

            foreach (T elem in val)
            {
                tWriter(this, elem);
            }

            long endPos = stream.Position;
            uint ln     = (uint)(endPos - startPos);

            stream.Position = origPos;

            if (ln > Protocol.MaxArrayLength)
            {
                throw new Exception("Array length " + ln + " exceeds maximum allowed " + Protocol.MaxArrayLength + " bytes");
            }

            Write(ln);
            stream.Position = endPos;
        }