5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-29 21:21:35 +00:00
This commit is contained in:
Neil Alexander 2019-04-19 00:33:54 +01:00
parent b20c8b6da5
commit c593721362
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -27,10 +27,11 @@ func (c *Conn) startSearch() {
} }
if sinfo != nil { if sinfo != nil {
c.session = sinfo c.session = sinfo
c.core.log.Println("Search from API found", hex.EncodeToString(sinfo.theirPermPub[:])) c.core.log.Println("Search from API succeeded")
c.core.log.Println("Pubkey:", hex.EncodeToString(sinfo.theirPermPub[:]))
c.core.log.Println("Coords:", sinfo.coords)
} }
} }
// Try and search for the node on the network
doSearch := func() { doSearch := func() {
sinfo, isIn := c.core.searches.searches[*c.nodeID] sinfo, isIn := c.core.searches.searches[*c.nodeID]
if !isIn { if !isIn {
@ -43,25 +44,16 @@ func (c *Conn) startSearch() {
var isIn bool var isIn bool
switch { switch {
case !isIn || !sinfo.init: case !isIn || !sinfo.init:
// No or unintiialized session, so we need to search first
doSearch() doSearch()
case time.Since(sinfo.time) > 6*time.Second: case time.Since(sinfo.time) > 6*time.Second:
if sinfo.time.Before(sinfo.pingTime) && time.Since(sinfo.pingTime) > 6*time.Second { if sinfo.time.Before(sinfo.pingTime) && time.Since(sinfo.pingTime) > 6*time.Second {
// We haven't heard from the dest in a while
// We tried pinging but didn't get a response
// They may have changed coords
// Try searching to discover new coords
// Note that search spam is throttled internally
doSearch() doSearch()
} else { } else {
// We haven't heard about the dest in a while
now := time.Now() now := time.Now()
if !sinfo.time.Before(sinfo.pingTime) { if !sinfo.time.Before(sinfo.pingTime) {
// Update pingTime to start the clock for searches (above)
sinfo.pingTime = now sinfo.pingTime = now
} }
if time.Since(sinfo.pingSend) > time.Second { if time.Since(sinfo.pingSend) > time.Second {
// Send at most 1 ping per second
sinfo.pingSend = now sinfo.pingSend = now
c.core.sessions.sendPingPong(sinfo, false) c.core.sessions.sendPingPong(sinfo, false)
} }
@ -73,6 +65,10 @@ func (c *Conn) Read(b []byte) (int, error) {
if c.session == nil { if c.session == nil {
return 0, errors.New("session not open") return 0, errors.New("session not open")
} }
if !c.session.init {
// To prevent blocking forever on a session that isn't initialised
return 0, errors.New("session not initialised")
}
p := <-c.session.recv p := <-c.session.recv
defer util.PutBytes(p.Payload) defer util.PutBytes(p.Payload)
if !c.session.nonceIsOK(&p.Nonce) { if !c.session.nonceIsOK(&p.Nonce) {