Skip to content

Commit

Permalink
Merge pull request #46 from PabloMK7/disconnectbyid
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbarrow authored Apr 19, 2024
2 parents e2d333d + ba656ff commit 5f38bf3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
17 changes: 17 additions & 0 deletions mutex_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ func (m *MutexMap[K, V]) Delete(key K) {
delete(m.real, key)
}

// DeleteIf deletes every element if the predicate returns true.
// Returns the amount of elements deleted.
func (m *MutexMap[K, V]) DeleteIf(predicate func(key K, value V) bool) int {
m.Lock()
defer m.Unlock()

amount := 0
for key, value := range m.real {
if predicate(key, value) {
delete(m.real, key)
amount++
}
}

return amount
}

// 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()
Expand Down
5 changes: 1 addition & 4 deletions prudp_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nex

import (
"crypto/md5"
"fmt"
"net"
"time"

Expand Down Expand Up @@ -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)
})
})
}
Expand Down
7 changes: 7 additions & 0 deletions prudp_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 1 addition & 6 deletions resend_scheduler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nex

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -112,11 +111,7 @@ func (rs *ResendScheduler) resendPacket(pendingPacket *PendingPacket) bool {
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 true
}
Expand Down

0 comments on commit 5f38bf3

Please sign in to comment.