MCPcopy Create free account
hub / github.com/DNAProject/DNA / Connect

Method Connect

p2pserver/net/netserver/netserver.go:252–325  ·  view source on GitHub ↗

Connect used to connect net address under sync or cons mode

(addr string)

Source from the content-addressed store, hash-verified

250
251//Connect used to connect net address under sync or cons mode
252func (this *NetServer) Connect(addr string) error {
253 if this.IsAddrInOutConnRecord(addr) {
254 log.Debugf("[p2p]Address: %s is in OutConnectionRecord,", addr)
255 return nil
256 }
257 if this.IsOwnAddress(addr) {
258 return nil
259 }
260 if !this.AddrValid(addr) {
261 return nil
262 }
263
264 this.connectLock.Lock()
265 connCount := uint(this.GetOutConnRecordLen())
266 if connCount >= config.DefConfig.P2PNode.MaxConnOutBound {
267 log.Warnf("[p2p]Connect: out connections(%d) reach the max limit(%d)", connCount,
268 config.DefConfig.P2PNode.MaxConnOutBound)
269 this.connectLock.Unlock()
270 return errors.New("[p2p]connect: out connections reach the max limit")
271 }
272 this.connectLock.Unlock()
273
274 if this.IsNbrPeerAddr(addr) {
275 return nil
276 }
277 this.connectLock.Lock()
278 if addOK := this.AddOutConnectingList(addr); !addOK {
279 log.Debug("[p2p]node exist in connecting list", addr)
280 }
281 this.connectLock.Unlock()
282
283 isTls := config.DefConfig.P2PNode.IsTLS
284 var conn net.Conn
285 var err error
286 var remotePeer *peer.Peer
287 if isTls {
288 conn, err = TLSDial(addr)
289 if err != nil {
290 this.RemoveFromConnectingList(addr)
291 log.Debugf("[p2p]connect %s failed:%s", addr, err.Error())
292 return err
293 }
294 } else {
295 conn, err = nonTLSDial(addr)
296 if err != nil {
297 this.RemoveFromConnectingList(addr)
298 log.Debugf("[p2p]connect %s failed:%s", addr, err.Error())
299 return err
300 }
301 }
302
303 addr = conn.RemoteAddr().String()
304 log.Debugf("[p2p]peer %s connect with %s with %s",
305 conn.LocalAddr().String(), conn.RemoteAddr().String(),
306 conn.RemoteAddr().Network())
307
308 this.AddOutConnRecord(addr)
309 remotePeer = peer.NewPeer()

Callers

nothing calls this directly

Calls 15

IsAddrInOutConnRecordMethod · 0.95
IsOwnAddressMethod · 0.95
AddrValidMethod · 0.95
GetOutConnRecordLenMethod · 0.95
IsNbrPeerAddrMethod · 0.95
AddOutConnectingListMethod · 0.95
AddOutConnRecordMethod · 0.95
AddPeerAddressMethod · 0.95
AttachChanMethod · 0.95
SetStateMethod · 0.95
SendMethod · 0.95

Tested by

no test coverage detected