private void PrimitiveSend(ResoucesInfo resoucesInfo) { //Loger.Info($"发送资源RID[{resoucesInfo.Rid}]数据开始"); using (var sender = new TransferSender(this._net, resoucesInfo)) { try { if (!this._senderDic.TryAdd(resoucesInfo.Rid, sender)) { throw new ApplicationException("this._senderDic.TryAdd失败"); } sender.Send(); } finally { TransferSender sender2; this._senderDic.TryRemove(resoucesInfo.Rid, out sender2); //Loger.Info($"发送资源RID[{resoucesInfo.Rid}]数据完成"); } } }
/// <summary> /// 创建 /// </summary> /// <param name="resoucesInfo"></param> public SendDataNotifyMessage(ResoucesInfo resoucesInfo) : base(new CommonHeader(TransferCommands.SendNotify, TimeEx.GetTimestamp(), resoucesInfo.Rid)) { this.Priority = resoucesInfo.Policy.Priority; byte sendMode; if (resoucesInfo.Length <= TransferConstant.MESSAGE_MAX_SIZE) { var data = new byte[resoucesInfo.Length]; Array.Copy(resoucesInfo.Data, resoucesInfo.Postion, data, 0, data.Length); this.Data = data; sendMode = ResourceTypeConstant.Message; } else { switch (resoucesInfo.ResoucesType) { case TransferDataType.Data: sendMode = ResourceTypeConstant.ResourceData; break; case TransferDataType.Stream: sendMode = ResourceTypeConstant.ResourceStream; break; case TransferDataType.File: sendMode = ResourceTypeConstant.ResourceFile; this.FileName = Path.GetFileName(resoucesInfo.FilePath); break; default: throw new NotImplementedException($"未实现的资源类型:{resoucesInfo.ResoucesType.ToString()}"); } } this.ResourceType = sendMode; this.Timeout = resoucesInfo.Policy.MillisecondsTimeout; this.Size = resoucesInfo.Length; }
/// <summary> /// 发送数据 /// </summary> /// <param name="filePath">要发送的文件路径</param> /// <param name="postion">发送数据在流中的起始位置</param> /// <param name="length">要发送数据长度</param> /// <param name="policy">发送策略</param> public void SendFile(string filePath, long postion, long length, TransferPolicy policy) { if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException(nameof(filePath)); } if (!File.Exists(filePath)) { throw new FileNotFoundException("要发送的文件不存在", filePath); } if (postion < 0) { throw new ArgumentOutOfRangeException($"发送数据在文件中的起始位置值:{postion}无效", nameof(postion)); } if (length < 1) { throw new ArgumentOutOfRangeException($"要发送的数据长度值:{length}无效", nameof(length)); } var totalLen = new FileInfo(filePath).Length; if (postion + length > totalLen) { throw new ArgumentOutOfRangeException($"要发送的数据长度值:{length}过大,超出文件[{postion}-{totalLen}]范围"); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } using (var resoucesInfo = new ResoucesInfo(filePath, policy, postion, length)) { this.PrimitiveSend(resoucesInfo); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="filePath">要发送的文件路径</param> /// <param name="policy">发送数据在流中的起始位置</param> public void SendFile(string filePath, TransferPolicy policy) { if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException(nameof(filePath)); } if (!File.Exists(filePath)) { throw new FileNotFoundException("要发送的文件不存在", filePath); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } using (var resoucesInfo = new ResoucesInfo(filePath, policy, 0, new FileInfo(filePath).Length)) { this.PrimitiveSend(resoucesInfo); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="data">要发送的数据</param> /// <param name="policy">发送策略</param> public void SendData(byte[] data, TransferPolicy policy) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (data.Length == 0) { return; } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } using (var resoucesInfo = new ResoucesInfo(data, policy, 0, data.Length)) { this.PrimitiveSend(resoucesInfo); } }
/// <summary> /// 发送数据流 /// </summary> /// <param name="stream">要发送的流</param> /// <param name="postion">发送数据在流中的起始位置</param> /// <param name="length">要发送数据长度</param> /// <param name="policy">发送策略</param> public void SendData(Stream stream, long postion, long length, TransferPolicy policy) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException("流不可读", nameof(stream)); } if (postion < 0) { throw new ArgumentOutOfRangeException($"发送数据在流中的起始位置值:{postion}无效", nameof(postion)); } if (length < 1) { throw new ArgumentOutOfRangeException($"要发送的数据长度值:{length}无效", nameof(length)); } if (postion + length > stream.Length) { throw new ArgumentOutOfRangeException($"要发送的数据长度值:{length}过大,超出流[{postion}-{stream.Length}]范围"); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } using (var resoucesInfo = new ResoucesInfo(stream, policy, postion, length)) { this.PrimitiveSend(resoucesInfo); } }
/// <summary> /// 发送数据 /// </summary> /// <param name="data">要发送的数据</param> /// <param name="postion">发送数据在流中的起始位置</param> /// <param name="length">要发送数据长度</param> /// <param name="policy">发送策略</param> public void SendData(byte[] data, int postion, int length, TransferPolicy policy) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (data.Length == 0) { return; } if (postion < 0) { throw new ArgumentOutOfRangeException($"发送数据在总数据中的起始位置值:{postion}无效", nameof(postion)); } if (length < 1) { throw new ArgumentOutOfRangeException($"要发送的数据长度值:{length}无效", nameof(length)); } if (postion + length > data.Length) { throw new ArgumentOutOfRangeException($"要发送的数据长度值:{length}过大,超出数据[{postion}-{data.Length}]范围"); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } using (var resoucesInfo = new ResoucesInfo(data, policy, postion, length)) { this.PrimitiveSend(resoucesInfo); } }
public TransferSender(ITransferNet net, ResoucesInfo resoucesInfo) { this._net = net; this._resoucesInfo = resoucesInfo; }