forked from quic-go/quic-go
implement a function to find the highest supported version
This commit is contained in:
@@ -3,6 +3,7 @@ package protocol
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -18,6 +19,7 @@ const (
|
||||
)
|
||||
|
||||
// SupportedVersions lists the versions that the server supports
|
||||
// must be in sorted order
|
||||
var SupportedVersions = []VersionNumber{
|
||||
Version34, Version35, Version36,
|
||||
}
|
||||
@@ -49,6 +51,33 @@ func IsSupportedVersion(v VersionNumber) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
type byVersionNumber []VersionNumber
|
||||
|
||||
func (a byVersionNumber) Len() int { return len(a) }
|
||||
func (a byVersionNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a byVersionNumber) Less(i, j int) bool { return a[i] < a[j] }
|
||||
|
||||
// HighestSupportedVersion finds the highest version number that is both present in other and in SupportedVersions
|
||||
// the versions in other do not need to be ordered
|
||||
// it returns true and the version number, if there is one, otherwise false
|
||||
func HighestSupportedVersion(other []VersionNumber) (bool, VersionNumber) {
|
||||
sort.Sort(byVersionNumber(other))
|
||||
|
||||
i := len(other) - 1 // index of other
|
||||
j := len(SupportedVersions) - 1 // index of SupportedVersions
|
||||
for i >= 0 && j >= 0 {
|
||||
if other[i] == SupportedVersions[j] {
|
||||
return true, SupportedVersions[j]
|
||||
}
|
||||
if other[i] > SupportedVersions[j] {
|
||||
i--
|
||||
} else {
|
||||
j--
|
||||
}
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
|
||||
func init() {
|
||||
var b bytes.Buffer
|
||||
for _, v := range SupportedVersions {
|
||||
|
||||
Reference in New Issue
Block a user