@@ -38,6 +38,7 @@ type piggybackingController struct {
3838 acks []uint32
3939 dtlsCallback func (packet []byte , rAddr net.Addr )
4040 newFlight bool
41+ connected bool
4142}
4243
4344// init sets the controller to its initial off state. SetDtlsCallback flips it
@@ -54,6 +55,7 @@ func (p *piggybackingController) init() {
5455func (p * piggybackingController ) flushOnConnected () []packetWithCrc {
5556 p .mu .Lock ()
5657 defer p .mu .Unlock ()
58+ p .connected = true
5759 if p .state != PiggybackingStateOff {
5860 return nil
5961 }
@@ -77,27 +79,29 @@ func (a *Agent) SetDtlsCallback(cb func(packet []byte, rAddr net.Addr)) {
7779
7880// Piggyback stores a packet to be picked in a round-robin fashion.
7981// Returns `true` if packet is to be consumed.
82+ // A nil packet signals that the local DTLS handshake completed.
8083func (a * Agent ) Piggyback (packet []byte , end bool ) bool {
8184 a .piggyback .mu .Lock ()
8285 defer a .piggyback .mu .Unlock ()
83- if a .piggyback .state == PiggybackingStateOff {
84- return a . connectionState != ConnectionStateConnected
86+ if a .piggyback .state == PiggybackingStateOff && a . piggyback . connected {
87+ return false
8588 }
8689
8790 if packet != nil {
8891 // If we receive a packet after the end of a flight we need
8992 // to clear the outgoing list.
9093 if a .piggyback .newFlight {
9194 a .piggyback .packets = []packetWithCrc {}
95+ a .piggyback .packetsIndex = 0
9296 }
9397 a .piggyback .newFlight = end
9498 crc := crc32 .ChecksumIEEE (packet )
9599 a .piggyback .packets = append (a .piggyback .packets , packetWithCrc {packet , crc })
96- } else {
100+ } else if a . piggyback . state != PiggybackingStateOff {
97101 a .piggyback .state = PiggybackingStatePending
98102 }
99103 // If we are connected we could send DTLS plain.
100- return true // a.connectionState == ConnectionStateConnected
104+ return true
101105}
102106
103107// GetPiggybackDataAndAcks returns a packet from the stored list in a round-robin fashion and a list of acks.
0 commit comments