예제 #1
0
        public static SharedArray ConvertToBytes(this EventMeta metaInfo)
        {
            var buffer = SharedArray.Rent();

            buffer.Write(metaInfo.Version);
            buffer.Write(metaInfo.Timestamp);
            buffer.WriteUtf8String(metaInfo.FlowId);
            return(buffer);
        }
예제 #2
0
 public void Rent(int length)
 {
     using var buffer = SharedArray.Rent(length);
     Assert.True(buffer.Position == 0);
     Assert.True(buffer.Length % 4096 == 0);
     buffer.Write(10);
     Assert.Equal(sizeof(int), buffer.AsSpan().Length);
     Assert.Equal(sizeof(int), buffer.ToArray().Length);
 }
예제 #3
0
        public void WriteShort(int count)
        {
            using var buffer = SharedArray.Rent();
            for (int i = 0; i < count; i++)
            {
                buffer.Write((short)i);
            }

            Assert.Equal(sizeof(short) * count, buffer.AsSpan().Length);
            Assert.Equal(sizeof(short) * count, buffer.ToArray().Length);
        }
예제 #4
0
        public void WriteChars(int count)
        {
            using var buffer = SharedArray.Rent();
            var length = 0;

            for (int i = 0; i < count; i++)
            {
                buffer.WriteUtf8String(i.ToString().AsSpan());
                length += Encoding.UTF8.GetByteCount(i.ToString());
            }

            Assert.Equal(length, buffer.AsSpan().Length);
            Assert.Equal(length, buffer.ToArray().Length);
        }
예제 #5
0
        public static SharedArray ConvertToBytes(this in EventTransUnit eventBitUnit)
        {
            var buffer        = SharedArray.Rent();
            var eventNameSpan = eventBitUnit.EventName.AsSpan();

            var(actorIdBytes, idType) = eventBitUnit.ActorId switch
            {
                long id => (actorIdBytes : BitConverter.GetBytes(id), idType : ActorIdType.Long),
                string id => (actorIdBytes : Encoding.UTF8.GetBytes(id), idType : ActorIdType.String),
                Guid id => (actorIdBytes : Encoding.UTF8.GetBytes(id.ToString()), idType : ActorIdType.Guid),
                _ => throw new ArgumentOutOfRangeException(eventBitUnit.ActorId.GetType().Name),
            };
            buffer.Write((byte)BytesHeaderType.Event);
            buffer.Write((byte)idType);
            buffer.Write((ushort)Encoding.UTF8.GetByteCount(eventNameSpan));
            buffer.Write((ushort)actorIdBytes.Length);
            buffer.Write((ushort)eventBitUnit.MetaBytes.Length);
            buffer.Write(eventBitUnit.EventBytes.Length);
            buffer.WriteUtf8String(eventNameSpan);
            buffer.Write(actorIdBytes);
            buffer.Write(eventBitUnit.MetaBytes);
            buffer.Write(eventBitUnit.EventBytes);
            return(buffer);
        }