Exemple #1
0
		public void SerializeLocal(BinaryWriter writer)
		{
			//Check in sequence of headers.. 
			FlagsByte bFlags = new FlagsByte();
			if (IsUserMsg)
				bFlags.SetOn(FlagsByte.Flag.TRANS);

			object tmpHdr;
			tmpHdr = (Header)headers[(object)(HeaderType.REQUEST_COORELATOR)];
			if (tmpHdr != null)
			{
				RequestCorrelator.HDR corHdr = (RequestCorrelator.HDR)tmpHdr;
				corHdr.SerializeLocal(writer);
				bFlags.SetOn(FlagsByte.Flag.COR);
			}

			tmpHdr = (Header)headers[(object)(HeaderType.TOTAL)];
			if (tmpHdr != null)
			{
				TOTAL.HDR totalHdr = (TOTAL.HDR)tmpHdr;
				totalHdr.SerializeLocal(writer);
				bFlags.SetOn(FlagsByte.Flag.TOTAL);
			}

			tmpHdr = (Header)headers[(object)(HeaderType.TCP)];
			if (tmpHdr != null)
			{
				TcpHeader tcpHdr = (TcpHeader)tmpHdr;
				tcpHdr.SerializeLocal(writer);
				bFlags.SetOn(FlagsByte.Flag.TCP);
			}

			writer.Write(Convert.ToInt16(prio));
			writer.Write(handledAsynchronously);
            writer.Write(arrivalTime.Ticks);
            writer.Write(sendTime.Ticks);
            writer.Write(responseExpected);
            writer.Write(_type);
            if (stream != null)
            {
                writer.Write(length);
                writer.Write(((MemoryStream)stream).GetBuffer(), 0, length);
            }
            else
            {
                int length = buf.Length;
                writer.Write(length);
                writer.Write(buf);
            }
			long curPos = writer.BaseStream.Position;
			writer.BaseStream.Position = 8; //afte 4 bytes of total size and 4 bytes of message size ..here comes the flag.
			writer.Write(bFlags.DataByte);
			writer.BaseStream.Position = curPos;
		}
Exemple #2
0
        internal static byte[] serializeMessage(Message msg)
        {
            int len = 0;
            byte[] buffie;
            FlagsByte flags = new FlagsByte();

            msg.Dest = null;
            msg.Dests = null;

            RequestCorrelator.HDR rqHeader = (RequestCorrelator.HDR)msg.getHeader(HeaderType.REQUEST_COORELATOR);

            if (rqHeader != null)
            {
                rqHeader.serializeFlag = false;
            }

            Stream stmOut = new MemoryStream();
            stmOut.Write(Util.WriteInt32(len), 0, 4);
            stmOut.Write(Util.WriteInt32(len), 0, 4);

            if (msg.IsUserMsg)
            {
                BinaryWriter msgWriter = new BinaryWriter(stmOut, new UTF8Encoding(true));
                flags.SetOn(FlagsByte.Flag.TRANS);
                msgWriter.Write(flags.DataByte);
                msg.SerializeLocal(msgWriter);
            }
            else
            {
                flags.SetOff(FlagsByte.Flag.TRANS);
                stmOut.WriteByte(flags.DataByte);
                CompactBinaryFormatter.Serialize(stmOut, msg, null, false);
            }

            len = (int)stmOut.Position - 4;

            int payloadLength = 0;
            // the user payload size. payload is passed on untill finally send on the socket.
            if (msg.Payload != null)
            {
                for (int i = 0; i < msg.Payload.Length; i++)
                {
                    payloadLength += ((byte[])msg.Payload.GetValue(i)).Length;
                }
                len += payloadLength;
            }

            stmOut.Position = 0;
            stmOut.Write(Util.WriteInt32(len), 0, 4);
            stmOut.Write(Util.WriteInt32(len - 4 - payloadLength), 0, 4);
            stmOut.Position = 0;
            buffie = new byte[len + 4 - payloadLength];
            stmOut.Read(buffie, 0, len + 4 - payloadLength);
            stmOut.Position = 0;

            return buffie;
        }