예제 #1
0
 public void Release(SocketAsyncEventArgs e)
 {
     if (e.Buffer != null)
     {
         _bufferPool.Release(new ArraySegment <byte>(e.Buffer, e.Offset, e.Count));
     }
     if (e.BufferList != null)
     {
         foreach (var segment in e.BufferList)
         {
             _bufferPool.Release(segment);
         }
     }
     e.Dispose();
 }
예제 #2
0
        public bool TryWrite <T>(T item, IAirlockSerializer <T> serializer, DateTimeOffset timestamp, IBufferPool bufferPool)
        {
            if (!bufferPool.TryAcquire(out var buffer))
            {
                return(false);
            }

            var startingPosition       = buffer.Position;
            var serializationSucceeded = false;

            try
            {
                serializationSucceeded = recordSerializer.TrySerialize(item, serializer, timestamp, buffer);
            }
            finally
            {
                if (serializationSucceeded)
                {
                    buffer.WrittenRecords++;
                }
                else
                {
                    buffer.Position = startingPosition;
                }

                bufferPool.Release(buffer);
            }

            return(serializationSucceeded);
        }