internal async Task SendDatagramAsync(RakSession session, Datagram datagram) { if (datagram.MessageParts.Count == 0) { Log.Warn($"Failed to send #{datagram.Header.DatagramSequenceNumber.IntValue()}"); datagram.PutPool(); return; } if (datagram.TransmissionCount > 10) { if (Log.IsDebugEnabled) { Log.Warn($"Retransmission count exceeded. No more resend of #{datagram.Header.DatagramSequenceNumber.IntValue()} Type: {datagram.FirstMessageId} (0x{datagram.FirstMessageId:x2}) for {session.Username}"); } datagram.PutPool(); Interlocked.Increment(ref ServerInfo.NumberOfFails); //TODO: Disconnect! Because of encryption, this connection can't be used after this point return; } datagram.Header.DatagramSequenceNumber = Interlocked.Increment(ref session.DatagramSequenceNumber); datagram.TransmissionCount++; datagram.RetransmitImmediate = false; byte[] buffer = ArrayPool <byte> .Shared.Rent(1600); int length = (int)datagram.GetEncoded(ref buffer); datagram.Timer.Restart(); if (!session.WaitingForAckQueue.TryAdd(datagram.Header.DatagramSequenceNumber.IntValue(), datagram)) { Log.Warn($"Datagram sequence unexpectedly existed in the ACK/NAK queue already {datagram.Header.DatagramSequenceNumber.IntValue()}"); datagram.PutPool(); } //lock (session.SyncRoot) { await SendDataAsync(buffer, length, session.EndPoint); ArrayPool <byte> .Shared.Return(buffer); } }
internal void SendDatagram(PlayerNetworkSession session, Datagram datagram) { if (datagram.MessageParts.Count == 0) { datagram.PutPool(); Log.WarnFormat("Failed to resend #{0}", datagram.Header.datagramSequenceNumber.IntValue()); return; } if (datagram.TransmissionCount > 10) { if (Log.IsDebugEnabled) { Log.WarnFormat("Retransmission count exceeded. No more resend of #{0} Type: {2} (0x{2:x2}) for {1}", datagram.Header.datagramSequenceNumber.IntValue(), session.Username, datagram.FirstMessageId); } datagram.PutPool(); Interlocked.Increment(ref ServerInfo.NumberOfFails); return; } datagram.Header.datagramSequenceNumber = Interlocked.Increment(ref session.DatagramSequenceNumber); datagram.TransmissionCount++; datagram.RetransmitImmediate = false; //byte[] data = datagram.Encode(); byte[] data; var lenght = (int)datagram.GetEncoded(out data); datagram.Timer.Restart(); if (!session.WaitingForAcksQueue.TryAdd(datagram.Header.datagramSequenceNumber.IntValue(), datagram)) { Log.Warn(string.Format("Datagram sequence unexpectedly existed in the ACK/NAK queue already {0}", datagram.Header.datagramSequenceNumber.IntValue())); } lock (session.SyncRoot) { SendData(data, lenght, session.EndPoint); } }