Merge pull request #65 from wolfendale/connection-state-checks

Connection State Changes
This commit is contained in:
Daniel López Guimaraes 2024-06-14 22:29:19 +02:00 committed by GitHub
commit 090cfb5568
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 17 deletions

View file

@ -30,6 +30,11 @@ func (pdq *PacketDispatchQueue) Dispatched(packet PRUDPPacketInterface) {
delete(pdq.queue, packet.SequenceID())
}
// Purge clears the queue of all pending packets.
func (pdq *PacketDispatchQueue) Purge() {
clear(pdq.queue)
}
// NewPacketDispatchQueue initializes a new PacketDispatchQueue with a starting counter value.
func NewPacketDispatchQueue() *PacketDispatchQueue {
return &PacketDispatchQueue{

View file

@ -62,6 +62,10 @@ func (pc *PRUDPConnection) SetPID(pid *types.PID) {
// reset resets the connection state to all zero values
func (pc *PRUDPConnection) reset() {
pc.packetDispatchQueues.Clear(func(_ uint8, packetDispatchQueue *PacketDispatchQueue) {
packetDispatchQueue.Purge()
})
pc.slidingWindows.Clear(func(_ uint8, slidingWindow *SlidingWindow) {
slidingWindow.ResendScheduler.Stop()
})

View file

@ -126,7 +126,6 @@ func (pep *PRUDPEndPoint) processPacket(packet PRUDPPacketInterface, socket *Soc
}
packet.SetSender(connection)
connection.resetHeartbeat()
if packet.HasFlag(constants.PacketFlagAck) || packet.HasFlag(constants.PacketFlagMultiAck) {
pep.handleAcknowledgment(packet)
@ -142,12 +141,13 @@ func (pep *PRUDPEndPoint) processPacket(packet PRUDPPacketInterface, socket *Soc
func (pep *PRUDPEndPoint) handleAcknowledgment(packet PRUDPPacketInterface) {
connection := packet.Sender().(*PRUDPConnection)
if connection.ConnectionState != StateConnected {
// TODO - Log this?
// * Connection is in a bad state, drop the packet and let it die
if connection.ConnectionState < StateConnected {
return
}
connection.resetHeartbeat()
if packet.HasFlag(constants.PacketFlagMultiAck) {
pep.handleMultiAcknowledgment(packet)
return
@ -207,12 +207,7 @@ func (pep *PRUDPEndPoint) handleMultiAcknowledgment(packet PRUDPPacketInterface)
func (pep *PRUDPEndPoint) handleSyn(packet PRUDPPacketInterface) {
connection := packet.Sender().(*PRUDPConnection)
if connection.ConnectionState != StateNotConnected {
// TODO - Log this?
// * Connection is in a bad state, drop the packet and let it die
return
}
connection.resetHeartbeat()
var ack PRUDPPacketInterface
@ -259,12 +254,12 @@ func (pep *PRUDPEndPoint) handleSyn(packet PRUDPPacketInterface) {
func (pep *PRUDPEndPoint) handleConnect(packet PRUDPPacketInterface) {
connection := packet.Sender().(*PRUDPConnection)
if connection.ConnectionState != StateConnecting {
// TODO - Log this?
// * Connection is in a bad state, drop the packet and let it die
if connection.ConnectionState < StateConnecting {
return
}
connection.resetHeartbeat()
var ack PRUDPPacketInterface
if packet.Version() == 2 {
@ -387,12 +382,12 @@ func (pep *PRUDPEndPoint) handleConnect(packet PRUDPPacketInterface) {
func (pep *PRUDPEndPoint) handleData(packet PRUDPPacketInterface) {
connection := packet.Sender().(*PRUDPConnection)
if connection.ConnectionState != StateConnected {
// TODO - Log this?
// * Connection is in a bad state, drop the packet and let it die
if connection.ConnectionState < StateConnected {
return
}
connection.resetHeartbeat()
if packet.HasFlag(constants.PacketFlagReliable) {
pep.handleReliable(packet)
} else {
@ -420,12 +415,19 @@ func (pep *PRUDPEndPoint) handleDisconnect(packet PRUDPPacketInterface) {
}
func (pep *PRUDPEndPoint) handlePing(packet PRUDPPacketInterface) {
connection := packet.Sender().(*PRUDPConnection)
if connection.ConnectionState < StateConnected {
return
}
connection.resetHeartbeat()
if packet.HasFlag(constants.PacketFlagNeedsAck) {
pep.acknowledgePacket(packet)
}
if packet.HasFlag(constants.PacketFlagReliable) {
connection := packet.Sender().(*PRUDPConnection)
connection.Lock()
defer connection.Unlock()