From 2944be4faff0419cc4e4c7d4dbe68672d56473df Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 23 Jan 2019 17:05:16 +0000 Subject: [PATCH] Further tweaks --- src/yggdrasil/awdl.go | 18 ++++++++++++------ src/yggdrasil/link.go | 6 ------ src/yggdrasil/mobile_ios.go | 10 ++++++---- src/yggdrasil/peer.go | 1 + 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/yggdrasil/awdl.go b/src/yggdrasil/awdl.go index ba3131ff..3e26a251 100644 --- a/src/yggdrasil/awdl.go +++ b/src/yggdrasil/awdl.go @@ -2,6 +2,7 @@ package yggdrasil import ( "errors" + "io" "sync" ) @@ -24,8 +25,13 @@ type awdlReadWriteCloser struct { } func (c awdlReadWriteCloser) Read(p []byte) (n int, err error) { - p = <-c.fromAWDL - return len(p), nil + select { + case packet := <-c.fromAWDL: + n = copy(p, packet) + return n, nil + default: + return 0, io.EOF + } } func (c awdlReadWriteCloser) Write(p []byte) (n int, err error) { @@ -48,10 +54,10 @@ func (l *awdl) init(c *Core) error { return nil } -func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name, local, remote string) (*awdlInterface, error) { +func (l *awdl) create(name, local, remote string) (*awdlInterface, error) { rwc := awdlReadWriteCloser{ - fromAWDL: fromAWDL, - toAWDL: toAWDL, + fromAWDL: make(chan []byte, 1), + toAWDL: make(chan []byte, 1), } s := stream{} s.init(rwc) @@ -66,7 +72,7 @@ func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name, local, rem l.mutex.Lock() l.interfaces[name] = &intf l.mutex.Unlock() - go link.handler() + go intf.link.handler() return &intf, nil } diff --git a/src/yggdrasil/link.go b/src/yggdrasil/link.go index e763fb1e..8b833f53 100644 --- a/src/yggdrasil/link.go +++ b/src/yggdrasil/link.go @@ -87,12 +87,10 @@ func (intf *linkInterface) handler() error { if err != nil { return err } - intf.link.core.log.Println("Sent my metadata") metaBytes, err = intf.msgIO._recvMetaBytes() if err != nil { return err } - intf.link.core.log.Println("Received their metadata") meta = version_metadata{} if !meta.decode(metaBytes) || !meta.check() { return errors.New("failed to decode metadata") @@ -102,7 +100,6 @@ func (intf *linkInterface) handler() error { intf.link.core.log.Println("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer)) return errors.New("failed to connect: wrong version") } - intf.link.core.log.Println("Do we have a link already?") // Check if we already have a link to this node intf.info.box = meta.box intf.info.sig = meta.sig @@ -127,7 +124,6 @@ func (intf *linkInterface) handler() error { intf.link.core.log.Println("DEBUG: registered interface for", intf.name) } intf.link.mutex.Unlock() - intf.link.core.log.Println("Create peer") // Create peer shared := crypto.GetSharedKey(myLinkPriv, &meta.link) intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name) @@ -149,7 +145,6 @@ func (intf *linkInterface) handler() error { intf.peer.close = func() { intf.msgIO.close() } go intf.peer.linkLoop() // Start the writer - intf.link.core.log.Println("Start writer") go func() { interval := 4 * time.Second timer := time.NewTimer(interval) @@ -195,7 +190,6 @@ func (intf *linkInterface) handler() error { }() intf.link.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle // Run reader loop - intf.link.core.log.Println("Start reader") for { msg, err := intf.msgIO.readMsg() if len(msg) > 0 { diff --git a/src/yggdrasil/mobile_ios.go b/src/yggdrasil/mobile_ios.go index e864c889..5e0d4aa4 100644 --- a/src/yggdrasil/mobile_ios.go +++ b/src/yggdrasil/mobile_ios.go @@ -30,9 +30,7 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) { } func (c *Core) AWDLCreateInterface(name, local, remote string) error { - fromAWDL := make(chan []byte, 32) - toAWDL := make(chan []byte, 32) - if intf, err := c.awdl.create(fromAWDL, toAWDL, name, local, remote); err != nil || intf == nil { + if intf, err := c.awdl.create(name, local, remote); err != nil || intf == nil { c.log.Println("c.awdl.create:", err) return err } @@ -45,7 +43,11 @@ func (c *Core) AWDLShutdownInterface(name string) error { func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) { if intf := c.awdl.getInterface(identity); intf != nil { - return <-intf.rwc.toAWDL, nil + read, ok := <-intf.rwc.toAWDL + if !ok { + return nil, errors.New("AWDLRecvPacket: channel closed") + } + return read, nil } return nil, errors.New("AWDLRecvPacket identity not known: " + identity) } diff --git a/src/yggdrasil/peer.go b/src/yggdrasil/peer.go index 2cd1afe8..ad54fbc3 100644 --- a/src/yggdrasil/peer.go +++ b/src/yggdrasil/peer.go @@ -250,6 +250,7 @@ func (p *peer) handleTraffic(packet []byte, pTypeLen int) { func (p *peer) sendPacket(packet []byte) { // Is there ever a case where something more complicated is needed? // What if p.out blocks? + atomic.AddUint64(&p.bytesSent, uint64(len(packet))) p.out(packet) }