dnscrypt-proxy/vendor/github.com/quic-go/quic-go/closed_conn.go

58 lines
1.8 KiB
Go
Raw Normal View History

2022-07-21 18:50:10 +02:00
package quic
import (
2022-08-30 20:45:06 +02:00
"math/bits"
"net"
2022-07-21 18:50:10 +02:00
"github.com/quic-go/quic-go/internal/utils"
2022-07-21 18:50:10 +02:00
)
// A closedLocalConn is a connection that we closed locally.
// When receiving packets for such a connection, we need to retransmit the packet containing the CONNECTION_CLOSE frame,
// with an exponential backoff.
type closedLocalConn struct {
2024-03-26 19:56:06 +01:00
counter uint32
logger utils.Logger
2022-07-21 18:50:10 +02:00
sendPacket func(net.Addr, packetInfo)
2022-07-21 18:50:10 +02:00
}
var _ packetHandler = &closedLocalConn{}
// newClosedLocalConn creates a new closedLocalConn and runs it.
2024-03-26 19:56:06 +01:00
func newClosedLocalConn(sendPacket func(net.Addr, packetInfo), logger utils.Logger) packetHandler {
2022-08-30 20:45:06 +02:00
return &closedLocalConn{
2024-03-26 19:56:06 +01:00
sendPacket: sendPacket,
logger: logger,
2022-07-21 18:50:10 +02:00
}
}
func (c *closedLocalConn) handlePacket(p receivedPacket) {
2022-08-30 20:45:06 +02:00
c.counter++
2022-07-21 18:50:10 +02:00
// exponential backoff
// only send a CONNECTION_CLOSE for the 1st, 2nd, 4th, 8th, 16th, ... packet arriving
2022-08-30 20:45:06 +02:00
if bits.OnesCount32(c.counter) != 1 {
return
2022-07-21 18:50:10 +02:00
}
2022-08-30 20:45:06 +02:00
c.logger.Debugf("Received %d packets after sending CONNECTION_CLOSE. Retransmitting.", c.counter)
c.sendPacket(p.remoteAddr, p.info)
2022-07-21 18:50:10 +02:00
}
2024-03-26 19:56:06 +01:00
func (c *closedLocalConn) destroy(error) {}
func (c *closedLocalConn) closeWithTransportError(TransportErrorCode) {}
2022-07-21 18:50:10 +02:00
// A closedRemoteConn is a connection that was closed remotely.
// For such a connection, we might receive reordered packets that were sent before the CONNECTION_CLOSE.
// We can just ignore those packets.
2024-03-26 19:56:06 +01:00
type closedRemoteConn struct{}
2022-07-21 18:50:10 +02:00
var _ packetHandler = &closedRemoteConn{}
2024-03-26 19:56:06 +01:00
func newClosedRemoteConn() packetHandler {
return &closedRemoteConn{}
2022-07-21 18:50:10 +02:00
}
2024-03-26 19:56:06 +01:00
func (c *closedRemoteConn) handlePacket(receivedPacket) {}
func (c *closedRemoteConn) destroy(error) {}
func (c *closedRemoteConn) closeWithTransportError(TransportErrorCode) {}