本文整理匯總了Golang中github.com/multiformats/go-multiaddr.Multiaddr類的典型用法代碼示例。如果您正苦於以下問題:Golang Multiaddr類的具體用法?Golang Multiaddr怎麽用?Golang Multiaddr使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Multiaddr類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: addrInAddrs
func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool {
for _, b := range as {
if a.Equal(b) {
return true
}
}
return false
}
示例2: addPortMapping
func addPortMapping(nmgr *natManager, intaddr ma.Multiaddr) {
nat := nmgr.NAT()
if nat == nil {
panic("natManager addPortMapping called without a nat.")
}
// first, check if the port mapping already exists.
for _, mapping := range nat.Mappings() {
if mapping.InternalAddr().Equal(intaddr) {
return // it exists! return.
}
}
ctx := context.TODO()
lm := make(lgbl.DeferredMap)
lm["internalAddr"] = func() interface{} { return intaddr.String() }
defer log.EventBegin(ctx, "natMgrAddPortMappingWait", lm).Done()
select {
case <-nmgr.proc.Closing():
lm["outcome"] = "cancelled"
return // no use.
case <-nmgr.ready: // wait until it's ready.
}
// actually start the port map (sub-event because waiting may take a while)
defer log.EventBegin(ctx, "natMgrAddPortMapping", lm).Done()
// get the nat
m, err := nat.NewMapping(intaddr)
if err != nil {
lm["outcome"] = "failure"
lm["error"] = err
return
}
extaddr, err := m.ExternalAddr()
if err != nil {
lm["outcome"] = "failure"
lm["error"] = err
return
}
lm["outcome"] = "success"
lm["externalAddr"] = func() interface{} { return extaddr.String() }
log.Infof("established nat port mapping: %s <--> %s", intaddr, extaddr)
}
示例3: NewMapping
// NewMapping attemps to construct a mapping on protocol and internal port
// It will also periodically renew the mapping until the returned Mapping
// -- or its parent NAT -- is Closed.
//
// May not succeed, and mappings may change over time;
// NAT devices may not respect our port requests, and even lie.
// Clients should not store the mapped results, but rather always
// poll our object for the latest mappings.
func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
if nat == nil {
return nil, fmt.Errorf("no nat available")
}
network, addr, err := manet.DialArgs(maddr)
if err != nil {
return nil, fmt.Errorf("DialArgs failed on addr:", maddr.String())
}
switch network {
case "tcp", "tcp4", "tcp6":
network = "tcp"
case "udp", "udp4", "udp6":
network = "udp"
default:
return nil, fmt.Errorf("transport not supported by NAT: %s", network)
}
intports := strings.Split(addr, ":")[1]
intport, err := strconv.Atoi(intports)
if err != nil {
return nil, err
}
m := &mapping{
nat: nat,
proto: network,
intport: intport,
intaddr: maddr,
}
m.proc = goprocess.WithTeardown(func() error {
nat.rmMapping(m)
return nil
})
nat.addMapping(m)
m.proc.AddChild(periodic.Every(MappingDuration/3, func(worker goprocess.Process) {
nat.establishMapping(m)
}))
// do it once synchronously, so first mapping is done right away, and before exiting,
// allowing users -- in the optimistic case -- to use results right after.
nat.establishMapping(m)
return m, nil
}
示例4: HasConsistentTransport
// HasConsistentTransport returns true if the address 'a' shares a
// protocol set with any address in the green set. This is used
// to check if a given address might be one of the addresses a peer is
// listening on.
func HasConsistentTransport(a ma.Multiaddr, green []ma.Multiaddr) bool {
protosMatch := func(a, b []ma.Protocol) bool {
if len(a) != len(b) {
return false
}
for i, p := range a {
if b[i].Code != p.Code {
return false
}
}
return true
}
protos := a.Protocols()
for _, ga := range green {
if protosMatch(protos, ga.Protocols()) {
return true
}
}
return false
}
示例5: Matches
func (ptrn *pattern) Matches(a ma.Multiaddr) bool {
ok, rem := ptrn.partialMatch(a.Protocols())
return ok && len(rem) == 0
}