Exemple #1
0
        /// <summary>
        /// 自由线程,检测未发送的数据并发出,存在其中的就是没有收到确认包的数据包
        /// </summary>
        void CheckUnConfirmedQueue()
        {
            do
            {
                if (sendlist.Count > 0)
                {
                    UdpPacket[] array = null;

                    lock (sendlist)
                    {
                        array = sendlist.ToArray();
                    }
                    //挨个重新发送并计数
                    Array.ForEach(array, s =>
                    {
                        s.sendtimes++;
                        if (s.sendtimes >= MaxResendTimes)
                        {
                            //sOnPackageSendFailure//出发发送失败事件
                            sendlist.Remove(s);//移除该包
                        }
                        else
                        {
                            //重新发送
                            byte[] udpPacketDatagram = SerializationUnit.SerializeObject(s);
                            client.Send(udpPacketDatagram, udpPacketDatagram.Length, s.remoteip);
                        }
                    });
                }

                Thread.Sleep(CheckQueueTimeInterval);//间隔一定时间重发数据
            } while (true);
        }
Exemple #2
0
        //接收数据的回调函数
        public void ReceiveCallback(IAsyncResult param)
        {
            if (param.IsCompleted)
            {
                UdpPacket udpp = null;
                try
                {
                    byte[] data = client.EndReceive(param, ref remotIpEnd);//接收数据,当Client端连接主机的时候,test就变成Cilent端的IP了
                    udpp = (UdpPacket)SerializationUnit.DeserializeObject(data);
                }
                catch (Exception ex)
                {
                    //异常处理操作
                }
                finally
                {
                    AsyncReceiveData();
                }
                if (udpp != null)//触发数据包收到事件
                {
                    if (!RecListDic.ContainsKey(udpp.sequence))
                    {
                        RecListDic.Add(udpp.sequence, new RecDataList(udpp));
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    else
                    {
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    foreach (KeyValuePair <long, RecDataList> ss in RecListDic)//循环看看是否有哪一个满了
                    {
                        Msg m = ss.Value.show();
                        if (m != null)
                        {
                            if (ReceivedOk != null)
                            {
                                ReceivedOk(m, remotIpEnd);
                            }
                            RecListDic.Remove(ss.Value.sequence);
                            break;
                        }
                    }

                    PackageEventArgs arg = new PackageEventArgs(udpp, remotIpEnd);
                    OnPackageReceived(arg);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 同步数据接收方法
        /// </summary>
        public void ReceiveData()
        {
            while (true)
            {
                IPEndPoint retip = null;
                UdpPacket  udpp  = null;
                try
                {
                    byte[] data = client.Receive(ref retip);//接收数据,当Client端连接主机的时候,retip就变成Cilent端的IP了
                    udpp = (UdpPacket)SerializationUnit.DeserializeObject(data);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    //异常处理操作
                }
                if (udpp != null)
                {
                    if (!RecListDic.ContainsKey(udpp.sequence))
                    {
                        RecListDic.Add(udpp.sequence, new RecDataList(udpp));
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    else
                    {
                        RecDataList rdl = RecListDic[udpp.sequence];
                        rdl.addPacket(udpp);
                    }
                    foreach (KeyValuePair <long, RecDataList> ss in RecListDic)//循环看看是否有哪一个满了
                    {
                        Msg m = ss.Value.show();
                        if (m != null)
                        {
                            if (ReceivedOk != null)
                            {
                                ReceivedOk(m, retip);
                            }
                            RecListDic.Remove(ss.Value.sequence);
                            break;
                        }
                    }

                    PackageEventArgs arg = new PackageEventArgs(udpp, retip);
                    OnPackageReceived(arg);//数据包收到触发事件
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 同步发送分包数据
        /// </summary>
        /// <param name="message"></param>
        public void SendData(Msg message)
        {
            ICollection <UdpPacket> udpPackets = UdpPacketSplitter.Split(message);

            foreach (UdpPacket udpPacket in udpPackets)
            {
                byte[] udpPacketDatagram = SerializationUnit.SerializeObject(udpPacket);
                this.client.Connect(udpPacket.remoteip);
                //使用同步发送
                client.Send(udpPacketDatagram, udpPacketDatagram.Length, udpPacket.remoteip);
                if (udpPacket.IsRequireReceiveCheck)
                {
                    PushSendItemToList(udpPacket);//将该消息压入列表
                }
            }
        }
Exemple #5
0
        public static ICollection <UdpPacket> Split(Msg message)
        {
            byte[] datagram = null;
            try
            {
                datagram = SerializationUnit.SerializeObject(message);
            }
            catch (Exception e)
            {
                //AddTalkMessage("数据转型异常");
            }
            //产生一个序列号,用来标识包数据属于哪一组
            Random Rd             = new Random();
            long   SequenceNumber = Rd.Next(88888, 999999);
            ICollection <UdpPacket> udpPackets = UdpPacketSplitter.Split(SequenceNumber, datagram, 10240, message.destinationIP, message.port);

            return(udpPackets);
        }
Exemple #6
0
        /// <summary>
        /// 异步分包发送数组的方法
        /// </summary>
        /// <param name="message"></param>
        public void AsyncSendData(Msg message)
        {
            ICollection <UdpPacket> udpPackets = UdpPacketSplitter.Split(message);

            this.client.Connect(udpPackets.FirstOrDefault().remoteip);

            foreach (UdpPacket udpPacket in udpPackets)
            {
                byte[] udpPacketDatagram = SerializationUnit.SerializeObject(udpPacket);

                //使用同步发送
                //client.Send(udpPacketDatagram, udpPacketDatagram.Length);

                //使用异步的方法发送数据
                this.client.BeginSend(udpPacketDatagram, udpPacketDatagram.Length, new AsyncCallback(SendCallback), null);
                //Thread.Sleep(10);
            }
        }
Exemple #7
0
 public Msg show()
 {
     if (RecudpPackets.Count == total)//表示已经收集满了
     {
         //重组数据
         foreach (UdpPacket udpPacket in RecudpPackets)
         {
             //偏移量
             int offset = (udpPacket.index - 1) * udpPacket.dataLength;
             Buffer.BlockCopy(udpPacket.data, 0, DataBuffer, offset, udpPacket.data.Length);
         }
         Msg rmsg = (Msg)SerializationUnit.DeserializeObject(DataBuffer);
         DataBuffer = null;
         RecudpPackets.Clear();
         return(rmsg);
     }
     else
     {
         return(null);
     }
 }
Exemple #8
0
 //把这个对象生成byte[]
 public byte[] ToArray()
 {
     return(SerializationUnit.SerializeObject(this));
 }