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:
parent
188a9e439d
commit
705b914d00
@ -7,13 +7,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type awdl struct {
|
type awdl struct {
|
||||||
core *Core
|
link *link
|
||||||
mutex sync.RWMutex // protects interfaces below
|
mutex sync.RWMutex // protects interfaces below
|
||||||
interfaces map[string]*awdlInterface
|
interfaces map[string]*awdlInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
type awdlInterface struct {
|
type awdlInterface struct {
|
||||||
link *linkInterface
|
linkif *linkInterface
|
||||||
rwc awdlReadWriteCloser
|
rwc awdlReadWriteCloser
|
||||||
peer *peer
|
peer *peer
|
||||||
stream stream
|
stream stream
|
||||||
@ -45,53 +45,53 @@ func (c awdlReadWriteCloser) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *awdl) init(c *Core) error {
|
func (a *awdl) init(l *link) error {
|
||||||
l.core = c
|
a.link = l
|
||||||
l.mutex.Lock()
|
a.mutex.Lock()
|
||||||
l.interfaces = make(map[string]*awdlInterface)
|
a.interfaces = make(map[string]*awdlInterface)
|
||||||
l.mutex.Unlock()
|
a.mutex.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *awdl) create(name, local, remote string) (*awdlInterface, error) {
|
func (a *awdl) create(name, local, remote string) (*awdlInterface, error) {
|
||||||
rwc := awdlReadWriteCloser{
|
rwc := awdlReadWriteCloser{
|
||||||
fromAWDL: make(chan []byte, 1),
|
fromAWDL: make(chan []byte, 1),
|
||||||
toAWDL: make(chan []byte, 1),
|
toAWDL: make(chan []byte, 1),
|
||||||
}
|
}
|
||||||
s := stream{}
|
s := stream{}
|
||||||
s.init(rwc)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
intf := awdlInterface{
|
intf := awdlInterface{
|
||||||
link: link,
|
linkif: linkif,
|
||||||
rwc: rwc,
|
rwc: rwc,
|
||||||
}
|
}
|
||||||
l.mutex.Lock()
|
a.mutex.Lock()
|
||||||
l.interfaces[name] = &intf
|
a.interfaces[name] = &intf
|
||||||
l.mutex.Unlock()
|
a.mutex.Unlock()
|
||||||
go intf.link.handler()
|
go intf.linkif.handler()
|
||||||
return &intf, nil
|
return &intf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *awdl) getInterface(identity string) *awdlInterface {
|
func (a *awdl) getInterface(identity string) *awdlInterface {
|
||||||
l.mutex.RLock()
|
a.mutex.RLock()
|
||||||
defer l.mutex.RUnlock()
|
defer a.mutex.RUnlock()
|
||||||
if intf, ok := l.interfaces[identity]; ok {
|
if intf, ok := a.interfaces[identity]; ok {
|
||||||
return intf
|
return intf
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *awdl) shutdown(identity string) error {
|
func (a *awdl) shutdown(identity string) error {
|
||||||
if intf, ok := l.interfaces[identity]; ok {
|
if intf, ok := a.interfaces[identity]; ok {
|
||||||
close(intf.link.closed)
|
close(intf.linkif.closed)
|
||||||
intf.rwc.Close()
|
intf.rwc.Close()
|
||||||
l.mutex.Lock()
|
a.mutex.Lock()
|
||||||
delete(l.interfaces, identity)
|
delete(a.interfaces, identity)
|
||||||
l.mutex.Unlock()
|
a.mutex.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("Interface not found or already closed")
|
return errors.New("Interface not found or already closed")
|
||||||
|
@ -44,8 +44,7 @@ type Core struct {
|
|||||||
searches searches
|
searches searches
|
||||||
multicast multicast
|
multicast multicast
|
||||||
tcp tcpInterface
|
tcp tcpInterface
|
||||||
link link // TODO: not sure if this wants to be here?
|
link link
|
||||||
awdl awdl
|
|
||||||
log *log.Logger
|
log *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ type link struct {
|
|||||||
core *Core
|
core *Core
|
||||||
mutex sync.RWMutex // protects interfaces below
|
mutex sync.RWMutex // protects interfaces below
|
||||||
interfaces map[linkInfo]*linkInterface
|
interfaces map[linkInfo]*linkInterface
|
||||||
|
awdl awdl // AWDL interface support
|
||||||
}
|
}
|
||||||
|
|
||||||
type linkInfo struct {
|
type linkInfo struct {
|
||||||
@ -49,7 +50,7 @@ func (l *link) init(c *Core) error {
|
|||||||
l.interfaces = make(map[linkInfo]*linkInterface)
|
l.interfaces = make(map[linkInfo]*linkInterface)
|
||||||
l.mutex.Unlock()
|
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")
|
l.core.log.Println("Failed to start AWDL interface")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -30,19 +30,19 @@ 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 {
|
||||||
if intf, err := c.awdl.create(name, local, remote); err != nil || intf == nil {
|
if intf, err := c.link.awdl.create(name, local, remote); err != nil || intf == nil {
|
||||||
c.log.Println("c.awdl.create:", err)
|
c.log.Println("c.link.awdl.create:", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Core) AWDLShutdownInterface(name string) error {
|
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) {
|
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
|
read, ok := <-intf.rwc.toAWDL
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("AWDLRecvPacket: channel closed")
|
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 {
|
func (c *Core) AWDLSendPacket(identity string, buf []byte) error {
|
||||||
packet := append(util.GetBytes(), buf[:]...)
|
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
|
intf.rwc.fromAWDL <- packet
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user