/// <summary> /// 发送一定时间丢包的 /// 该实现和发送端实现不一样 /// </summary> private void SendLost() { Task.Factory.StartNew(() => { Thread.Sleep(100); //先发送启动确认包; SendAck(); if (lstSeq.Count > 0) { for (int i = 0; i < lstSeq.Count; i++) { LostPackage lost = new LostPackage() { PackID = this.PackID, PackSeq = lstSeq[i], direction = 0 }; Stream.Write(DestHost, DestPort, lost.GetBuffer()); } } WaitRecNum++; if (!IsShut) { SendLost(); } if (WaitRecNum - lastRecNum > RecTimeOut * 10) { //实际是WaitRecNum-lastRecNum>RecTimeOut*1000/100; Stop(); } }); }
/// <summary> /// 丢失 /// </summary> /// <param name="package"></param> public void AddLost(LostPackage package) { var pack = SendBuffer.Find(x => x.PackSeq == package.PackSeq); if (pack == null) { //发送一个丢失包 LostPackage lost = new LostPackage() { PackID = package.PackID, PackSeq = package.PackSeq, direction = 1 }; SendData(lost); } else { SendData(pack); } }
/// <summary> /// 从接收session接收数据 /// </summary> /// <param name="sender"></param> /// <param name="data"></param> /// <param name="host"></param> private void Stream_DataCall(object sender, byte[] data, HostAndPort host) { //创建类型 BasePackage package = new BasePackage(data); //所有接收信息都经过 switch (package.PackType) { case PackageControl.Data: package = new DATAPackage(data); break; case PackageControl.Ack: package = new ACKPackage(data); break; case PackageControl.Fin: package = new FINPackage(data); break; case PackageControl.Lost: package = new LostPackage(data); break; case PackageControl.ShutDown: package = new ShutDownPackage(data); break; case PackageControl.Syn: package = new SYNPackage(data); break; } package.DestHost = host.Host; package.DestPort = host.Port; AddPackage(host, package); }
/// <summary> /// 发送方丢失包 /// </summary> /// <param name="lost"></param> public void Add(LostPackage lost) { //如果丢失就从丢失包中移除,数据等待,没有就超时 lstSeq.Remove(lost.PackSeq); }
/// <summary> /// 加入数据包 /// </summary> /// <param name="package"></param> public void Add(BasePackage package) { ReceiveQueue receiveQueue = null; if (receiveTime.ContainsKey(package.PackID)) { //发送一次完成,触发发送端移除 SendSucess(package.PackID, package.DestHost, package.DestPort); return; } if (!queue.ContainsKey(package.PackID)) { //接收队列计算一个真实的量用于请求发送 int len = (int)(package.Length / package.PackSize); len = package.Length % package.PackSize > 0 ? len + 1 : len; receiveQueue = new ReceiveQueue(len) { Stream = senderSession.SocketStream, DestHost = this.Host.Host, DestPort = this.Host.Port }; queue[package.PackID] = receiveQueue; } if (queue.TryGetValue(package.PackID, out receiveQueue)) { if (package is DATAPackage) { if (receiveQueue.Add((DATAPackage)package)) { //接收完成 ReceiveSucess(package.PackID); if (dataNotify != null) { dataNotify.Invoke(this, receiveQueue.GetData(), Host); } receiveQueue.Stop(); } } else if (package is ShutDownPackage) { ShutDownPackage pack = package as ShutDownPackage; if (0 == pack.direction) { senderSession.Add(pack); } else { receiveQueue.Add(pack); } } else if (package is LostPackage) { //说明是回执信息,转给发送队列 LostPackage pack = package as LostPackage; if (0 == pack.direction) { senderSession.Add(package); } else { receiveQueue.Add(pack); } } else if (package is ACKPackage) { senderSession.Add(package); } else if (package is FINPackage) { senderSession.Add(package); } } }