matrix-pinecone/types/varu64.go
Neil Alexander 1ce778fcde
Soft-SNEK (#43)
* Initial soft-state testing

* Fix `pineconeip`

* Try raising the intervals a bit

* Tighter expiry

* Tweaks

* Try to speed up convergence but reduce idle noise

* More tweaks

* Handle disconnections on destination ports, tweak next-hops

* All traffic is either bootstrap or not, more tweaking

* Protocol calming, loop detection, bootstrap signatures

* Tidy up sim

* Fix unit test

* Don't drop pong responses as looping traffic

* Remove unused snake path id

* Remove unused variable from nextHopsSNEK

* Unassign setup acks capability from soft state capabilites

* Cleanup comments to reflect new softstate logic

* Remove unused candidate variable from state

* Update sim and events to reflect softstate changes

* Update wireshark dissector to match softstate frames

* Add unit test for snake bootstrap

* Fix incorrect comment about bootstrap forwarding conditions

* Add `queue` interface

* Initial docs update for softstate algorithm

* Softstate doc updates

* Fix logic for selecting lowest latency links

* Update docs with watermark information (#52)

* Update docs with watermark information

* Further clarifications to watermark docs

* Fix `pinecone.lua`

Co-authored-by: Devon Hudson <devonhudson@librem.one>
2022-07-08 14:52:11 +01:00

59 lines
1.2 KiB
Go

// Copyright 2021 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import "fmt"
type Varu64 uint64
func (n Varu64) MarshalBinary(b []byte) (int, error) {
if len(b) < n.Length() {
return 0, fmt.Errorf("input slice too small")
}
l := n.Length()
i := l - 1
b[i] = byte(n & 0x7f)
for n >>= 7; n != 0; n >>= 7 {
i--
b[i] = byte(n | 0x80)
}
return l, nil
}
func (n *Varu64) UnmarshalBinary(buf []byte) (int, error) {
l := 0
*n = Varu64(0)
for _, b := range buf {
*n <<= 7
*n |= Varu64(b & 0x7f)
l++
if b&0x80 == 0 {
break
}
}
return l, nil
}
func (n Varu64) Length() int {
l := 1
for e := n >> 7; e > 0; e >>= 7 {
l++
}
return l
}
func (n Varu64) MinLength() int {
return 1
}