WriteLength() public method

public WriteLength ( bool recoverStreamPosition ) : void
recoverStreamPosition bool
return void
コード例 #1
0
        public void Serialize(Stream dest, Exception value)
        {
            if (dest == null)
                throw new ArgumentNullException("dest");
            if (value == null)
                throw new ArgumentNullException("value");

            var type = value.GetType();
            int typeAlias = _data.TypeTable.GetAlias(type);
            var typeSerializable = _data.MessageSerializer.CanSerialize(type);

            dest.WriteByte((byte)((typeAlias != 0 ? 1 : 0) | (typeSerializable ? 2 : 0)));

            if (typeAlias != 0)
                dest.Write32BitEncodedInt(typeAlias);
            else
                dest.WriteString(type.FullName);

            if (typeSerializable)
            {
                var lengthMarker = new StreamLengthMarker(dest, true);
                _data.MessageSerializer.Serialize(dest, value);
                lengthMarker.WriteLength(true);
            }
        }
コード例 #2
0
        public void Serialize(Stream dest, Exception value)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            var type             = value.GetType();
            int typeAlias        = _data.TypeTable.GetAlias(type);
            var typeSerializable = _data.MessageSerializer.CanSerialize(type);

            dest.WriteByte((byte)((typeAlias != 0 ? 1 : 0) | (typeSerializable ? 2 : 0)));

            if (typeAlias != 0)
            {
                dest.Write32BitEncodedInt(typeAlias);
            }
            else
            {
                dest.WriteString(type.FullName);
            }

            if (typeSerializable)
            {
                var lengthMarker = new StreamLengthMarker(dest, true);
                _data.MessageSerializer.Serialize(dest, value);
                lengthMarker.WriteLength(true);
            }
        }
コード例 #3
0
        // +--------+--------+--+----------+-----------+-------------+-----------+------------+-------+
        // | LEN(4) | CRC(4) |H1| ID (1~6) | AID (1~6) | M_SIG (1~6) | M_LEN (4) | M_DATA (~) | E (~) |
        // +--------+--------+--+----------+-----------+-------------+-----------+------------+-------+
        // H=[ME....TT] T=Type, M=Message?, E=Exception?
        // ID=RequestId, AID=ActorId, M=Message, E=Exception

        public void Serialize(Stream stream, object packet)
        {
            var p = (Packet)packet;

            // Jump 8 Bytes for writing Length | Checksum
            var packetLengthMarker = new StreamLengthMarker(stream, false);

            stream.Seek(8, SeekOrigin.Current);

            // Write Packet Header
            var header = (byte)((byte)(p.Type) |
                                (byte)(p.Message != null ? 0x80 : 0) |
                                (byte)(p.Exception != null ? 0x40 : 0));

            stream.WriteByte(header);
            stream.Write7BitEncodedInt(p.ActorId);
            stream.Write7BitEncodedInt(p.RequestId);

            // Write Message
            if (p.Message != null)
            {
                if (p.Type == PacketType.System)
                {
                    // System message: Always string.
                    stream.WriteString((string)p.Message);
                }
                else
                {
                    // User message: Length, Signature, and Data
                    var messageTypeAlias = _data.TypeTable.GetAlias(p.Message.GetType());
                    stream.Write7BitEncodedInt(messageTypeAlias);
                    var messageLengthMarker = new StreamLengthMarker(stream, true);
                    _data.MessageSerializer.Serialize(stream, p.Message);
                    messageLengthMarker.WriteLength(true);
                }
            }

            // Write Exception
            if (p.Exception != null)
            {
                _exceptionSerializer.Serialize(stream, p.Exception);
            }

            // Write Length
            packetLengthMarker.WriteLength(false);

            // Encrypt and Calc Checksum
            ArraySegment <byte> s0, s1;

            GetBuffers(stream, (int)packetLengthMarker.StartPosition + 8, packetLengthMarker.Length - 4,
                       out s0, out s1);
            var ctx = new EncryptContext {
                Key = _serializeWrapKey
            };

            Encrypt(s0.Array, s0.Offset, s0.Array, s0.Offset, s0.Count, ref ctx);
            Encrypt(s1.Array, s1.Offset, s1.Array, s1.Offset, s1.Count, ref ctx);
            if (_serializeWrapKey != 0)
            {
                _serializeWrapKey += 1;
                if (_serializeWrapKey == 0)
                {
                    _serializeWrapKey = 1;
                }
            }

            // Write Checksum
            var hashBytes = BitConverter.GetBytes(ctx.Hash);

            stream.Write(hashBytes, 0, hashBytes.Length);

            // End of stream, again.
            stream.Seek(packetLengthMarker.EndPosition, SeekOrigin.Begin);

            // Pending WrapKey
            if (_serializeWrapPendingKey != 0)
            {
                _serializeWrapKey        = _serializeWrapPendingKey;
                _serializeWrapPendingKey = 0;
            }
        }
