mirror of
https://github.com/PretendoNetwork/nex-go.git
synced 2025-04-02 11:02:14 -04:00
Implement and use deleteConnectionByID
This commit is contained in:
parent
509b274086
commit
90d81f6eb9
4 changed files with 26 additions and 10 deletions
17
mutex_map.go
17
mutex_map.go
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue