github.com/sagernet/quic-go@v0.43.1-beta.1/ech/closed_conn.go (about)

     1  package quic
     2  
     3  import (
     4  	"math/bits"
     5  	"net"
     6  
     7  	"github.com/sagernet/quic-go/internal/utils"
     8  )
     9  
    10  // A closedLocalConn is a connection that we closed locally.
    11  // When receiving packets for such a connection, we need to retransmit the packet containing the CONNECTION_CLOSE frame,
    12  // with an exponential backoff.
    13  type closedLocalConn struct {
    14  	counter uint32
    15  	logger  utils.Logger
    16  
    17  	sendPacket func(net.Addr, packetInfo)
    18  }
    19  
    20  var _ packetHandler = &closedLocalConn{}
    21  
    22  // newClosedLocalConn creates a new closedLocalConn and runs it.
    23  func newClosedLocalConn(sendPacket func(net.Addr, packetInfo), logger utils.Logger) packetHandler {
    24  	return &closedLocalConn{
    25  		sendPacket: sendPacket,
    26  		logger:     logger,
    27  	}
    28  }
    29  
    30  func (c *closedLocalConn) handlePacket(p receivedPacket) {
    31  	c.counter++
    32  	// exponential backoff
    33  	// only send a CONNECTION_CLOSE for the 1st, 2nd, 4th, 8th, 16th, ... packet arriving
    34  	if bits.OnesCount32(c.counter) != 1 {
    35  		return
    36  	}
    37  	c.logger.Debugf("Received %d packets after sending CONNECTION_CLOSE. Retransmitting.", c.counter)
    38  	c.sendPacket(p.remoteAddr, p.info)
    39  }
    40  
    41  func (c *closedLocalConn) destroy(error)                              {}
    42  func (c *closedLocalConn) closeWithTransportError(TransportErrorCode) {}
    43  
    44  // A closedRemoteConn is a connection that was closed remotely.
    45  // For such a connection, we might receive reordered packets that were sent before the CONNECTION_CLOSE.
    46  // We can just ignore those packets.
    47  type closedRemoteConn struct{}
    48  
    49  var _ packetHandler = &closedRemoteConn{}
    50  
    51  func newClosedRemoteConn() packetHandler {
    52  	return &closedRemoteConn{}
    53  }
    54  
    55  func (c *closedRemoteConn) handlePacket(receivedPacket)                {}
    56  func (c *closedRemoteConn) destroy(error)                              {}
    57  func (c *closedRemoteConn) closeWithTransportError(TransportErrorCode) {}