diff --git a/core/common.go b/core/common.go index 56c5c12..8353ab3 100644 --- a/core/common.go +++ b/core/common.go @@ -20,7 +20,6 @@ import ( const MinNodeNameLen = 8 func getmac(ip string) string { - //get mac relative to the ip address which connected to the mq. ifaces, err := net.Interfaces() if err != nil { return "" diff --git a/core/p2papp.go b/core/p2papp.go index c35aed1..7bb309a 100644 --- a/core/p2papp.go +++ b/core/p2papp.go @@ -204,12 +204,15 @@ func (app *p2pApp) listen() error { if app.rtid != 0 { go app.relayHeartbeatLoop() } - for app.tunnel.isRuning() && app.running { + for app.tunnel.isRuning() { if app.config.Protocol == "udp" { app.listenUDP() } else { app.listenTCP() } + if !app.running { + break + } time.Sleep(time.Second * 10) } return nil diff --git a/core/p2pnetwork.go b/core/p2pnetwork.go index 703bb3b..26f064f 100644 --- a/core/p2pnetwork.go +++ b/core/p2pnetwork.go @@ -23,15 +23,15 @@ var ( ) type P2PNetwork struct { - conn *websocket.Conn - online bool - running bool - restartCh chan bool - wg sync.WaitGroup - writeMtx sync.Mutex - serverTs int64 - localTs int64 - hbTime time.Time + conn *websocket.Conn + online bool + running bool + restartCh chan bool + wgReconnect sync.WaitGroup + writeMtx sync.Mutex + serverTs int64 + localTs int64 + hbTime time.Time // msgMap sync.Map msgMap map[uint64]chan []byte //key: nodeID msgMapMtx sync.Mutex @@ -72,7 +72,7 @@ func (pn *P2PNetwork) run() { case <-pn.restartCh: pn.online = false - pn.wg.Wait() // wait read/write goroutine exited + pn.wgReconnect.Wait() // wait read/write goroutine end err := pn.init() if err != nil { gLog.Println(LvERROR, "P2PNetwork init error:", err) @@ -151,7 +151,6 @@ func (pn *P2PNetwork) autorunApp() { continue } pn.runAll() - time.Sleep(time.Second * 10) } gLog.Println(LvINFO, "autorunApp end") } @@ -436,6 +435,11 @@ func (pn *P2PNetwork) newTunnel(t *P2PTunnel, tid uint64, isClient bool) error { } func (pn *P2PNetwork) init() error { gLog.Println(LvINFO, "init start") + go func() { //reconnect at least 5s + pn.wgReconnect.Add(1) + defer pn.wgReconnect.Done() + time.Sleep(NatTestTimeout) + }() var err error for { // detect nat type @@ -568,8 +572,8 @@ func (pn *P2PNetwork) handleMessage(t int, msg []byte) { func (pn *P2PNetwork) readLoop() { gLog.Printf(LvDEBUG, "P2PNetwork readLoop start") - pn.wg.Add(1) - defer pn.wg.Done() + pn.wgReconnect.Add(1) + defer pn.wgReconnect.Done() for pn.running { pn.conn.SetReadDeadline(time.Now().Add(NetworkHeartbeatTime + 10*time.Second)) t, msg, err := pn.conn.ReadMessage() diff --git a/core/protocol.go b/core/protocol.go index 4876967..6eb73c8 100644 --- a/core/protocol.go +++ b/core/protocol.go @@ -10,7 +10,7 @@ import ( "time" ) -const OpenP2PVersion = "3.5.5" +const OpenP2PVersion = "3.5.6" const ProductName string = "openp2p" const LeastSupportVersion = "3.0.0"