func (t *udp_srt) pending(id common.NodeID, ptype byte, callback func(interface{}) bool) <-chan error {
	ch := make(chan error, 1)
	p := &pending{from: id, ptype: ptype, callback: callback, errc: ch}
	select {
	case t.addpending <- p:
	case <-t.closing:
		ch <- errClosed
	}
	return ch
}
pending adds a reply callback to the pending reply queue.
see the documentation of type pending for a detailed explanation.
package discover
import (
	"bytes"
	"container/list"
	"errors"
	"fmt"
	"net"
	"sort"
	"time"
	"github.com/ThinkiumGroup/go-common"
	"github.com/ThinkiumGroup/go-common/log"
	"github.com/ThinkiumGroup/go-thinkium/config"
	"github.com/ThinkiumGroup/go-thinkium/network/nat"
	"github.com/stephenfire/go-rtl"
)
func init() {
	p := neighborsSort{Version: srtVersion, ChainID: common.NilChainID, NetType: common.BranchDataNet, Expiration: ^uint64(0)}
	maxSizeNode := rpcNode{IP: make(net.IP, 16), UDP: ^uint16(0), TCP: ^uint16(0), RPC: ^uint16(0), ID: nodeDBNilNodeID}
	for n := 0; ; n++ {
		p.Nodes = append(p.Nodes, maxSizeNode)
		bs, err := rtl.Marshal(p)
		if err != nil {
			panic("cannot encode: " + err.Error())
		}
		if headSize+len(bs)+1 >= 1280 {
			maxNeighbors = n
			break
		}
	}
}
const (
	srtVersion = 1
	visitNeighourChainCount = 2
	neighbourChainCount = visitNeighourChainCount * 2
	friendChainDistance = neighbourChainCount + 1
	SortTableSize = 64
)
func GetVisitChainIds(boots []*ChainDataNodes, centre common.ChainID) common.ChainIDs {
	if len(boots) == 0 {
		return nil
	}
	selfIdx := getChainIndex(boots, centre)
	if selfIdx == -1 {
		return nil
	}
	chainCount := len(boots)
	var chainIds common.ChainIDs
	if chainCount < friendChainDistance {
		for i := 0; i < chainCount; i++ {
			chainIds = append(chainIds, boots[i].ChainId)
		}
		return chainIds
	}
	visitChainCount := (chainCount/friendChainDistance + neighbourChainCount) / 2
	chainIds = append(chainIds, centre)
	for i := 0; i < visitChainCount; i++ {
		if i < visitNeighourChainCount {
			idx := selfIdx + i + 1
			if idx >= chainCount {
				idx = idx - chainCount
			}
			chainIds = append(chainIds, boots[idx].ChainId)
			continue
		}
		idx := selfIdx + visitNeighourChainCount + (i-visitNeighourChainCount+1)*friendChainDistance
		if idx >= chainCount {
			idx = idx % chainCount
		}
		chainIds = append(chainIds, boots[idx].ChainId)
	}
	sort.SliceStable(chainIds, func(i, j int) bool {
		return chainIds[i] < chainIds[j]
	})
	return chainIds
}
func GetTargetChain(boots []*ChainDataNodes, selfChain common.ChainID, targetChain common.ChainID) common.ChainID {
	connectedIndexes := getConnectedIndexes(boots, selfChain)
	if len(connectedIndexes) == 0 {
		return common.NilChainID
	}
	targetIdx := getChainIndex(boots, targetChain)
	if targetIdx == -1 {
		return common.NilChainID
	}
	if targetIdx < connectedIndexes[0] {
		if connectedIndexes[0]-targetIdx <= len(boots)+targetIdx-connectedIndexes[len(connectedIndexes)-1] {
			return boots[connectedIndexes[0]].ChainId
		}
		return boots[connectedIndexes[len(connectedIndexes)-1]].ChainId
	}
	if targetIdx >= connectedIndexes[len(connectedIndexes)-1] {
		if len(boots)+connectedIndexes[0]-targetIdx <= targetIdx-connectedIndexes[len(connectedIndexes)-1] {
			return boots[connectedIndexes[0]].ChainId
		}
		return boots[connectedIndexes[len(connectedIndexes)-1]].ChainId
	}
	idx := sort.Search(len(connectedIndexes), func(i int) bool {
		return connectedIndexes[i] > targetIdx
	})
	if connectedIndexes[idx]-targetIdx < targetIdx-connectedIndexes[idx-1] {
		return boots[connectedIndexes[idx]].ChainId
	}
	return boots[connectedIndexes[idx-1]].ChainId
}
func GetConnectedChains(boots []*ChainDataNodes, centre common.ChainID) common.ChainIDs {
	idxes := getConnectedIndexes(boots, centre)
	var ret common.ChainIDs
	for _, idx := range idxes {
		ret = append(ret, boots[idx].ChainId)
	}
	return ret
}
func getConnectedIndexes(boots []*ChainDataNodes, centre common.ChainID) []int {
	selfIdx := getChainIndex(boots, centre)
	if selfIdx == -1 {
		return nil
	}
	chainCount := len(boots)
	visitChainCount := (chainCount/friendChainDistance + neighbourChainCount) / 2
	var indexes []int
	if chainCount < friendChainDistance {
		for i := 0; i < chainCount; i++ {
			indexes = append(indexes, i)
		}
		return indexes
	}
	indexes = append(indexes, selfIdx)
	for i := 0; i < visitChainCount; i++ {
		if i < visitNeighourChainCount {
			vidx := selfIdx + i + 1
			if vidx >= chainCount {
				vidx = vidx - chainCount
			}
			indexes = append(indexes, vidx)
			cidx := selfIdx - i - 1
			if cidx < 0 {
				cidx = chainCount + cidx
			}
			indexes = append(indexes, cidx)
			continue
		}
		vidx := selfIdx + visitNeighourChainCount + (i-visitNeighourChainCount+1)*friendChainDistance
		if vidx >= chainCount {
			vidx = vidx - chainCount
		}
		indexes = append(indexes, vidx)
		cidx := selfIdx - visitNeighourChainCount - (i-visitNeighourChainCount+1)*friendChainDistance
		if cidx < 0 {
			cidx = cidx + chainCount
		}
		indexes = append(indexes, cidx)
	}
	sort.SliceStable(indexes, func(i, j int) bool {
		return indexes[i] < indexes[j]
	})
	return indexes
}
func getChainIndex(boots []*ChainDataNodes, cid common.ChainID) int {
	for i, b := range boots {
		if b.ChainId == cid {
			return i
		}
	}
	return -1
}
func IsIn(cids common.ChainIDs, cid common.ChainID) bool {
	for _, id := range cids {
		if id == cid {
			return true
		}
	}
	return false
}
type udp_srt struct {
	conn        *net.UDPConn
	netrestrict *Netlist
	ourEndpoint rpcEndpoint
	addpending chan *pending
	gotreply   chan reply
	closing chan struct{}
	nat     nat.Nat
	*STable
}
func SetupDiscoverySRT(self *Node, c *net.UDPConn, cfg UDPConfig) (Discovery, error) {
	tab, udp, err := setupSORTUDP(self, c, cfg)
	if err != nil {
		return nil, err
	}
	log.Infof("P2P SORT UDP setup %s", tab.self)
	return udp, nil
}
func setupSORTUDP(self *Node, c *net.UDPConn, cfg UDPConfig) (*STable, *udp_srt, error) {
	udp := &udp_srt{
		conn:       c,
		closing:    make(chan struct{}),
		gotreply:   make(chan reply),
		addpending: make(chan *pending),
	}
	realaddr := c.LocalAddr().(*net.UDPAddr)
	if cfg.AnnounceAddr != nil {
		realaddr = cfg.AnnounceAddr
	}
	udp.ourEndpoint = makeEndpoint(realaddr, self.TCP)
	tab, err := newSTable(udp, self, cfg)
	if err != nil {
		return nil, nil, err
	}
	udp.STable = tab
	go tab.loop()
	go udp.loop()
	go udp.readLoop(nil)
	return udp.STable, udp, nil
}
func (t *udp_srt) loop() {
	var (
		plist        = list.New()
		timeout      = time.NewTimer(0)
		nextTimeout  *pending  
		contTimeouts = 0       
		ntpWarnTime  = time.Unix(0, 0)
	)
	<-timeout.C  
	defer timeout.Stop()
	resetTimeout := func() {
		if plist.Front() == nil || nextTimeout == plist.Front().Value {
			return
		}
		now := time.Now()
		for el := plist.Front(); el != nil; el = el.Next() {
			nextTimeout = el.Value.(*pending)
			if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout {
				timeout.Reset(dist)
				return
			}
			nextTimeout.errc <- errClockWarp
			plist.Remove(el)
		}
		nextTimeout = nil
		timeout.Stop()
	}
	for {
		resetTimeout()
		select {
		case <-t.closing:
			for el := plist.Front(); el != nil; el = el.Next() {
				el.Value.(*pending).errc <- errClosed
			}
			return
		case p := <-t.addpending:
			p.deadline = time.Now().Add(respTimeout)
			plist.PushBack(p)
		case r := <-t.gotreply:
			var matched bool
			for el := plist.Front(); el != nil; el = el.Next() {
				p := el.Value.(*pending)
				if p.from == r.from && p.ptype == r.ptype {
					matched = true
					if p.callback(r.data) {
						p.errc <- nil
						plist.Remove(el)
					}
					contTimeouts = 0
				}
			}
			r.matched <- matched
		case now := <-timeout.C:
			nextTimeout = nil
			for el := plist.Front(); el != nil; el = el.Next() {
				p := el.Value.(*pending)
				if now.After(p.deadline) || now.Equal(p.deadline) {
					p.errc <- errTimeout
					plist.Remove(el)
					contTimeouts++
				}
			}
			if contTimeouts > ntpFailureThreshold {
				if time.Since(ntpWarnTime) >= ntpWarningCooldown {
					ntpWarnTime = time.Now()
					go checkClockDrift()
				}
				contTimeouts = 0
			}
		}
	}
}
func (t *udp_srt) write(toaddr *net.UDPAddr, packet []byte) error {
	_, err := t.conn.WriteToUDP(packet, toaddr)
	return err
}
func (t *udp_srt) readLoop(unhandled chan<- ReadPacket) {
	if unhandled != nil {
		defer close(unhandled)
	}
	buf := make([]byte, 1280)
	for {
		nbytes, from, err := t.conn.ReadFromUDP(buf)
		if IsTemporaryError(err) {
			if config.IsLogOn(config.NetDebugLog) {
				log.Debugf("P2P SORT UDP read Temporary error %v", err)
			}
			continue
		} else if err != nil {
			if config.IsLogOn(config.NetDebugLog) {
				log.Warnf("P2P SORT UDP read error %v", err)
			}
			return
		}
		if t.HandlePacket(from, buf[:nbytes]) != nil && unhandled != nil {
			select {
			case unhandled <- ReadPacket{buf[:nbytes], from}:
			default:
			}
		}
	}
}
func (t *udp_srt) HandlePacket(from *net.UDPAddr, buf []byte) error {
	packet, fromID, hash, err := decodePacketSort(buf)
	if err != nil {
		log.Errorf("P2P Bad discover packet addr %s err %v", from, err)
		return err
	}
	return packet.handleSort(t, from, fromID, hash)
}
func (t *udp_srt) Type() DiscoveryType {
	return SRT
}
func (t *udp_srt) Version() uint32 {
	return srtVersion
}
func (t *udp_srt) NodeTable() DiscoverTable {
	return t.STable
}
func (t *udp_srt) GetChainID(id common.NodeID) (common.ChainID, error) {
	ChainDataNodes := t.GetDataNodes()
	for _, chaindatanode := range ChainDataNodes {
		for _, datanode := range chaindatanode.DataNodes {
			if id == datanode.ID {
				return chaindatanode.ChainId, nil
			}
		}
	}
	return 0, errors.New("P2P nodeId is not in the DataNodes")
}
func (t *udp_srt) Ping(toid common.NodeID, toaddr *net.UDPAddr) error {
	return <-t.SendPing(toid, toaddr, nil)
}
func (t *udp_srt) FindNode(toid common.NodeID, toaddr *net.UDPAddr, target interface{}) (map[common.ChainID][]*Node, error) {
	if t.STable == nil {
		return nil, errEmptyTable
	}
	if time.Since(t.db.lastPingReceived(toid)) > nodeDBNodeExpiration {
		t.Ping(toid, toaddr)
		t.waitping(toid)
	}
	return t.SendFindNode(toid, toaddr)
}
func (t *udp_srt) Close() error {
	t.STable.Close()
	close(t.closing)
	return t.conn.Close()
}
func (t *udp_srt) Send(toaddr *net.UDPAddr, ptype byte, req packetSort) ([]byte, error) {
	packet, hash, err := encodePacketSort(ptype, req)
	if err != nil {
		log.Errorf("P2P SORT UDP encodePacketSort error %v", err)
		return hash, err
	}
	return hash, t.write(toaddr, packet)
}
func (t *udp_srt) SendPing(toid common.NodeID, toaddr *net.UDPAddr, callback func()) <-chan error {
	req := &pingSort{
		Version:    srtVersion,
		ChainID:    t.chainId,
		NetType:    t.netType,
		From:       t.ourEndpoint,
		To:         makeEndpoint(toaddr, 0),  
		Expiration: uint64(time.Now().Add(expiration).Unix()),
	}
	packet, hash, err := encodePacketSort(pingPacket, req)
	if err != nil {
		errc := make(chan error, 1)
		errc <- err
		return errc
	}
	errc := t.pending(toid, pongPacket, func(p interface{}) bool {
		ok := bytes.Equal(p.(*pongSort).ReplyTok, hash)
		if ok && callback != nil {
			callback()
		}
		return ok
	})
	t.write(toaddr, packet)
	return errc
}
func (t *udp_srt) SendFindNode(toid common.NodeID, toaddr *net.UDPAddr) (map[common.ChainID][]*Node, error) {
	ret := make(map[common.ChainID][]*Node)
	nreceived := 0
	errc := t.pending(toid, neighborsPacket, func(r interface{}) bool {
		reply := r.(*neighborsSort)
		for _, rn := range reply.Nodes {
			nreceived++
			n, err := t.nodeFromRPC(toaddr, rn)
			if err != nil {
				continue
			}
			ret[reply.ChainID] = append(ret[reply.ChainID], n)
		}
		return nreceived >= SortTableSize
	})
	t.Send(toaddr, findnodePacket, &findnodeSort{
		Version:    srtVersion,
		ChainID:    t.chainId,
		NetType:    t.netType,
		Expiration: uint64(time.Now().Add(expiration).Unix()),
	})
	return ret, <-errc
}
func (t *udp_srt) waitping(from common.NodeID) error {
	return <-t.pending(from, pingPacket, func(interface{}) bool { return true })
}