mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-22 11:30:31 +08:00
init
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user