本文整理匯總了Golang中github.com/libp2p/go-libp2p-net.Stream.Write方法的典型用法代碼示例。如果您正苦於以下問題:Golang Stream.Write方法的具體用法?Golang Stream.Write怎麽用?Golang Stream.Write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/libp2p/go-libp2p-net.Stream
的用法示例。
在下文中一共展示了Stream.Write方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: doEcho
// doEcho reads some data from a stream, writes it back and closes the
// stream.
func doEcho(s inet.Stream) {
buf := make([]byte, 1024)
n, err := s.Read(buf)
if err != nil {
log.Println(err)
return
}
log.Printf("read request: %q\n", buf[:n])
_, err = s.Write(buf[:n])
if err != nil {
log.Println(err)
return
}
}
示例3: ping
func ping(s inet.Stream) (time.Duration, error) {
buf := make([]byte, PingSize)
u.NewTimeSeededRand().Read(buf)
before := time.Now()
_, err := s.Write(buf)
if err != nil {
return 0, err
}
rbuf := make([]byte, PingSize)
_, err = io.ReadFull(s, rbuf)
if err != nil {
return 0, err
}
if !bytes.Equal(buf, rbuf) {
return 0, errors.New("ping packet was incorrect!")
}
return time.Since(before), nil
}