Beispiel #1
0
        protected void OnPackageReceived(UdpPacket packet)
        {
            if (packet.Type == Packet.PACKTE_TYPE_MULTI_UDP)
            {
                MultiUdpPacket mp = packet as MultiUdpPacket;
                if (!_recvingPacketDic.TryGetValue(mp.ParentID, out MultiUdpPacket parentMp))
                {
                    parentMp         = new MultiUdpPacket(mp.TotalLength);
                    parentMp.ID      = mp.ParentID;
                    parentMp.Address = packet.Address;
                    parentMp.Port    = packet.Port;

                    _recvingPacketDic.TryAdd(mp.ParentID, parentMp);
                }
                parentMp.CopyFragmentBuff(mp);

                if (parentMp.TotalLength == parentMp.Length)
                {
                    //收取中缓存移除,添加到已经接受的包中
                    _recvingPacketDic.TryRemove(mp.ParentID, out MultiUdpPacket pMp);

                    IPacketResolver resolver = PacketResolverFactory.CreateResolver(pMp.FragmentBuff, 0, pMp.TotalLength, this.SecurityKeys.Private);
                    LoggerFactory.Debug("get resolver:{0}", resolver.GetType().Name);

                    UdpPacket udpPacket = resolver.Resolve() as UdpPacket;
                    udpPacket.Address = packet.Address;
                    udpPacket.Port    = packet.Port;

                    _receivedPackets.Add(udpPacket);
                }
            }
            else
            {
                _receivedPackets.Add(packet);
            }
        }
Beispiel #2
0
        private void AsyncReceiveHandler(IAsyncResult ar)
        {
            bool isExiting = false;

            if (ar.IsCompleted)
            {
                byte[]     buff     = null;
                IPEndPoint remoteEp = null;
                try
                {
                    buff = _client.EndReceive(ar, ref remoteEp);
                    LoggerFactory.Debug("received:{0}", buff);
                }
                catch (Exception e)
                {
                    OnError(Errors.OutofSizePacket, "收包错误。", e);
                }

                if (buff != null && buff.Length > 0)
                {
                    try
                    {
                        IPacketResolver resolver = PacketResolverFactory.CreateResolver(buff, 0, buff.Length, this.SecurityKeys.Private);
                        LoggerFactory.Debug("get resolver:{0}", resolver.GetType().Name);

                        UdpPacket packet = resolver.Resolve() as UdpPacket;
                        packet.Address = remoteEp.Address;
                        packet.Port    = remoteEp.Port;
                        LoggerFactory.Debug("resolved packet:{0}", packet);

                        OnPackageReceived(packet);

                        if (packet.Type == Packet.PACKTE_TYPE_UDP &&
                            packet.CMD == UdpPacket.CMD_EXIT &&
                            packet.Address.Equals(this.IP))
                        {
                            //如果是当前用户发出退出请求,下一步则不监听了
                            isExiting = true;
                        }
                    }
                    catch (Exception e)
                    {
                        OnError(Errors.ResolveError, "解包错误。", e);
                    }
                }
            }

            if (isExiting ||
                this._state == ClientState.Closing ||
                this._state == ClientState.Closed)
            {
                //正在准备退出
            }
            else
            {
                try
                {
                    //继续下一次收包
                    _client.BeginReceive(AsyncReceiveHandler, null);
                    LoggerFactory.Debug("begin receive");
                }
                catch (Exception e)
                {
                    OnError(Errors.OutofSizePacket, "接受消息停止,需要重新监听....", e);
                }
            }
        }