2022-04-01 16:16:46 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/protocol"
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/utils"
|
|
|
|
)
|
|
|
|
|
2022-08-19 16:34:07 +00: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,
|
2022-04-01 16:16:46 +00:00
|
|
|
// with an exponential backoff.
|
2022-08-19 16:34:07 +00:00
|
|
|
type closedLocalConn struct {
|
2022-04-01 16:16:46 +00:00
|
|
|
conn sendConn
|
|
|
|
connClosePacket []byte
|
|
|
|
|
|
|
|
closeOnce sync.Once
|
2022-08-19 16:34:07 +00:00
|
|
|
closeChan chan struct{} // is closed when the connection is closed or destroyed
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
receivedPackets chan *receivedPacket
|
|
|
|
counter uint64 // number of packets received
|
|
|
|
|
|
|
|
perspective protocol.Perspective
|
|
|
|
|
|
|
|
logger utils.Logger
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
var _ packetHandler = &closedLocalConn{}
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// newClosedLocalConn creates a new closedLocalConn and runs it.
|
|
|
|
func newClosedLocalConn(
|
2022-04-01 16:16:46 +00:00
|
|
|
conn sendConn,
|
|
|
|
connClosePacket []byte,
|
|
|
|
perspective protocol.Perspective,
|
|
|
|
logger utils.Logger,
|
|
|
|
) packetHandler {
|
2022-08-19 16:34:07 +00:00
|
|
|
s := &closedLocalConn{
|
2022-04-01 16:16:46 +00:00
|
|
|
conn: conn,
|
|
|
|
connClosePacket: connClosePacket,
|
|
|
|
perspective: perspective,
|
|
|
|
logger: logger,
|
|
|
|
closeChan: make(chan struct{}),
|
|
|
|
receivedPackets: make(chan *receivedPacket, 64),
|
|
|
|
}
|
|
|
|
go s.run()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedLocalConn) run() {
|
2022-04-01 16:16:46 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case p := <-s.receivedPackets:
|
|
|
|
s.handlePacketImpl(p)
|
|
|
|
case <-s.closeChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedLocalConn) handlePacket(p *receivedPacket) {
|
2022-04-01 16:16:46 +00:00
|
|
|
select {
|
|
|
|
case s.receivedPackets <- p:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedLocalConn) handlePacketImpl(_ *receivedPacket) {
|
2022-04-01 16:16:46 +00:00
|
|
|
s.counter++
|
|
|
|
// exponential backoff
|
|
|
|
// only send a CONNECTION_CLOSE for the 1st, 2nd, 4th, 8th, 16th, ... packet arriving
|
|
|
|
for n := s.counter; n > 1; n = n / 2 {
|
|
|
|
if n%2 != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.logger.Debugf("Received %d packets after sending CONNECTION_CLOSE. Retransmitting.", s.counter)
|
|
|
|
if err := s.conn.Write(s.connClosePacket); err != nil {
|
|
|
|
s.logger.Debugf("Error retransmitting CONNECTION_CLOSE: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedLocalConn) shutdown() {
|
2022-04-01 16:16:46 +00:00
|
|
|
s.destroy(nil)
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedLocalConn) destroy(error) {
|
2022-04-01 16:16:46 +00:00
|
|
|
s.closeOnce.Do(func() {
|
|
|
|
close(s.closeChan)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedLocalConn) getPerspective() protocol.Perspective {
|
2022-04-01 16:16:46 +00:00
|
|
|
return s.perspective
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00: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.
|
2022-04-01 16:16:46 +00:00
|
|
|
// We can just ignore those packets.
|
2022-08-19 16:34:07 +00:00
|
|
|
type closedRemoteConn struct {
|
2022-04-01 16:16:46 +00:00
|
|
|
perspective protocol.Perspective
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
var _ packetHandler = &closedRemoteConn{}
|
2022-04-01 16:16:46 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func newClosedRemoteConn(pers protocol.Perspective) packetHandler {
|
|
|
|
return &closedRemoteConn{perspective: pers}
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func (s *closedRemoteConn) handlePacket(*receivedPacket) {}
|
|
|
|
func (s *closedRemoteConn) shutdown() {}
|
|
|
|
func (s *closedRemoteConn) destroy(error) {}
|
|
|
|
func (s *closedRemoteConn) getPerspective() protocol.Perspective { return s.perspective }
|