public void ReadWriteLongSet()
        {
            var writer = new BufferValueWriter (new byte[1]);

            for (int i = 0; i < 20480; ++i)
                writer.WriteInt32(i);

            writer.Flush();

            var reader = new BufferValueReader (writer.Buffer);
            for (int i = 0; i < 20480; ++i)
                Assert.AreEqual(i, reader.ReadInt32());
        }
Exemple #2
0
        public void ReadWriteLongSet()
        {
            var writer = new BufferValueWriter(new byte[1]);

            for (int i = 0; i < 20480; ++i)
            {
                writer.WriteInt32(i);
            }

            writer.Flush();

            var reader = new BufferValueReader(writer.Buffer);

            for (int i = 0; i < 20480; ++i)
            {
                Assert.AreEqual(i, reader.ReadInt32());
            }
        }
        protected void EncryptMessage(BufferValueWriter writer, ref int headerLength)
        {
            AesManaged am = AES;

            if (am == null)
            {
                return;
            }

            ICryptoTransform encryptor = null;

            byte[] iv = null;
            lock (am)
            {
                am.GenerateIV();
                iv        = am.IV;
                encryptor = am.CreateEncryptor();
            }

            const int workingHeaderLength = LengthOffset + sizeof(int);             // right after length

            int r = ((writer.Length - workingHeaderLength) % encryptor.OutputBlockSize);

            if (r != 0)
            {
                writer.Pad(encryptor.OutputBlockSize - r);
            }

            byte[] payload = encryptor.TransformFinalBlock(writer.Buffer, workingHeaderLength, writer.Length - workingHeaderLength);

            writer.Length = workingHeaderLength;
            writer.InsertBytes(workingHeaderLength, BitConverter.GetBytes(iv.Length), 0, sizeof(int));
            writer.InsertBytes(workingHeaderLength + sizeof(int), iv, 0, iv.Length);
            writer.WriteInt32(payload.Length);
            writer.InsertBytes(writer.Length, payload, 0, payload.Length);

            headerLength += iv.Length + sizeof(int);
        }
        public unsafe byte[] GetBytes(Message message, out int length, byte[] buffer)
                #endif
        {
            string callCategory = null;

                        #if TRACE
            int c = GetNextCallId();
            callCategory = String.Format("{0} {1}:GetBytes({2},{3})", this.connectionType, c, message, buffer.Length);
                        #endif

            Trace.WriteLineIf(NTrace.TraceVerbose, "Entering", callCategory);

            int messageId = message.Header.MessageId;
            if (message.Header.IsResponse)
            {
                messageId |= ResponseFlag;
            }

            BufferValueWriter writer = new BufferValueWriter(buffer);
            writer.EnsureAdditionalCapacity(15);

            int cid          = (connection != null) ? connection.ConnectionId : 0;
            int headerLength = BaseHeaderLength;

                        #if SAFE
            writer.WriteByte(message.Protocol.id);

            writer.WriteInt32(cid);              // TODO: Change to variable length later

            writer.WriteUInt16(message.MessageType);
            writer.Length += sizeof(int);              // length placeholder

            writer.WriteInt32(messageId);

            if (message.Header.IsResponse)
            {
                writer.WriteInt32(message.Header.ResponseMessageId);
                headerLength += sizeof(int);
            }
                        #else
            fixed(byte *bptr = writer.Buffer)
            {
                *bptr = message.Protocol.id;
                *((int *)(bptr + 1))    = cid;
                *((ushort *)(bptr + 5)) = message.MessageType;
                *((int *)(bptr + 11))   = messageId;

                if (message.Header.IsResponse)
                {
                    *((int *)(bptr + 15)) = message.Header.ResponseMessageId;
                    headerLength         += sizeof(int);
                }
            }

            writer.Extend(headerLength);
                        #endif

            if (this.serializationContext == null)
            {
                if (this.connection != null)
                {
                    this.serializationContext = new SerializationContext(this.connection, this.protocols);
                }
                else
                {
                    this.serializationContext = new SerializationContext(this.protocols);
                }
            }

            message.WritePayload(this.serializationContext, writer);

            if (message.Encrypted)
            {
                EncryptMessage(writer, ref headerLength);
            }
            else if (message.Authenticated)
            {
                                #if SAFE
                for (int i = LengthOffset; i < LengthOffset + sizeof(int); ++i)
                {
                    writer.Buffer[i] = 0;
                }
                                #else
                fixed(byte *mptr = writer.Buffer)
                * ((int *)(mptr + LengthOffset)) = 0;
                                #endif

                SignMessage(message, this.signingHashAlgorithm, writer);
            }

            byte[] rawMessage = writer.Buffer;
            length = writer.Length;

                        #if SAFE
            Buffer.BlockCopy(BitConverter.GetBytes(length), 0, rawMessage, LengthOffset, sizeof(int));
                        #else
            fixed(byte *mptr = rawMessage)
            * ((int *)(mptr + LengthOffset)) = length;
                        #endif

            Trace.WriteLineIf(NTrace.TraceVerbose, String.Format("Exiting. Length: {0}", length), callCategory);

            return(rawMessage);
        }