private Message DeserializeMessage(int length, byte[] data) { int headerLength = BitConverter.ToInt32(data, 0); int bodyLength = BitConverter.ToInt32(data, 4); Assert.Equal <int>(length, headerLength + bodyLength + 8); //Serialized lengths are incorrect byte[] header = new byte[headerLength]; Array.Copy(data, 8, header, 0, headerLength); byte[] body = new byte[bodyLength]; Array.Copy(data, 8 + headerLength, body, 0, bodyLength); var headerList = new List <ArraySegment <byte> >(); headerList.Add(new ArraySegment <byte>(header)); var bodyList = new List <ArraySegment <byte> >(); bodyList.Add(new ArraySegment <byte>(body)); var context = new DeserializationContext(this.fixture.SerializationManager) { StreamReader = new BinaryTokenStreamReader(headerList) }; var deserializedMessage = new Message { Headers = SerializationManager.DeserializeMessageHeaders(context) }; deserializedMessage.SetBodyBytes(bodyList); return(deserializedMessage); }
// Initializes body and header but does not take ownership of byte. // Caller must clean up bytes public Message(List <ArraySegment <byte> > header) { var context = new DeserializationContext(RuntimeClient.Current.InternalGrainFactory) { StreamReader = new BinaryTokenStreamReader(header) }; Headers = SerializationManager.DeserializeMessageHeaders(context); }
// Initializes body and header but does not take ownership of byte. // Caller must clean up bytes public Message(List <ArraySegment <byte> > header) { var context = new DeserializationContext { StreamReader = new BinaryTokenStreamReader(header) }; Headers = SerializationManager.DeserializeMessageHeaders(context); }
public Message(List <ArraySegment <byte> > header, List <ArraySegment <byte> > body) { metadata = new Dictionary <string, object>(); var input = new BinaryTokenStreamReader(header); headers = SerializationManager.DeserializeMessageHeaders(input); BufferPool.GlobalPool.Release(header); bodyBytes = body; bodyObject = null; headerBytes = null; }
// Initializes body and header but does not take ownership of byte. // Caller must clean up bytes public Message(List <ArraySegment <byte> > header, List <ArraySegment <byte> > body, bool deserializeBody = false) { var input = new BinaryTokenStreamReader(header); Headers = SerializationManager.DeserializeMessageHeaders(input); if (deserializeBody) { bodyObject = DeserializeBody(body); } else { bodyBytes = body; } }
private void RunTest(int numItems) { InvokeMethodRequest request = new InvokeMethodRequest(0, 2, 0, null); Message resp = this.messageFactory.CreateMessage(request, InvokeMethodOptions.None); resp.Id = new CorrelationId(); resp.SendingSilo = SiloAddress.New(new IPEndPoint(IPAddress.Loopback, 200), 0); resp.TargetSilo = SiloAddress.New(new IPEndPoint(IPAddress.Loopback, 300), 0); resp.SendingGrain = GrainId.NewId(); resp.TargetGrain = GrainId.NewId(); resp.IsAlwaysInterleave = true; Assert.True(resp.IsUsingInterfaceVersions); List <object> requestBody = new List <object>(); for (int k = 0; k < numItems; k++) { requestBody.Add(k + ": test line"); } resp.BodyObject = requestBody; string s = resp.ToString(); output.WriteLine(s); int dummy; var serialized = resp.Serialize(this.fixture.SerializationManager, out dummy, out dummy); int length = serialized.Sum <ArraySegment <byte> >(x => x.Count); byte[] data = new byte[length]; int n = 0; foreach (var buffer in serialized) { Array.Copy(buffer.Array, buffer.Offset, data, n, buffer.Count); n += buffer.Count; } resp.ReleaseBodyAndHeaderBuffers(); int headerLength = BitConverter.ToInt32(data, 0); int bodyLength = BitConverter.ToInt32(data, 4); Assert.Equal <int>(length, headerLength + bodyLength + 8); //Serialized lengths are incorrect byte[] header = new byte[headerLength]; Array.Copy(data, 8, header, 0, headerLength); byte[] body = new byte[bodyLength]; Array.Copy(data, 8 + headerLength, body, 0, bodyLength); var headerList = new List <ArraySegment <byte> >(); headerList.Add(new ArraySegment <byte>(header)); var bodyList = new List <ArraySegment <byte> >(); bodyList.Add(new ArraySegment <byte>(body)); var context = new DeserializationContext(this.fixture.SerializationManager) { StreamReader = new BinaryTokenStreamReader(headerList) }; var resp1 = new Message { Headers = SerializationManager.DeserializeMessageHeaders(context) }; resp1.SetBodyBytes(bodyList); //byte[] serialized = resp.FormatForSending(); //Message resp1 = new Message(serialized, serialized.Length); Assert.Equal(resp.Category, resp1.Category); //Category is incorrect" Assert.Equal(resp.Direction, resp1.Direction); //Direction is incorrect Assert.Equal(resp.Id, resp1.Id); //Correlation ID is incorrect Assert.Equal(resp.IsAlwaysInterleave, resp1.IsAlwaysInterleave); //Foo Boolean is incorrect Assert.Equal(resp.CacheInvalidationHeader, resp1.CacheInvalidationHeader); //Bar string is incorrect Assert.True(resp.TargetSilo.Equals(resp1.TargetSilo)); Assert.True(resp.TargetGrain.Equals(resp1.TargetGrain)); Assert.True(resp.SendingGrain.Equals(resp1.SendingGrain)); Assert.True(resp.SendingSilo.Equals(resp1.SendingSilo)); //SendingSilo is incorrect Assert.True(resp1.IsUsingInterfaceVersions); List <object> responseList = Assert.IsAssignableFrom <List <object> >(resp1.GetDeserializedBody(this.fixture.SerializationManager)); Assert.Equal <int>(numItems, responseList.Count); //Body list has wrong number of entries for (int k = 0; k < numItems; k++) { Assert.IsAssignableFrom <string>(responseList[k]); //Body list item " + k + " has wrong type Assert.Equal <string>((string)(requestBody[k]), (string)(responseList[k])); //Body list item " + k + " is incorrect } }
public bool TryDecodeMessage(out Message msg) { msg = null; // Is there enough read into the buffer to continue (at least read the lengths?) if (receiveOffset - decodeOffset < CalculateKnownMessageSize()) { return(false); } // parse lengths if needed if (headerLength == 0 || bodyLength == 0) { // get length segments List <ArraySegment <byte> > lenghts = ByteArrayBuilder.BuildSegmentListWithLengthLimit(readBuffer, decodeOffset, Message.LENGTH_HEADER_SIZE); // copy length segment to buffer int lengthBufferoffset = 0; foreach (ArraySegment <byte> seg in lenghts) { Buffer.BlockCopy(seg.Array, seg.Offset, lengthBuffer, lengthBufferoffset, seg.Count); lengthBufferoffset += seg.Count; } // read lengths headerLength = BitConverter.ToInt32(lengthBuffer, 0); bodyLength = BitConverter.ToInt32(lengthBuffer, 4); } // If message is too big for current buffer size, grow while (decodeOffset + CalculateKnownMessageSize() > currentBufferSize) { GrowBuffer(); } // Is there enough read into the buffer to read full message if (receiveOffset - decodeOffset < CalculateKnownMessageSize()) { return(false); } // decode header int headerOffset = decodeOffset + Message.LENGTH_HEADER_SIZE; List <ArraySegment <byte> > header = ByteArrayBuilder.BuildSegmentListWithLengthLimit(readBuffer, headerOffset, headerLength); // decode body int bodyOffset = headerOffset + headerLength; List <ArraySegment <byte> > body = ByteArrayBuilder.BuildSegmentListWithLengthLimit(readBuffer, bodyOffset, bodyLength); // build message this.deserializationContext.Reset(); this.deserializationContext.StreamReader.Reset(header); msg = new Message { Headers = SerializationManager.DeserializeMessageHeaders(this.deserializationContext) }; try { if (this.supportForwarding) { // If forwarding is supported, then deserialization will be deferred until the body value is needed. // Need to maintain ownership of buffer, so we need to duplicate the body buffer. msg.SetBodyBytes(this.DuplicateBuffer(body)); } else { // Attempt to deserialize the body immediately. msg.DeserializeBodyObject(this.serializationManager, body); } } finally { MessagingStatisticsGroup.OnMessageReceive(msg, headerLength, bodyLength); if (headerLength + bodyLength > this.serializationManager.LargeObjectSizeThreshold) { Log.Info( ErrorCode.Messaging_LargeMsg_Incoming, "Receiving large message Size={0} HeaderLength={1} BodyLength={2}. Msg={3}", headerLength + bodyLength, headerLength, bodyLength, msg.ToString()); if (Log.IsEnabled(LogLevel.Trace)) { Log.Trace("Received large message {0}", msg.ToLongString()); } } // update parse receiveOffset and clear lengths decodeOffset = bodyOffset + bodyLength; headerLength = 0; bodyLength = 0; AdjustBuffer(); } return(true); }