/// <summary>发送数据
        /// </summary>
        public override Task <FastDFSResp> SendRequestAsync <T>(FastDFSReq <T> request)
        {
            _taskCompletionSource = new TaskCompletionSource <FastDFSResp>();
            //上下文,当前的信息
            _context = BuildContext <T>(request);

            var bodyBuffer = request.EncodeBody(Option);

            if (request.Header.Length == 0)
            {
                request.Header.Length = request.InputStream != null ? request.InputStream.Length + bodyBuffer.Length : bodyBuffer.Length;
            }

            var headerBuffer = request.Header.ToBytes();
            var newBuffer    = ByteUtil.Combine(headerBuffer, bodyBuffer);

            //流文件发送
            if (request.InputStream != null)
            {
                _channel.WriteAsync(Unpooled.WrappedBuffer(newBuffer));
                var stream = new FixChunkedStream(request.InputStream);
                _channel.WriteAndFlushAsync(stream);
            }
            else
            {
                _channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(newBuffer));
            }
            return(_taskCompletionSource.Task);
        }
        public override async ValueTask <FastDFSResp> SendRequestAsync <T>(FastDFSReq <T> request)
        {
            _tcs = new TaskCompletionSource <FastDFSResp>();

            _resp = new T();

            //上下文,当前的信息
            _context = BuildContext <T>(request);

            var bodyBuffer = request.EncodeBody(Configuration);

            if (request.Header.Length == 0)
            {
                request.Header.Length = request.InputStream != null ? request.InputStream.Length + bodyBuffer.Length : bodyBuffer.Length;
            }

            var headerBuffer = request.Header.ToBytes();
            var newBuffer    = ByteUtil.Combine(headerBuffer, bodyBuffer);

            //流文件发送
            if (request.InputStream != null)
            {
                await _client.SendAsync(newBuffer);

                using (request.InputStream)
                {
                    var chunkSize = 8192;

                    while (true)
                    {
                        var availableBytes = request.InputStream.Length - request.InputStream.Position;
                        if (availableBytes <= 0)
                        {
                            break;
                        }

                        int readChunkSize = (int)Math.Min(chunkSize, availableBytes);

                        var buffers = new byte[readChunkSize];
                        _ = await request.InputStream.ReadAsync(buffers, 0, buffers.Length);

                        await _client.SendAsync(buffers);
                    }
                }
            }
            else
            {
                await _client.SendAsync(newBuffer);
            }

            return(await _tcs.Task);
        }
Exemple #3
0
        public override Task <FastDFSResp> SendRequestAsync <T>(FastDFSReq <T> request)
        {
            _taskCompletionSource = new TaskCompletionSource <FastDFSResp>();
            //上下文,当前的信息
            _context = BuildContext <T>(request);

            var bodyBuffer = request.EncodeBody(Option);

            if (request.Header.Length == 0)
            {
                request.Header.Length = request.InputStream != null ? request.InputStream.Length + bodyBuffer.Length : bodyBuffer.Length;
            }

            var headerBuffer = request.Header.ToBytes();
            var newBuffer    = ByteUtil.Combine(headerBuffer, bodyBuffer);

            //流文件发送
            if (request.InputStream != null)
            {
                _client.SendAsync(newBuffer);

                using (request.InputStream)
                {
                    //设置缓冲区大小
                    byte[] buffers = new byte[1024 * 1024];
                    //读取一次
                    int r = request.InputStream.Read(buffers, 0, buffers.Length);
                    //判断本次是否读取到了数据
                    while (r > 0)
                    {
                        _client.SendAsync(buffers).AsTask().Wait();
                        r = request.InputStream.Read(buffers, 0, buffers.Length);
                    }
                }
            }
            else
            {
                _client.SendAsync(newBuffer).AsTask().Wait();
            }
            return(_taskCompletionSource.Task);
        }
        /// <summary>发送数据
        /// </summary>
        public override async ValueTask <FastDFSResp> SendRequestAsync <T>(FastDFSReq <T> request)
        {
            if (!_channel.Active)
            {
                Logger.LogWarning("DotNetty connection, channel is inactive! {0}.", ConnectionAddress);
                await DisconnectAsync();

                throw new Exception("Current connection is inactive!");
            }

            _tcs = new TaskCompletionSource <FastDFSResp>();

            _resp = new T();

            //上下文,当前的信息
            _context = BuildContext <T>(request);

            var bodyBuffer = request.EncodeBody(Configuration);

            if (request.Header.Length == 0)
            {
                request.Header.Length = request.InputStream != null ? request.InputStream.Length + bodyBuffer.Length : bodyBuffer.Length;
            }

            var headerBuffer = request.Header.ToBytes();
            var newBuffer    = ByteUtil.Combine(headerBuffer, bodyBuffer);

            //流文件发送
            if (request.InputStream != null)
            {
                await _channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(newBuffer));

                var stream = new FixChunkedStream(request.InputStream);
                await _channel.WriteAndFlushAsync(stream);
            }
            else
            {
                await _channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(newBuffer));
            }
            return(await _tcs.Task);
        }