Refactor increments, rando connID
This commit is contained in:
parent
4afe8f17bb
commit
f7fa4fb688
3 changed files with 16 additions and 18 deletions
|
@ -101,6 +101,14 @@ type Packet interface {
|
|||
io.WriterTo
|
||||
}
|
||||
|
||||
// Inc increments a uint16. It avoids 0 (65535 + 1 = 1).
|
||||
func Inc(p *uint16) {
|
||||
*p++
|
||||
if *p == 0 {
|
||||
*p++
|
||||
}
|
||||
}
|
||||
|
||||
// ParsePacket parses the body of a UDP packet for a domain header, and then
|
||||
// based on the packet type, an AURP-Tr header, an AURP routing header, and
|
||||
// then a particular packet type.
|
||||
|
|
|
@ -69,20 +69,6 @@ type Transport struct {
|
|||
LocalSeq, RemoteSeq uint16
|
||||
}
|
||||
|
||||
func (tr *Transport) IncLocalSeq() {
|
||||
tr.LocalSeq++
|
||||
if tr.LocalSeq == 0 {
|
||||
tr.LocalSeq = 1
|
||||
}
|
||||
}
|
||||
|
||||
func (tr *Transport) IncRemoteSeq() {
|
||||
tr.RemoteSeq++
|
||||
if tr.RemoteSeq == 0 {
|
||||
tr.RemoteSeq = 1
|
||||
}
|
||||
}
|
||||
|
||||
// domainHeader returns a new domain header suitable for sending a packet.
|
||||
func (tr *Transport) domainHeader(pt PacketType) DomainHeader {
|
||||
return DomainHeader{
|
||||
|
|
12
main.go
12
main.go
|
@ -21,6 +21,7 @@ import (
|
|||
"errors"
|
||||
"flag"
|
||||
"log"
|
||||
"math/rand/v2"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
@ -74,7 +75,10 @@ func main() {
|
|||
log.Printf("EtherTalk configuration: %+v", cfg.EtherTalk)
|
||||
|
||||
peers := make(map[udpAddr]*peer)
|
||||
nextConnID := uint16(1)
|
||||
var nextConnID uint16
|
||||
for nextConnID == 0 {
|
||||
nextConnID = uint16(rand.IntN(0x10000))
|
||||
}
|
||||
|
||||
ln, err := net.ListenUDP("udp4", &net.UDPAddr{Port: int(cfg.ListenPort)})
|
||||
if err != nil {
|
||||
|
@ -124,9 +128,9 @@ func main() {
|
|||
raddr: raddr,
|
||||
recv: make(chan aurp.Packet, 1024),
|
||||
}
|
||||
nextConnID++
|
||||
goHandler(peer)
|
||||
aurp.Inc(&nextConnID)
|
||||
peers[udpAddrFromNet(raddr)] = peer
|
||||
goHandler(peer)
|
||||
}
|
||||
|
||||
// Incoming packet loop
|
||||
|
@ -176,7 +180,6 @@ func main() {
|
|||
pr := peers[ra]
|
||||
if pr == nil {
|
||||
// New peer!
|
||||
nextConnID++
|
||||
pr = &peer{
|
||||
cfg: cfg,
|
||||
tr: &aurp.Transport{
|
||||
|
@ -188,6 +191,7 @@ func main() {
|
|||
raddr: raddr,
|
||||
recv: make(chan aurp.Packet, 1024),
|
||||
}
|
||||
aurp.Inc(&nextConnID)
|
||||
peers[ra] = pr
|
||||
goHandler(pr)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue