public bool WriteTo(ByteBuffer dst, ClientMessage clientMessage) { if (_currentFrame == null) { _currentFrame = clientMessage.FirstFrame; } for (; ;) { var isLastFrame = _currentFrame.next == null; if (WriteFrame(dst, _currentFrame, isLastFrame)) { _writeOffset = LengthNotWrittenYet; if (isLastFrame) { _currentFrame = null; return(true); } _currentFrame = _currentFrame.next; } else { return(false); } } }
public static List <long> Decode(ClientMessage.Frame frame) { var itemCount = frame.Content == null ? 0 : frame.Content.Length / Bits.LongSizeInBytes; var result = new List <long>(itemCount); for (var i = 0; i < itemCount; i++) { result.Add(FixedSizeTypesCodec.DecodeLong(frame.Content, i * Bits.LongSizeInBytes)); } return(result); }
public static void Encode(ClientMessage clientMessage, IEnumerable <long> collection) { var itemCount = collection.Count(); var frame = new ClientMessage.Frame(new byte[itemCount * Bits.LongSizeInBytes]); var i = 0; foreach (var value in collection) { FixedSizeTypesCodec.EncodeLong(frame.Content, i * Bits.LongSizeInBytes, value); i++; } clientMessage.Add(frame); }
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 static byte[] Decode(ClientMessage.Frame frame) { return(frame.Content); }
public static IData Decode(ClientMessage.Frame frame) { return(new HeapData(frame.Content)); }
private bool WriteFrame(ByteBuffer dst, ClientMessage.Frame frame, bool isLastFrame) { // the number of bytes that can be written to the bb var bytesWritable = dst.Remaining(); var frameContentLength = frame.Content == null ? 0 : frame.Content.Length; //if write offset is -1 put the length and flags byte first if (_writeOffset == -1) { if (bytesWritable >= ClientMessage.SizeOfFrameLengthAndFlags) { Bits.WriteIntL(dst.Array(), dst.Position, frameContentLength + ClientMessage.SizeOfFrameLengthAndFlags); dst.Position += Bits.IntSizeInBytes; if (isLastFrame) { Bits.WriteShortL(dst.Array(), dst.Position, (short)(frame.Flags | ClientMessage.IsFinalFlag)); } else { Bits.WriteShortL(dst.Array(), dst.Position, (short)frame.Flags); } dst.Position += Bits.ShortSizeInBytes; _writeOffset = 0; } else { return(false); } } bytesWritable = dst.Remaining(); if (frame.Content == null) { return(true); } // the number of bytes that need to be written var bytesNeeded = frameContentLength - _writeOffset; int bytesWrite; bool done; if (bytesWritable >= bytesNeeded) { // all bytes for the value are available bytesWrite = bytesNeeded; done = true; } else { // not all bytes for the value are available. Write as much as is available bytesWrite = bytesWritable; done = false; } dst.Put(frame.Content, _writeOffset, bytesWrite); _writeOffset += bytesWrite; return(done); }