Пример #1
0
        public void Send(IPEndPoint destination, RUDPPacketType type = RUDPPacketType.DAT, RUDPPacketFlags flags = RUDPPacketFlags.NUL, byte[] data = null, int[] intData = null)
        {
            InitSequence(destination);
            RUDPConnectionData sq = _sequences[destination.ToString()];

            if ((data != null && data.Length < _maxMTU) || data == null)
            {
                SendPacket(new RUDPPacket()
                {
                    Dst   = destination,
                    Id    = sq.PacketId,
                    Type  = type,
                    Flags = flags,
                    Data  = data
                });
                sq.PacketId++;
            }
            else if (data != null && data.Length >= _maxMTU)
            {
                int i = 0;
                List <RUDPPacket> PacketsToSend = new List <RUDPPacket>();
                while (i < data.Length)
                {
                    int min = i;
                    int max = _maxMTU;
                    if ((min + max) > data.Length)
                    {
                        max = data.Length - min;
                    }
                    byte[] buf = data.Skip(i).Take(max).ToArray();
                    PacketsToSend.Add(new RUDPPacket()
                    {
                        Dst   = destination,
                        Id    = sq.PacketId,
                        Type  = type,
                        Flags = flags,
                        Data  = buf
                    });
                    i += _maxMTU;
                }
                foreach (RUDPPacket p in PacketsToSend)
                {
                    p.Qty = PacketsToSend.Count;
                    SendPacket(p);
                }
                sq.PacketId++;
            }
            else
            {
                throw new Exception("This should not happen");
            }
            if (sq.PacketId > PacketIdLimit)
            {
                sq.PacketId = 0;
            }
        }
Пример #2
0
        private int Send(IPEndPoint destination, RUDPPacketType type = RUDPPacketType.DAT, RUDPPacketFlags flags = RUDPPacketFlags.NUL, byte[] data = null, int[] intData = null, Action <RUDPPacket> OnPacketReceivedByDestination = null)
        {
            if (!_isAlive)
            {
                return(-1);
            }
            RUDPPacket         packet = null;
            bool               reset  = false;
            RUDPConnectionData cn     = GetConnection(destination);

            if ((data != null && data.Length < _maxMTU) || data == null)
            {
                packet = new RUDPPacket()
                {
                    Serializer = _serializer,
                    Dst        = destination,
                    Id         = cn.PacketId,
                    Type       = type,
                    Flags      = flags,
                    Data       = data,
                    intData    = intData,
                    OnPacketReceivedByDestination = OnPacketReceivedByDestination
                };
                SendPacket(packet);
                cn.PacketId++;
                if (!IsServer && cn.Local > SequenceLimit)
                {
                    reset = true;
                }
            }
            else if (data != null && data.Length >= _maxMTU)
            {
                int i = 0;
                List <RUDPPacket> PacketsToSend = new List <RUDPPacket>();
                while (i < data.Length)
                {
                    int min = i;
                    int max = _maxMTU;
                    if ((min + max) > data.Length)
                    {
                        max = data.Length - min;
                    }
                    byte[] buf = data.Skip(i).Take(max).ToArray();
                    PacketsToSend.Add(new RUDPPacket()
                    {
                        Serializer = _serializer,
                        Dst        = destination,
                        Id         = cn.PacketId,
                        Type       = type,
                        Flags      = flags,
                        Data       = buf,
                        intData    = intData
                    });
                    i += _maxMTU;
                }
                foreach (RUDPPacket p in PacketsToSend)
                {
                    p.Qty = PacketsToSend.Count;
                    p.OnPacketReceivedByDestination = OnPacketReceivedByDestination;
                    SendPacket(p);
                }
                cn.PacketId++;
                if (!IsServer && cn.Local > SequenceLimit)
                {
                    reset = true;
                }
                packet = PacketsToSend.First();
            }
            else
            {
                throw new Exception("This should not happen");
            }
            if (cn.PacketId > PacketIdLimit)
            {
                cn.PacketId = 0;
            }
            if (reset)
            {
                SendPacket(new RUDPPacket()
                {
                    Serializer = _serializer,
                    Dst        = destination,
                    Type       = RUDPPacketType.RST
                });
                cn.Local = IsServer ? ServerStartSequence : ClientStartSequence;
            }
            return(packet.Id);
        }