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);
            }
        }
        public EncodeResult Encode()
        {
            using (MemoryStream ms = new MemoryStream(8042))
            {
                BinaryWriter wtr = new BinaryWriter(ms, UdpPacket.ENCODING);

                EncodeResult result = new EncodeResult();

                if (_packet is MultiUdpPacket)
                {
                    MultiUdpPacket p        = _packet as MultiUdpPacket;
                    long           parentId = p.ID;

                    int pos = 0;
                    int len = p.MaxFragmentLength;

                    while (pos < p.FragmentBuff.Length)
                    {
                        int restLen = p.FragmentBuff.Length - pos;
                        if (restLen < len)
                        {
                            len = restLen;
                        }

                        //包头
                        p.GenerateID();

                        wtr.Write(p.Version);
                        wtr.Write(p.Type);
                        wtr.Write(p.ID);
                        wtr.Write(parentId);
                        wtr.Write(p.TotalLength);

                        wtr.Write(pos);
                        wtr.Write(len);
                        wtr.Write(p.FragmentBuff, pos, len);

                        byte[] buff = ms.ToArray();
                        result.AddFragment(buff);

                        wtr.Seek(0, SeekOrigin.Begin);
                        ms.Position = 0;
                        ms.SetLength(0);

                        pos += len;
                    }
                }
                else
                {
                    //包头
                    wtr.Write(_packet.Version);
                    wtr.Write(_packet.Type);
                    wtr.Write(_packet.ID);
                    wtr.Write(_packet.Command);
                    wtr.Write(_packet.FromMAC);
                    wtr.Write(_packet.ToMAC);

                    switch (_packet.CMD)
                    {
                    case UdpPacket.CMD_ENTRY:
                        EncodeEntryExtend(wtr, _packet);
                        break;

                    case UdpPacket.CMD_SEND_TEXT:
                        EncodeTextExtend(wtr, _packet.Extend);
                        break;

                    case UdpPacket.CMD_SEND_IMAGE:
                        EncodeImageExtend(wtr, _packet.Extend);
                        break;

                    case UdpPacket.CMD_SEND_FILE_REQUEST:
                        EncodeSendFileRequestExtend(wtr, _packet.Extend);
                        break;

                    case UdpPacket.CMD_RESPONSE:
                        EncodeResponseExtend(wtr, _packet.Extend);
                        break;

                    case UdpPacket.CMD_STATE:
                        EncodeEntryExtend(wtr, _packet);
                        break;

                    case UdpPacket.CMD_RETRANSMIT:
                        EncodeRetransmitExtend(wtr, _packet.Extend);
                        break;

                    case UdpPacket.CMD_USER_LIST:
                        EncodeUserListExtend(wtr, _packet.Extend);
                        break;

                    default:
                        break;
                    }

                    byte[] buff = ms.ToArray();
                    result.AddFragment(buff);
                }

                wtr.Close();
                ms.Close();

                return(result);
            }
        }
Beispiel #3
0
        public Packet Resolve()
        {
            using (MemoryStream ms = new MemoryStream(this._datagram))
            {
                BinaryReader rdr = new BinaryReader(ms, Packet.ENCODING);

                short version = rdr.ReadInt16();
                byte  type    = rdr.ReadByte(); //skip packet.Type;
                long  id      = rdr.ReadInt64();

                UdpPacket packet = null;
                if (type == Packet.PACKTE_TYPE_MULTI_UDP)
                {
                    //分别看下是否复合UDP分包
                    MultiUdpPacket packetm = new MultiUdpPacket();
                    packetm.Version      = version;
                    packetm.ID           = id;
                    packetm.ParentID     = rdr.ReadInt64();
                    packetm.TotalLength  = rdr.ReadInt32();
                    packetm.Position     = rdr.ReadInt32();
                    packetm.Length       = rdr.ReadInt32();
                    packetm.FragmentBuff = rdr.ReadBytes(packetm.Length);

                    packet = packetm;
                }
                else
                {
                    packet         = new UdpPacket();
                    packet.Version = version;
                    packet.ID      = id;
                    packet.Command = rdr.ReadUInt64();
                    packet.FromMAC = rdr.ReadString();
                    packet.ToMAC   = rdr.ReadString();

                    switch (packet.CMD)
                    {
                    case UdpPacket.CMD_ENTRY:
                        packet.Extend = ResolveEntryExtend(rdr, packet.Command);
                        break;

                    case UdpPacket.CMD_SEND_TEXT:
                        packet.Extend = ResolveTextExtend(rdr, this._securityKey);
                        break;

                    case UdpPacket.CMD_SEND_IMAGE:
                        packet.Extend = ResolveImageExtend(rdr, this._securityKey);
                        break;

                    case UdpPacket.CMD_SEND_FILE_REQUEST:
                        packet.Extend = ResolveSendFileRequestExtend(rdr, this._securityKey);
                        break;

                    case UdpPacket.CMD_RESPONSE:
                        packet.Extend = ResolveResponseExtend(rdr);
                        break;

                    case UdpPacket.CMD_STATE:
                        packet.Extend = ResolveEntryExtend(rdr, packet.Command);
                        break;

                    case UdpPacket.CMD_RETRANSMIT:
                        packet.Extend = ResolveRetransmitExtend(rdr);
                        break;

                    case UdpPacket.CMD_USER_LIST:
                        packet.Extend = ResolveUserListExtend(rdr);
                        break;

                    default:
                        break;
                    }
                }

                return(packet);
            }
        }
Beispiel #4
0
        public void Send(UdpPacket packet)
        {
            if (this._state != ClientState.Working)
            {
                return;
            }

            //异步发送
            IPEndPoint remoteIpEp = new IPEndPoint(packet.Address, packet.Port);

            packet.GenerateID();

            LoggerFactory.Debug("parepare send packet:{0}", packet);

            EncodeResult result = null;

            try
            {
                IPacketEncoder encoder = PacketEncoderFactory.CreateEncoder(packet);
                LoggerFactory.Debug("get encoder:{0}", encoder.GetType().Name);

                result = encoder.Encode(null);
                LoggerFactory.Debug("encode packet:{0}", result);

                if (result.Length > UDP_MAX_BUF_SIZE)
                {
                    //超过大小的分包处理
                    MultiUdpPacket mpacket = new MultiUdpPacket(result.Fragments[0]);
                    mpacket.ID       = packet.ID;
                    mpacket.ParentID = packet.ID;

                    encoder = PacketEncoderFactory.CreateEncoder(mpacket);
                    LoggerFactory.Debug("get encoder:{0}", encoder.GetType().Name);

                    result = encoder.Encode(UDP_MAX_BUF_SIZE - MultiUdpPacket.HEAD_SIZE);
                    LoggerFactory.Debug("encode packet:{0}", result);
                }
            }
            catch (Exception e)
            {
                OnSendPackage(packet, false);
                OnError(Errors.EncodeError, "加密包错误。", e);
            }

            try
            {
                for (int i = 0; i < result.Fragments.Count; i++)
                {
                    byte[]    buf   = result.Fragments[i];
                    SendState state = new SendState();
                    state.LastPacket = i == result.Fragments.Count - 1;
                    state.Packet     = packet;
                    _client.BeginSend(buf, buf.Length, remoteIpEp, new AsyncCallback(AsyncSendHandler), state);
                    Thread.Sleep(20);//稍微等待一下,避免丢包
                }
            }
            catch (Exception e)
            {
                OnSendPackage(packet, false);
                OnError(Errors.NetworkError, "发送包错误。", e);
            }
        }