本文整理汇总了Golang中github.com/libp2p/go-libp2p-net.Stream.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Stream.Close方法的具体用法?Golang Stream.Close怎么用?Golang Stream.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/libp2p/go-libp2p-net.Stream
的用法示例。
在下文中一共展示了Stream.Close方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PingHandler
func (p *PingService) PingHandler(s inet.Stream) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
buf := make([]byte, PingSize)
timer := time.NewTimer(pingTimeout)
defer timer.Stop()
go func() {
select {
case <-timer.C:
case <-ctx.Done():
}
s.Close()
}()
for {
_, err := io.ReadFull(s, buf)
if err != nil {
log.Debug(err)
return
}
_, err = s.Write(buf)
if err != nil {
log.Debug(err)
return
}
timer.Reset(pingTimeout)
}
}
示例2: EchoStreamHandler
func EchoStreamHandler(stream inet.Stream) {
c := stream.Conn()
log.Debugf("%s echoing %s", c.LocalPeer(), c.RemotePeer())
go func() {
defer stream.Close()
io.Copy(stream, stream)
}()
}
示例3: ResponseHandler
func (ids *IDService) ResponseHandler(s inet.Stream) {
defer s.Close()
c := s.Conn()
r := ggio.NewDelimitedReader(s, 2048)
mes := pb.Identify{}
if err := r.ReadMsg(&mes); err != nil {
return
}
ids.consumeMessage(&mes, c)
log.Debugf("%s received message from %s %s", ID,
c.RemotePeer(), c.RemoteMultiaddr())
}
示例4: RequestHandler
func (ids *IDService) RequestHandler(s inet.Stream) {
defer s.Close()
c := s.Conn()
bwc := ids.Host.GetBandwidthReporter()
s = mstream.WrapStream(s, bwc)
w := ggio.NewDelimitedWriter(s)
mes := pb.Identify{}
ids.populateMessage(&mes, s.Conn())
w.WriteMsg(&mes)
log.Debugf("%s sent message to %s %s", ID,
c.RemotePeer(), c.RemoteMultiaddr())
}
示例5: handleStream
// handleStream is our own handler, which returns an error for simplicity.
func (rs *RelayService) handleStream(s inet.Stream) error {
defer s.Close()
// read the header (src and dst peer.IDs)
src, dst, err := ReadHeader(s)
if err != nil {
return fmt.Errorf("stream with bad header: %s", err)
}
local := rs.host.ID()
switch {
case src == local:
return fmt.Errorf("relaying from self")
case dst == local: // it's for us! yaaay.
log.Debugf("%s consuming stream from %s", local, src)
return rs.consumeStream(s)
default: // src and dst are not local. relay it.
log.Debugf("%s relaying stream %s <--> %s", local, src, dst)
return rs.pipeStream(src, dst, s)
}
}
示例6: 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)
}