1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-22 11:30:31 +08:00
This commit is contained in:
fumiama
2021-10-24 20:56:16 +08:00
parent fa1608165b
commit d9138df3cd
3 changed files with 53 additions and 51 deletions

View File

@@ -2,16 +2,10 @@ package link
import (
"net"
"sync"
"github.com/fumiama/WireGold/gold/head"
)
var (
eps = make(map[string]*Link)
epmu sync.RWMutex
)
func AddPeer(peerip string, pubicKey [32]byte, endPoint string, keepAlive int64) (l *Link) {
peerip = net.ParseIP(peerip).String()
var ok bool
@@ -19,24 +13,23 @@ func AddPeer(peerip string, pubicKey [32]byte, endPoint string, keepAlive int64)
if ok {
return
}
e, err := net.ResolveUDPAddr("udp", endPoint)
if err != nil {
panic(err)
}
l = &Link{
PubicKey: pubicKey,
EndPoint: endPoint,
KeepAlive: keepAlive,
pipe: make(chan *head.Packet, 32),
peerip: net.ParseIP(peerip),
endpoint: e,
}
if endPoint != "" {
e, err := net.ResolveUDPAddr("udp", endPoint)
if err != nil {
panic(err)
}
l.EndPoint = endPoint
l.endpoint = e
}
connmapmu.Lock()
epmu.Lock()
connections[peerip] = l
eps[endPoint] = l
connmapmu.Unlock()
epmu.Unlock()
return
}
@@ -46,10 +39,3 @@ func IsInPeer(peer string) (p *Link, ok bool) {
connmapmu.RUnlock()
return
}
func IsEndpointInPeer(ep string) (p *Link, ok bool) {
epmu.RLock()
p, ok = eps[ep]
epmu.RUnlock()
return
}