Example #1
0
 private void SendInternal(IOState state)
 {
     if (!state.BandwidthController.CanTransmit(state.PendingBytes))
     {
         _sendQueue.Add(state);
         return;
     }
     if (state.WaitingForBuffer)
     {
         state.Buffer = _bufferAllocator.Allocate(state.Bytes);
         if (state.WaitingForBuffer)
         {
             _sendQueue.Add(state);
             return;
         }
     }
     state.BandwidthController.SetTransmittion(state.PendingBytes);
     state.Connection.Send(state.GetBufferForPending(), (sentCount, success) =>
     {
         try
         {
             if (success && sentCount > 0)
             {
                 if (sentCount < state.PendingBytes)
                 {
                     state.PendingBytes -= sentCount;
                     _sendQueue.Add(state);
                 }
                 else
                 {
                     var data = state.GetData();
                     state.SuccessCallback(data);
                 }
             }
             else
             {
                 state.FailureCallback();
             }
         }
         finally
         {
             state.Release();
             _bufferAllocator.Free(state.Buffer);
         }
     });
 }
Example #2
0
        public virtual async Task WriteBytesAsync(byte[] bytes)
        {
            var msgLen = bytes.Length + 1;
            var buf    = BufferAllocator.Allocate(msgLen);

            System.Buffer.BlockCopy(bytes, 0, buf.Array, buf.Offset, msgLen - 1);
            await Stream.WriteAsync(buf.Array, buf.Offset, msgLen);

            BufferAllocator.Free(buf);
        }
Example #3
0
            private async Task <ReadFileResult> ReadFile(int length)
            {
                IBuffer          content   = bufferAllocator.Allocate(length);
                Stream           dest      = content.GetStream();
                IMD5HashProvider chunkHash = MD5HashProviderFactory.GetHashProvider().CreateHash();

                byte[] b = ArrayPool <byte> .Shared.Rent(Configuration.BufferSize);

                try
                {
                    int read   = 0;
                    int toRead = length;
                    do
                    {
                        read = await fileStream.ReadAsync(b, offset : 0, count : Math.Min(b.Length, toRead)).ConfigureAwait(false);

                        toRead -= read;

                        await dest.WriteAsync(b, offset : 0, count : read).ConfigureAwait(false);

                        chunkHash.Append(b, offset: 0, size: read);
                        fileHash.Append(b, offset: 0, size: read);
                    } while (read > 0 && toRead > 0);
                    if (toRead > 0)
                    {
                        throw new Exception($"Expected to read {length} bytes, actual read {length - toRead} bytes");
                    }

                    chunkHash.Finalize(ArrayPool <byte> .Shared.Rent(0), 0, 0);
                    return(new ReadFileResult {
                        Content = content, Hash = chunkHash.GetComputedHashAsString()
                    });
                }
                catch
                {
                    content.Dispose();
                    throw;
                }
                finally
                {
                    ArrayPool <byte> .Shared.Return(b);

                    dest.Dispose();
                }
            }
Example #4
0
        private async Task ReadBufferAsync()
        {
            if (Pos < End)
            {
                return;
            }


            BufferAllocator.Free(Buffer);
            Buffer = BufferAllocator.Allocate(BufferSize);

            Pos = 0;
            var readed = await Stream.ReadAsync(Buffer.Array, Buffer.Offset, Buffer.Count, new CancellationToken());

            if (readed == 0)
            {
                throw new EndOfStreamException();
            }
            End = readed;
        }