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; } }
private List <RUDPPacket> PreparePackets(RUDPPacketType type, byte[] data) { List <RUDPPacket> rv = new List <RUDPPacket>(); if ((data != null && data.Length <= _maxMTU) || data == null) { lock (_seqMutex) { rv.Add(new RUDPPacket() { Serializer = Connection.Serializer, Channel = Id, Qty = 0, Data = data, Type = type, Seq = Local, Id = PacketId }); Local++; PacketId++; } } else if (data != null && data.Length >= _maxMTU) { int i = 0; while (i < data.Length) { int min = i; int max = _maxMTU; if ((min + max) > data.Length) { max = data.Length - min; } byte[] buf = new byte[max]; Buffer.BlockCopy(data, i, buf, 0, max); rv.Add(new RUDPPacket() { Serializer = Connection.Serializer, Channel = Id, Id = PacketId, Seq = Local, Type = type, Data = buf }); i += _maxMTU; Local++; } foreach (RUDPPacket p in rv) { p.Qty = rv.Count; } PacketId++; } foreach (RUDPPacket p in rv) { RUDPConnection.Trace("Prepared packet {0}", p); } return(rv); }
private void SendPacket(RUDPPacketType type, byte[] data = null) { lock (_sendMutex) _sendQueue.AddRange(PreparePackets(type, data)); _evSend.Set(); }
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); }