mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-06-16 22:25:08 +03:00
Refactor ipv6rwc to interface instead of double struct
This commit is contained in:
parent
486ffebedd
commit
e6fff2f0db
2 changed files with 41 additions and 34 deletions
|
@ -298,6 +298,38 @@ func (k *keyStore) writePC(bs []byte) (int, error) {
|
||||||
|
|
||||||
// Exported API
|
// Exported API
|
||||||
|
|
||||||
|
type ReadWriteCloser interface {
|
||||||
|
Address() address.Address
|
||||||
|
Subnet() address.Subnet
|
||||||
|
MTU() uint64
|
||||||
|
MaxMTU() uint64
|
||||||
|
SetMTU(uint64)
|
||||||
|
Read([]byte) (int, error)
|
||||||
|
Write([]byte) (int, error)
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReadWriteCloser(c *core.Core) ReadWriteCloser {
|
||||||
|
k := &keyStore{}
|
||||||
|
k.init(c)
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *keyStore) Address() address.Address {
|
||||||
|
return k.address
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *keyStore) Subnet() address.Subnet {
|
||||||
|
return k.subnet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *keyStore) MTU() uint64 {
|
||||||
|
k.mutex.Lock()
|
||||||
|
mtu := k.mtu
|
||||||
|
k.mutex.Unlock()
|
||||||
|
return mtu
|
||||||
|
}
|
||||||
|
|
||||||
func (k *keyStore) MaxMTU() uint64 {
|
func (k *keyStore) MaxMTU() uint64 {
|
||||||
return k.core.MTU()
|
return k.core.MTU()
|
||||||
}
|
}
|
||||||
|
@ -314,41 +346,16 @@ func (k *keyStore) SetMTU(mtu uint64) {
|
||||||
k.mutex.Unlock()
|
k.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *keyStore) MTU() uint64 {
|
func (k *keyStore) Read(p []byte) (n int, err error) {
|
||||||
k.mutex.Lock()
|
return k.readPC(p)
|
||||||
mtu := k.mtu
|
|
||||||
k.mutex.Unlock()
|
|
||||||
return mtu
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReadWriteCloser struct {
|
func (k *keyStore) Write(p []byte) (n int, err error) {
|
||||||
keyStore
|
return k.writePC(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReadWriteCloser(c *core.Core) *ReadWriteCloser {
|
func (k *keyStore) Close() error {
|
||||||
rwc := new(ReadWriteCloser)
|
err := k.core.Close()
|
||||||
rwc.init(c)
|
k.core.Stop()
|
||||||
return rwc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rwc *ReadWriteCloser) Address() address.Address {
|
|
||||||
return rwc.address
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rwc *ReadWriteCloser) Subnet() address.Subnet {
|
|
||||||
return rwc.subnet
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rwc *ReadWriteCloser) Read(p []byte) (n int, err error) {
|
|
||||||
return rwc.readPC(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rwc *ReadWriteCloser) Write(p []byte) (n int, err error) {
|
|
||||||
return rwc.writePC(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rwc *ReadWriteCloser) Close() error {
|
|
||||||
err := rwc.core.Close()
|
|
||||||
rwc.core.Stop()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ type MTU uint16
|
||||||
// should pass this object to the yggdrasil.SetRouterAdapter() function before
|
// should pass this object to the yggdrasil.SetRouterAdapter() function before
|
||||||
// calling yggdrasil.Start().
|
// calling yggdrasil.Start().
|
||||||
type TunAdapter struct {
|
type TunAdapter struct {
|
||||||
rwc *ipv6rwc.ReadWriteCloser
|
rwc ipv6rwc.ReadWriteCloser
|
||||||
config *config.NodeConfig
|
config *config.NodeConfig
|
||||||
log *log.Logger
|
log *log.Logger
|
||||||
addr address.Address
|
addr address.Address
|
||||||
|
@ -93,7 +93,7 @@ func MaximumMTU() uint64 {
|
||||||
|
|
||||||
// Init initialises the TUN module. You must have acquired a Listener from
|
// Init initialises the TUN module. You must have acquired a Listener from
|
||||||
// the Yggdrasil core before this point and it must not be in use elsewhere.
|
// the Yggdrasil core before this point and it must not be in use elsewhere.
|
||||||
func (tun *TunAdapter) Init(rwc *ipv6rwc.ReadWriteCloser, config *config.NodeConfig, log *log.Logger, options interface{}) error {
|
func (tun *TunAdapter) Init(rwc ipv6rwc.ReadWriteCloser, config *config.NodeConfig, log *log.Logger, options interface{}) error {
|
||||||
tun.rwc = rwc
|
tun.rwc = rwc
|
||||||
tun.config = config
|
tun.config = config
|
||||||
tun.log = log
|
tun.log = log
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue