Implement and use deleteConnectionByID

This commit is contained in:
PabloMK7 2024-04-18 14:47:40 +02:00
parent 509b274086
commit 90d81f6eb9
4 changed files with 26 additions and 10 deletions

View file

@ -43,6 +43,23 @@ func (m *MutexMap[K, V]) Delete(key K) {
delete(m.real, key)
}
// DeleteIf deletes every element if the callback returns true.
// Returns the amount of elements deleted.
func (m *MutexMap[K, V]) DeleteIf(callback func(key K, value V) bool) int {
m.Lock()
defer m.Unlock()
amout := 0
for key, value := range m.real {
if callback(key, value) {
delete(m.real, key)
amout++
}
}
return amout
}
// RunAndDelete runs a callback and removes the key afterwards
func (m *MutexMap[K, V]) RunAndDelete(key K, callback func(key K, value V)) {
m.Lock()

View file

@ -2,7 +2,6 @@ package nex
import (
"crypto/md5"
"fmt"
"net"
"time"
@ -195,9 +194,7 @@ func (pc *PRUDPConnection) startHeartbeat() {
pc.pingKickTimer = time.AfterFunc(maxSilenceTime, func() {
pc.cleanup() // * "removed" event is dispatched here
discriminator := fmt.Sprintf("%s-%d-%d", pc.Socket.Address.String(), pc.StreamType, pc.StreamID)
endpoint.Connections.Delete(discriminator)
endpoint.deleteConnectionByID(pc.ID)
})
})
}

View file

@ -100,6 +100,13 @@ func (pep *PRUDPEndPoint) EmitError(err *Error) {
}
}
// deleteConnectionByID deletes the connection with the specified ID
func (pep *PRUDPEndPoint) deleteConnectionByID(cid uint32) {
pep.Connections.DeleteIf(func(key string, value *PRUDPConnection) bool {
return value.ID == cid
})
}
func (pep *PRUDPEndPoint) processPacket(packet PRUDPPacketInterface, socket *SocketConnection) {
streamType := packet.SourceVirtualPortStreamType()
streamID := packet.SourceVirtualPortStreamID()

View file

@ -1,7 +1,6 @@
package nex
import (
"fmt"
"time"
)
@ -105,11 +104,7 @@ func (rs *ResendScheduler) resendPacket(pendingPacket *PendingPacket) {
rs.packets.Delete(packet.SequenceID())
connection.cleanup() // * "removed" event is dispatched here
streamType := packet.SourceVirtualPortStreamType()
streamID := packet.SourceVirtualPortStreamID()
discriminator := fmt.Sprintf("%s-%d-%d", packet.Sender().Address().String(), streamType, streamID)
connection.endpoint.Connections.Delete(discriminator)
connection.endpoint.deleteConnectionByID(connection.ID)
return
}