本文整理汇总了Golang中github.com/libp2p/go-libp2p-net.Conn.NewStream方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.NewStream方法的具体用法?Golang Conn.NewStream怎么用?Golang Conn.NewStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/libp2p/go-libp2p-net.Conn
的用法示例。
在下文中一共展示了Conn.NewStream方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: IdentifyConn
func (ids *IDService) IdentifyConn(c inet.Conn) {
ids.currmu.Lock()
if wait, found := ids.currid[c]; found {
ids.currmu.Unlock()
log.Debugf("IdentifyConn called twice on: %s", c)
<-wait // already identifying it. wait for it.
return
}
ch := make(chan struct{})
ids.currid[c] = ch
ids.currmu.Unlock()
defer close(ch)
s, err := c.NewStream()
if err != nil {
log.Debugf("error opening initial stream for %s: %s", ID, err)
log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer())
c.Close()
return
}
defer s.Close()
s.SetProtocol(ID)
if ids.Reporter != nil {
s = mstream.WrapStream(s, ids.Reporter)
}
// ok give the response to our handler.
if err := msmux.SelectProtoOrFail(ID, s); err != nil {
log.Debugf("error writing stream header for %s", ID)
log.Event(context.TODO(), "IdentifyOpenFailed", c.RemotePeer())
return
}
ids.ResponseHandler(s)
ids.currmu.Lock()
_, found := ids.currid[c]
delete(ids.currid, c)
ids.currmu.Unlock()
if !found {
log.Debugf("IdentifyConn failed to find channel (programmer error) for %s", c)
return
}
}