Source file src/vendor/golang.org/x/net/quic/sent_val.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package quic
     6  
     7  // A sentVal tracks sending some piece of information to the peer.
     8  // It tracks whether the information has been sent, acked, and
     9  // (when in-flight) the most recent packet to carry it.
    10  //
    11  // For example, a sentVal can track sending of a RESET_STREAM frame.
    12  //
    13  //   - unset: stream is active, no need to send RESET_STREAM
    14  //   - unsent: we should send a RESET_STREAM, but have not yet
    15  //   - sent: we have sent a RESET_STREAM, but have not received an ack
    16  //   - received: we have sent a RESET_STREAM, and the peer has acked the packet that contained it
    17  //
    18  // In the "sent" state, a sentVal also tracks the latest packet number to carry
    19  // the information. (QUIC packet numbers are always at most 62 bits in size,
    20  // so the sentVal keeps the number in the low 62 bits and the state in the high 2 bits.)
    21  type sentVal uint64
    22  
    23  const (
    24  	sentValUnset    = 0       // unset
    25  	sentValUnsent   = 1 << 62 // set, not sent to the peer
    26  	sentValSent     = 2 << 62 // set, sent to the peer but not yet acked; pnum is set
    27  	sentValReceived = 3 << 62 // set, peer acked receipt
    28  
    29  	sentValStateMask = 3 << 62
    30  )
    31  
    32  // isSet reports whether the value is set.
    33  func (s sentVal) isSet() bool { return s != 0 }
    34  
    35  // shouldSend reports whether the value is set and has not been sent to the peer.
    36  func (s sentVal) shouldSend() bool { return s.state() == sentValUnsent }
    37  
    38  // shouldSendPTO reports whether the value needs to be sent to the peer.
    39  // The value needs to be sent if it is set and has not been sent.
    40  // If pto is true, indicating that we are sending a PTO probe, the value
    41  // should also be sent if it is set and has not been acknowledged.
    42  func (s sentVal) shouldSendPTO(pto bool) bool {
    43  	st := s.state()
    44  	return st == sentValUnsent || (pto && st == sentValSent)
    45  }
    46  
    47  // isReceived reports whether the value has been received by the peer.
    48  func (s sentVal) isReceived() bool { return s == sentValReceived }
    49  
    50  // set sets the value and records that it should be sent to the peer.
    51  // If the value has already been sent, it is not resent.
    52  func (s *sentVal) set() {
    53  	if *s == 0 {
    54  		*s = sentValUnsent
    55  	}
    56  }
    57  
    58  // reset sets the value to the unsent state.
    59  func (s *sentVal) setUnsent() { *s = sentValUnsent }
    60  
    61  // clear sets the value to the unset state.
    62  func (s *sentVal) clear() { *s = sentValUnset }
    63  
    64  // setSent sets the value to the send state and records the number of the most recent
    65  // packet containing the value.
    66  func (s *sentVal) setSent(pnum packetNumber) {
    67  	*s = sentValSent | sentVal(pnum)
    68  }
    69  
    70  // setReceived sets the value to the received state.
    71  func (s *sentVal) setReceived() { *s = sentValReceived }
    72  
    73  // ackOrLoss reports that an acknowledgement has been received for the value,
    74  // or that the packet carrying the value has been lost.
    75  func (s *sentVal) ackOrLoss(pnum packetNumber, fate packetFate) {
    76  	if fate == packetAcked {
    77  		*s = sentValReceived
    78  	} else if *s == sentVal(pnum)|sentValSent {
    79  		*s = sentValUnsent
    80  	}
    81  }
    82  
    83  // ackLatestOrLoss reports that an acknowledgement has been received for the value,
    84  // or that the packet carrying the value has been lost.
    85  // The value is set to the acked state only if pnum is the latest packet containing it.
    86  //
    87  // We use this to handle acks for data that varies every time it is sent.
    88  // For example, if we send a MAX_DATA frame followed by an updated MAX_DATA value in a
    89  // second packet, we consider the data sent only upon receiving an ack for the most
    90  // recent value.
    91  func (s *sentVal) ackLatestOrLoss(pnum packetNumber, fate packetFate) {
    92  	if fate == packetAcked {
    93  		if *s == sentVal(pnum)|sentValSent {
    94  			*s = sentValReceived
    95  		}
    96  	} else {
    97  		if *s == sentVal(pnum)|sentValSent {
    98  			*s = sentValUnsent
    99  		}
   100  	}
   101  }
   102  
   103  func (s sentVal) state() uint64 { return uint64(s) & sentValStateMask }
   104  

View as plain text