github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/protocol/peer/peer.go (about) 1 package peer 2 3 import ( 4 "strings" 5 "sync" 6 "time" 7 8 "github.com/turingchain2020/turingchain/common/log/log15" 9 "github.com/turingchain2020/turingchain/system/p2p/dht/protocol" 10 "github.com/turingchain2020/turingchain/types" 11 ) 12 13 const ( 14 // Deprecated: old version, use peerInfo instead 15 peerInfoOld = "/turingchain/peerinfoReq/1.0.0" 16 peerInfo = "/turingchain/peer-info/1.0.0" //老版本 17 // Deprecated: old version, use peerVersion instead 18 peerVersionOld = "/turingchain/peerVersion/1.0.0" 19 peerVersion = "/turingchain/peer-version/1.0.0" 20 ) 21 22 const ( 23 blockchain = "blockchain" 24 mempool = "mempool" 25 ) 26 27 var log = log15.New("module", "p2p.peer") 28 29 func init() { 30 protocol.RegisterProtocolInitializer(InitProtocol) 31 } 32 33 // Protocol ... 34 type Protocol struct { 35 *protocol.P2PEnv 36 refreshing int32 37 38 // "/ip4/{ip}/tcp/{port}" 39 externalAddr string 40 mutex sync.Mutex 41 42 topicMutex sync.RWMutex 43 topicModule sync.Map 44 } 45 46 // InitProtocol init protocol 47 func InitProtocol(env *protocol.P2PEnv) { 48 p := Protocol{ 49 P2PEnv: env, 50 } 51 //先进行外部地址预设置 52 for _, multiAddr := range p.Host.Addrs() { 53 addr := multiAddr.String() 54 // 如果找到公网ip直接break,否则预设置一个内网ip 55 if isPublicIP(strings.Split(addr, "/")[2]) { 56 p.mutex.Lock() 57 p.externalAddr = addr 58 p.mutex.Unlock() 59 break 60 } 61 if !strings.Contains(addr, "127.0.0.1") && !strings.Contains(addr, "localhost") { 62 p.mutex.Lock() 63 p.externalAddr = addr 64 p.mutex.Unlock() 65 } 66 } 67 68 // Deprecated: old version, use peerInfo instead 69 protocol.RegisterStreamHandler(p.Host, peerInfoOld, p.handleStreamPeerInfoOld) 70 protocol.RegisterStreamHandler(p.Host, peerInfo, p.handleStreamPeerInfo) 71 // Deprecated: old version, use peerVersion instead 72 protocol.RegisterStreamHandler(p.Host, peerVersionOld, p.handleStreamVersionOld) 73 protocol.RegisterStreamHandler(p.Host, peerVersion, p.handleStreamVersion) 74 protocol.RegisterEventHandler(types.EventPeerInfo, p.handleEventPeerInfo) 75 protocol.RegisterEventHandler(types.EventGetNetInfo, p.handleEventNetInfo) 76 protocol.RegisterEventHandler(types.EventNetProtocols, p.handleEventNetProtocols) 77 78 //绑定订阅事件与相关处理函数 79 protocol.RegisterEventHandler(types.EventSubTopic, p.handleEventSubTopic) 80 //获取订阅topic列表 81 protocol.RegisterEventHandler(types.EventFetchTopics, p.handleEventGetTopics) 82 //移除订阅主题 83 protocol.RegisterEventHandler(types.EventRemoveTopic, p.handleEventRemoveTopic) 84 //发布消息 85 protocol.RegisterEventHandler(types.EventPubTopicMsg, p.handleEventPubMsg) 86 87 go p.detectNodeAddr() 88 go func() { 89 ticker1 := time.NewTicker(time.Second * 10) 90 if p.ChainCfg.IsTestNet() { 91 ticker1 = time.NewTicker(time.Second) 92 } 93 for { 94 select { 95 case <-p.Ctx.Done(): 96 return 97 case <-ticker1.C: 98 p.refreshPeerInfo() 99 } 100 } 101 }() 102 } 103 104 func (p *Protocol) checkDone() bool { 105 select { 106 case <-p.Ctx.Done(): 107 return true 108 default: 109 return false 110 } 111 }