/// <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); }
protected virtual TransportContext BuildContext <T>(FastDFSReq <T> request) where T : FastDFSResp, new() { var context = new TransportContext() { ReqType = request.GetType(), RespType = typeof(T), IsInputStream = request.InputStream != null, IsOutputStream = request.IsOutputStream, OutputFilePath = request.OutputFilePath }; return(context); }
/// <summary>请求执行器 /// </summary> /// <typeparam name="T">请求的类型<see cref="FastDFSCore.Protocols.FastDFSReq"/></typeparam> /// <param name="request">请求</param> /// <param name="connectionAddress">返回</param> /// <returns></returns> public async Task <T> Execute <T>(FastDFSReq <T> request, ConnectionAddress connectionAddress = null) where T : FastDFSResp, new() { var connection = connectionAddress == null?_connectionManager.GetTrackerConnection() : _connectionManager.GetStorageConnection(connectionAddress); if (connection == null) { throw new NullReferenceException($"Can't find connection,ipaddr:{connectionAddress} "); } connection.Open(); var response = await connection.SendRequestAsync <T>(request); connection.Close(); return(response as T); }
/// <summary>请求执行器 /// </summary> /// <typeparam name="T">请求的类型<see cref="FastDFSReq"/></typeparam> /// <param name="request">请求</param> /// <param name="clusterName">集群名</param> /// <param name="connectionAddress">返回</param> /// <returns></returns> public async ValueTask <T> Execute <T>(FastDFSReq <T> request, string clusterName, ConnectionAddress connectionAddress = null) where T : FastDFSResp, new() { var cluster = _clusterFactory.Get(clusterName); var connection = connectionAddress == null?cluster.GetTrackerConnection() : cluster.GetStorageConnection(connectionAddress); if (connection == null) { throw new NullReferenceException($"Can't find connection,ipaddr:{connectionAddress} "); } await connection.OpenAsync(); var response = await connection.SendRequestAsync <T>(request); await connection.CloseAsync(); return(response as T); }
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); }
/// <summary>发送数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="request"></param> /// <returns></returns> public abstract ValueTask <FastDFSResp> SendRequestAsync <T>(FastDFSReq <T> request) where T : FastDFSResp, new();