5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 06:20:26 +00:00

Further tweaks

This commit is contained in:
Neil Alexander 2019-01-23 17:05:16 +00:00
parent 81545fd9bf
commit 2944be4faf
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
4 changed files with 19 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package yggdrasil
import ( import (
"errors" "errors"
"io"
"sync" "sync"
) )
@ -24,8 +25,13 @@ type awdlReadWriteCloser struct {
} }
func (c awdlReadWriteCloser) Read(p []byte) (n int, err error) { func (c awdlReadWriteCloser) Read(p []byte) (n int, err error) {
p = <-c.fromAWDL select {
return len(p), nil 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) { func (c awdlReadWriteCloser) Write(p []byte) (n int, err error) {
@ -48,10 +54,10 @@ func (l *awdl) init(c *Core) error {
return nil 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{ rwc := awdlReadWriteCloser{
fromAWDL: fromAWDL, fromAWDL: make(chan []byte, 1),
toAWDL: toAWDL, toAWDL: make(chan []byte, 1),
} }
s := stream{} s := stream{}
s.init(rwc) s.init(rwc)
@ -66,7 +72,7 @@ func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name, local, rem
l.mutex.Lock() l.mutex.Lock()
l.interfaces[name] = &intf l.interfaces[name] = &intf
l.mutex.Unlock() l.mutex.Unlock()
go link.handler() go intf.link.handler()
return &intf, nil return &intf, nil
} }

View File

@ -87,12 +87,10 @@ func (intf *linkInterface) handler() error {
if err != nil { if err != nil {
return err return err
} }
intf.link.core.log.Println("Sent my metadata")
metaBytes, err = intf.msgIO._recvMetaBytes() metaBytes, err = intf.msgIO._recvMetaBytes()
if err != nil { if err != nil {
return err return err
} }
intf.link.core.log.Println("Received their metadata")
meta = version_metadata{} meta = version_metadata{}
if !meta.decode(metaBytes) || !meta.check() { if !meta.decode(metaBytes) || !meta.check() {
return errors.New("failed to decode metadata") 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)) 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") 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 // Check if we already have a link to this node
intf.info.box = meta.box intf.info.box = meta.box
intf.info.sig = meta.sig 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.core.log.Println("DEBUG: registered interface for", intf.name)
} }
intf.link.mutex.Unlock() intf.link.mutex.Unlock()
intf.link.core.log.Println("Create peer")
// Create peer // Create peer
shared := crypto.GetSharedKey(myLinkPriv, &meta.link) shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name) 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() } intf.peer.close = func() { intf.msgIO.close() }
go intf.peer.linkLoop() go intf.peer.linkLoop()
// Start the writer // Start the writer
intf.link.core.log.Println("Start writer")
go func() { go func() {
interval := 4 * time.Second interval := 4 * time.Second
timer := time.NewTimer(interval) 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 intf.link.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle
// Run reader loop // Run reader loop
intf.link.core.log.Println("Start reader")
for { for {
msg, err := intf.msgIO.readMsg() msg, err := intf.msgIO.readMsg()
if len(msg) > 0 { if len(msg) > 0 {

View File

@ -30,9 +30,7 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) {
} }
func (c *Core) AWDLCreateInterface(name, local, remote string) error { func (c *Core) AWDLCreateInterface(name, local, remote string) error {
fromAWDL := make(chan []byte, 32) if intf, err := c.awdl.create(name, local, remote); err != nil || intf == nil {
toAWDL := make(chan []byte, 32)
if intf, err := c.awdl.create(fromAWDL, toAWDL, name, local, remote); err != nil || intf == nil {
c.log.Println("c.awdl.create:", err) c.log.Println("c.awdl.create:", err)
return err return err
} }
@ -45,7 +43,11 @@ func (c *Core) AWDLShutdownInterface(name string) error {
func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) { func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
if intf := c.awdl.getInterface(identity); intf != nil { 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) return nil, errors.New("AWDLRecvPacket identity not known: " + identity)
} }

View File

@ -250,6 +250,7 @@ func (p *peer) handleTraffic(packet []byte, pTypeLen int) {
func (p *peer) sendPacket(packet []byte) { func (p *peer) sendPacket(packet []byte) {
// Is there ever a case where something more complicated is needed? // Is there ever a case where something more complicated is needed?
// What if p.out blocks? // What if p.out blocks?
atomic.AddUint64(&p.bytesSent, uint64(len(packet)))
p.out(packet) p.out(packet)
} }