mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-04 23:40:26 +08:00
adjust keep alive pos
This commit is contained in:
@@ -31,8 +31,6 @@ type Link struct {
|
||||
endpoint *net.UDPAddr
|
||||
// 本机允许接收/发送的 ip 网段
|
||||
allowedips []*net.IPNet
|
||||
// 是否已经调用过 keepAlive
|
||||
haskeepruning bool
|
||||
// 是否允许转发
|
||||
allowtrans bool
|
||||
// 连接的状态,详见下方 const
|
||||
@@ -53,7 +51,6 @@ const (
|
||||
func (m *Me) Connect(peer string) (*Link, error) {
|
||||
p, ok := m.IsInPeer(net.ParseIP(peer).String())
|
||||
if ok {
|
||||
p.keepAlive()
|
||||
return p, nil
|
||||
}
|
||||
return nil, errors.New("peer not exist")
|
||||
|
||||
@@ -10,19 +10,16 @@ import (
|
||||
|
||||
// 保持 NAT
|
||||
func (l *Link) keepAlive() {
|
||||
if l.keepalive > 0 && !l.haskeepruning {
|
||||
l.haskeepruning = true
|
||||
go func() {
|
||||
t := time.NewTicker(time.Second * time.Duration(l.keepalive))
|
||||
for range t.C {
|
||||
n, err := l.Write(head.NewPacket(head.ProtoHello, 0, l.peerip, 0, nil), false)
|
||||
if err == nil {
|
||||
logrus.Infoln("[link] send", n, "bytes keep alive packet")
|
||||
} else {
|
||||
logrus.Errorln("[link] send keep alive packet error:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
if l.keepalive > 0 {
|
||||
logrus.Infoln("[link.nat] start to keep alive")
|
||||
t := time.NewTicker(time.Second * time.Duration(l.keepalive))
|
||||
for range t.C {
|
||||
n, err := l.Write(head.NewPacket(head.ProtoHello, 0, l.peerip, 0, nil), false)
|
||||
if err == nil {
|
||||
logrus.Infoln("[link] send", n, "bytes keep alive packet")
|
||||
} else {
|
||||
logrus.Errorln("[link] send keep alive packet error:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed
|
||||
l.me.connections[peerip] = l
|
||||
l.me.connmapmu.Unlock()
|
||||
logrus.Infoln("[peer] add peer:", peerip, "allow:", allowedIPs)
|
||||
go l.keepAlive()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user