5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 13:00:47 +00:00

Move awdl into link

This commit is contained in:
Neil Alexander 2019-01-23 19:42:33 +00:00
parent 188a9e439d
commit 705b914d00
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
4 changed files with 33 additions and 33 deletions

View File

@ -7,13 +7,13 @@ import (
)
type awdl struct {
core *Core
link *link
mutex sync.RWMutex // protects interfaces below
interfaces map[string]*awdlInterface
}
type awdlInterface struct {
link *linkInterface
linkif *linkInterface
rwc awdlReadWriteCloser
peer *peer
stream stream
@ -45,53 +45,53 @@ func (c awdlReadWriteCloser) Close() error {
return nil
}
func (l *awdl) init(c *Core) error {
l.core = c
l.mutex.Lock()
l.interfaces = make(map[string]*awdlInterface)
l.mutex.Unlock()
func (a *awdl) init(l *link) error {
a.link = l
a.mutex.Lock()
a.interfaces = make(map[string]*awdlInterface)
a.mutex.Unlock()
return nil
}
func (l *awdl) create(name, local, remote string) (*awdlInterface, error) {
func (a *awdl) create(name, local, remote string) (*awdlInterface, error) {
rwc := awdlReadWriteCloser{
fromAWDL: make(chan []byte, 1),
toAWDL: make(chan []byte, 1),
}
s := stream{}
s.init(rwc)
link, err := l.core.link.create(&s, name, "awdl", local, remote)
linkif, err := a.link.create(&s, name, "awdl", local, remote)
if err != nil {
return nil, err
}
intf := awdlInterface{
link: link,
rwc: rwc,
linkif: linkif,
rwc: rwc,
}
l.mutex.Lock()
l.interfaces[name] = &intf
l.mutex.Unlock()
go intf.link.handler()
a.mutex.Lock()
a.interfaces[name] = &intf
a.mutex.Unlock()
go intf.linkif.handler()
return &intf, nil
}
func (l *awdl) getInterface(identity string) *awdlInterface {
l.mutex.RLock()
defer l.mutex.RUnlock()
if intf, ok := l.interfaces[identity]; ok {
func (a *awdl) getInterface(identity string) *awdlInterface {
a.mutex.RLock()
defer a.mutex.RUnlock()
if intf, ok := a.interfaces[identity]; ok {
return intf
}
return nil
}
func (l *awdl) shutdown(identity string) error {
if intf, ok := l.interfaces[identity]; ok {
close(intf.link.closed)
func (a *awdl) shutdown(identity string) error {
if intf, ok := a.interfaces[identity]; ok {
close(intf.linkif.closed)
intf.rwc.Close()
l.mutex.Lock()
delete(l.interfaces, identity)
l.mutex.Unlock()
a.mutex.Lock()
delete(a.interfaces, identity)
a.mutex.Unlock()
return nil
}
return errors.New("Interface not found or already closed")

View File

@ -44,8 +44,7 @@ type Core struct {
searches searches
multicast multicast
tcp tcpInterface
link link // TODO: not sure if this wants to be here?
awdl awdl
link link
log *log.Logger
}

View File

@ -15,6 +15,7 @@ type link struct {
core *Core
mutex sync.RWMutex // protects interfaces below
interfaces map[linkInfo]*linkInterface
awdl awdl // AWDL interface support
}
type linkInfo struct {
@ -49,7 +50,7 @@ func (l *link) init(c *Core) error {
l.interfaces = make(map[linkInfo]*linkInterface)
l.mutex.Unlock()
if err := l.core.awdl.init(c); err != nil {
if err := l.awdl.init(l); err != nil {
l.core.log.Println("Failed to start AWDL interface")
return err
}

View File

@ -30,19 +30,19 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) {
}
func (c *Core) AWDLCreateInterface(name, local, remote string) error {
if intf, err := c.awdl.create(name, local, remote); err != nil || intf == nil {
c.log.Println("c.awdl.create:", err)
if intf, err := c.link.awdl.create(name, local, remote); err != nil || intf == nil {
c.log.Println("c.link.awdl.create:", err)
return err
}
return nil
}
func (c *Core) AWDLShutdownInterface(name string) error {
return c.awdl.shutdown(name)
return c.link.awdl.shutdown(name)
}
func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
if intf := c.awdl.getInterface(identity); intf != nil {
if intf := c.link.awdl.getInterface(identity); intf != nil {
read, ok := <-intf.rwc.toAWDL
if !ok {
return nil, errors.New("AWDLRecvPacket: channel closed")
@ -54,7 +54,7 @@ func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
func (c *Core) AWDLSendPacket(identity string, buf []byte) error {
packet := append(util.GetBytes(), buf[:]...)
if intf := c.awdl.getInterface(identity); intf != nil {
if intf := c.link.awdl.getInterface(identity); intf != nil {
intf.rwc.fromAWDL <- packet
return nil
}