github.com/sagernet/quic-go@v0.43.1-beta.1/internal/logutils/frame.go (about)

     1  package logutils
     2  
     3  import (
     4  	"github.com/sagernet/quic-go/internal/protocol"
     5  	"github.com/sagernet/quic-go/internal/wire"
     6  	"github.com/sagernet/quic-go/logging"
     7  )
     8  
     9  // ConvertFrame converts a wire.Frame into a logging.Frame.
    10  // This makes it possible for external packages to access the frames.
    11  // Furthermore, it removes the data slices from CRYPTO and STREAM frames.
    12  func ConvertFrame(frame wire.Frame) logging.Frame {
    13  	switch f := frame.(type) {
    14  	case *wire.AckFrame:
    15  		// We use a pool for ACK frames.
    16  		// Implementations of the tracer interface may hold on to frames, so we need to make a copy here.
    17  		return ConvertAckFrame(f)
    18  	case *wire.CryptoFrame:
    19  		return &logging.CryptoFrame{
    20  			Offset: f.Offset,
    21  			Length: protocol.ByteCount(len(f.Data)),
    22  		}
    23  	case *wire.StreamFrame:
    24  		return &logging.StreamFrame{
    25  			StreamID: f.StreamID,
    26  			Offset:   f.Offset,
    27  			Length:   f.DataLen(),
    28  			Fin:      f.Fin,
    29  		}
    30  	case *wire.DatagramFrame:
    31  		return &logging.DatagramFrame{
    32  			Length: logging.ByteCount(len(f.Data)),
    33  		}
    34  	default:
    35  		return logging.Frame(frame)
    36  	}
    37  }
    38  
    39  func ConvertAckFrame(f *wire.AckFrame) *logging.AckFrame {
    40  	ranges := make([]wire.AckRange, 0, len(f.AckRanges))
    41  	ranges = append(ranges, f.AckRanges...)
    42  	ack := &logging.AckFrame{
    43  		AckRanges: ranges,
    44  		DelayTime: f.DelayTime,
    45  		ECNCE:     f.ECNCE,
    46  		ECT0:      f.ECT0,
    47  		ECT1:      f.ECT1,
    48  	}
    49  	return ack
    50  }