本文整理匯總了Golang中github.com/libp2p/go-libp2p-net.Stream.SetProtocol方法的典型用法代碼示例。如果您正苦於以下問題:Golang Stream.SetProtocol方法的具體用法?Golang Stream.SetProtocol怎麽用?Golang Stream.SetProtocol使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/libp2p/go-libp2p-net.Stream
的用法示例。
在下文中一共展示了Stream.SetProtocol方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: newStreamHandler
// newStreamHandler is the remote-opened stream handler for inet.Network
// TODO: this feels a bit wonky
func (h *BasicHost) newStreamHandler(s inet.Stream) {
before := time.Now()
protoID, handle, err := h.Mux().Negotiate(s)
took := time.Now().Sub(before)
if err != nil {
if err == io.EOF {
logf := log.Debugf
if took > time.Second*10 {
logf = log.Warningf
}
logf("protocol EOF: %s (took %s)", s.Conn().RemotePeer(), took)
} else {
log.Warning("protocol mux failed: %s (took %s)", err, took)
}
return
}
s.SetProtocol(protocol.ID(protoID))
if h.bwc != nil {
s = mstream.WrapStream(s, h.bwc)
}
log.Debugf("protocol negotiation took %s", took)
go handle(protoID, s)
}
示例2: newStreamHandler
// newStreamHandler is the remote-opened stream handler for inet.Network
// TODO: this feels a bit wonky
func (h *BasicHost) newStreamHandler(s inet.Stream) {
before := time.Now()
if h.NegotiateTimeout != 0 {
if err := s.SetDeadline(time.Now().Add(h.NegotiateTimeout)); err != nil {
log.Error("setting stream deadline: ", err)
s.Close()
return
}
}
protoID, handle, err := h.Mux().Negotiate(s)
took := time.Now().Sub(before)
if err != nil {
if err == io.EOF {
logf := log.Debugf
if took > time.Second*10 {
logf = log.Warningf
}
logf("protocol EOF: %s (took %s)", s.Conn().RemotePeer(), took)
} else {
log.Warning("protocol mux failed: %s (took %s)", err, took)
}
s.Close()
return
}
if h.NegotiateTimeout != 0 {
if err := s.SetDeadline(time.Time{}); err != nil {
log.Error("resetting stream deadline: ", err)
s.Close()
return
}
}
s.SetProtocol(protocol.ID(protoID))
if h.bwc != nil {
s = mstream.WrapStream(s, h.bwc)
}
log.Debugf("protocol negotiation took %s", took)
go handle(protoID, s)
}