mirror of
https://github.com/refraction-networking/uquic.git
synced 2025-04-03 20:27:35 +03:00
The speedup comes from multiple sources: 1. We now preallocate a byte slice, instead of appending multiple times. 2. Marshaling into a byte slice is faster than using a bytes.Buffer. 3. quicvarint.Write allocates, while quicvarint.Append doesn't.
47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package handshake
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/wire"
|
|
"github.com/lucas-clemente/quic-go/quicvarint"
|
|
)
|
|
|
|
const sessionTicketRevision = 2
|
|
|
|
type sessionTicket struct {
|
|
Parameters *wire.TransportParameters
|
|
RTT time.Duration // to be encoded in mus
|
|
}
|
|
|
|
func (t *sessionTicket) Marshal() []byte {
|
|
b := make([]byte, 0, 256)
|
|
b = quicvarint.Append(b, sessionTicketRevision)
|
|
b = quicvarint.Append(b, uint64(t.RTT.Microseconds()))
|
|
return t.Parameters.MarshalForSessionTicket(b)
|
|
}
|
|
|
|
func (t *sessionTicket) Unmarshal(b []byte) error {
|
|
r := bytes.NewReader(b)
|
|
rev, err := quicvarint.Read(r)
|
|
if err != nil {
|
|
return errors.New("failed to read session ticket revision")
|
|
}
|
|
if rev != sessionTicketRevision {
|
|
return fmt.Errorf("unknown session ticket revision: %d", rev)
|
|
}
|
|
rtt, err := quicvarint.Read(r)
|
|
if err != nil {
|
|
return errors.New("failed to read RTT")
|
|
}
|
|
var tp wire.TransportParameters
|
|
if err := tp.UnmarshalFromSessionTicket(r); err != nil {
|
|
return fmt.Errorf("unmarshaling transport parameters from session ticket failed: %s", err.Error())
|
|
}
|
|
t.Parameters = &tp
|
|
t.RTT = time.Duration(rtt) * time.Microsecond
|
|
return nil
|
|
}
|