コード例 #4
0
        // +--------+--------+--+----------+-----------+-------------+-----------+------------+-------+
        // | LEN(4) | CRC(4) |H1| ID (1~6) | AID (1~6) | M_SIG (1~6) | M_LEN (4) | M_DATA (~) | E (~) |
        // +--------+--------+--+----------+-----------+-------------+-----------+------------+-------+
        // H=[ME....TT] T=Type, M=Message?, E=Exception?
        // ID=RequestId, AID=ActorId, M=Message, E=Exception

        public void Serialize(Stream stream, object packet)
        {
            var p = (Packet)packet;

            // Jump 8 Bytes for writing Length | Checksum
            var packetLengthMarker = new StreamLengthMarker(stream, false);
            stream.Seek(8, SeekOrigin.Current);

            // Write Packet Header
            var header = (byte)((byte)(p.Type) |
                                (byte)(p.Message != null ? 0x80 : 0) |
                                (byte)(p.Exception != null ? 0x40 : 0));
            stream.WriteByte(header);
            stream.Write7BitEncodedInt(p.ActorId);
            stream.Write7BitEncodedInt(p.RequestId);

            // Write Message
            if (p.Message != null)
            {
                if (p.Type == PacketType.System)
                {
                    // System message: Always string.
                    stream.WriteString((string)p.Message);
                }
                else
                {
                    // User message: Length, Signature, and Data
                    var messageTypeAlias = _data.TypeTable.GetAlias(p.Message.GetType());
                    stream.Write7BitEncodedInt(messageTypeAlias);
                    var messageLengthMarker = new StreamLengthMarker(stream, true);
                    _data.MessageSerializer.Serialize(stream, p.Message);
                    messageLengthMarker.WriteLength(true);
                }
            }

            // Write Exception
            if (p.Exception != null)
            {
                _exceptionSerializer.Serialize(stream, p.Exception);
            }

            // Write Length
            packetLengthMarker.WriteLength(false);

            // Encrypt and Calc Checksum
            ArraySegment<byte> s0, s1;
            GetBuffers(stream, (int)packetLengthMarker.StartPosition + 8, packetLengthMarker.Length - 4,
                       out s0, out s1);
            var ctx = new EncryptContext { Key = _serializeWrapKey };
            Encrypt(s0.Array, s0.Offset, s0.Array, s0.Offset, s0.Count, ref ctx);
            Encrypt(s1.Array, s1.Offset, s1.Array, s1.Offset, s1.Count, ref ctx);
            if (_serializeWrapKey != 0)
            {
                _serializeWrapKey += 1;
                if (_serializeWrapKey == 0)
                    _serializeWrapKey = 1;
            }

            // Write Checksum
            var hashBytes = BitConverter.GetBytes(ctx.Hash);
            stream.Write(hashBytes, 0, hashBytes.Length);

            // End of stream, again.
            stream.Seek(packetLengthMarker.EndPosition, SeekOrigin.Begin);

            // Pending WrapKey
            if (_serializeWrapPendingKey != 0)
            {
                _serializeWrapKey = _serializeWrapPendingKey;
                _serializeWrapPendingKey = 0;
            }
        }