public virtual void OnData(ByteBuffer buffer) { while (buffer.HasRemaining()) { var complete = message.ReadFrom(buffer); if (!complete) { return; } //MESSAGE IS COMPLETE HERE if (message.IsFlagSet(ClientMessage.BeginAndEndFlags)) { //HANDLE-MESSAGE HandleMessage(message); message = ClientMessage.Create(); continue; } if (message.IsFlagSet(ClientMessage.BeginFlag)) { // first fragment var builder = new BufferBuilder(); builderBySessionIdMap.Add(message.GetCorrelationId(), builder); builder.Append(message.Buffer(), 0, message.GetFrameLength()); } else { var builder = builderBySessionIdMap[message.GetCorrelationId()]; if (builder.Position() == 0) { throw new InvalidOperationException(); } builder.Append(message.Buffer(), message.GetDataOffset(), message.GetFrameLength() - message.GetDataOffset()); if (message.IsFlagSet(ClientMessage.EndFlag)) { var msgLength = builder.Position(); var cm = ClientMessage.CreateForDecode(builder.Buffer(), 0); cm.SetFrameLength(msgLength); //HANDLE-MESSAGE HandleMessage(cm); builderBySessionIdMap.Remove(message.GetCorrelationId()); } } message = ClientMessage.Create(); } }
private bool ReadFrame(ByteBuffer src) { // init internal buffer var remaining = src.Remaining(); if (remaining < ClientMessage.SizeOfFrameLengthAndFlags) { // we don't have even the frame length and flags ready return(false); } if (_readOffset == InitialOffset) { var frameLength = Bits.ReadIntL(src.Array(), src.Position); if (frameLength < ClientMessage.SizeOfFrameLengthAndFlags) { throw new Exception( $"The client message frame reported illegal length ({frameLength} bytes). Minimal length is the size of frame header ({ClientMessage.SizeOfFrameLengthAndFlags} bytes)."); } src.Position += Bits.IntSizeInBytes; var flags = Bits.ReadShortL(src.Array(), src.Position) & IntMask; src.Position += Bits.ShortSizeInBytes; var size = frameLength - ClientMessage.SizeOfFrameLengthAndFlags; var bytes = new byte[size]; var frame = new ClientMessage.Frame(bytes, flags); if (_message == null) { _message = ClientMessage.CreateForDecode(frame); } else { _message.Add(frame); } _readOffset = 0; if (size == 0) { return(true); } } var tail = _message.LastFrame; return(Accumulate(src, tail.Content, tail.Content.Length - _readOffset)); }
public void OnRead(ByteBuffer src) { while (src.HasRemaining()) { var complete = _activeReader.ReadFrom(src); if (!complete) { break; } var firstFrame = _activeReader.Message.FirstFrame; var flags = firstFrame.Flags; if (ClientMessage.IsFlagSet(flags, ClientMessage.UnfragmentedMessage)) { HandleMessage(_activeReader.Message); } else { var frameIterator = _activeReader.Message.GetIterator(); //ignore the fragmentationFrame frameIterator.Next(); var startFrame = frameIterator.Next(); var fragmentationId = Bits.ReadLongL(firstFrame.Content, ClientMessage.FragmentationIdOffset); if (ClientMessage.IsFlagSet(flags, ClientMessage.BeginFragmentFlag)) { _builderBySessionIdMap[fragmentationId] = ClientMessage.CreateForDecode(startFrame); } else if (ClientMessage.IsFlagSet(flags, ClientMessage.EndFragmentFlag)) { var clientMessage = MergeIntoExistingClientMessage(fragmentationId); HandleMessage(clientMessage); } else { MergeIntoExistingClientMessage(fragmentationId); } } _activeReader = new ClientMessageReader(); } }