当前位置: 首页>>代码示例>>Golang>>正文


Golang Stream.Write方法代码示例

本文整理汇总了Golang中github.com/ipfs/go-ipfs/p2p/net.Stream.Write方法的典型用法代码示例。如果您正苦于以下问题:Golang Stream.Write方法的具体用法?Golang Stream.Write怎么用?Golang Stream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/ipfs/go-ipfs/p2p/net.Stream的用法示例。


在下文中一共展示了Stream.Write方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: EchoStreamHandler

func EchoStreamHandler(stream inet.Stream) {
	go func() {
		defer stream.Close()

		// pull out the ipfs conn
		c := stream.Conn()
		log.Infof("%s ponging to %s", c.LocalPeer(), c.RemotePeer())

		buf := make([]byte, 4)

		for {
			if _, err := stream.Read(buf); err != nil {
				if err != io.EOF {
					log.Info("ping receive error:", err)
				}
				return
			}

			if !bytes.Equal(buf, []byte("ping")) {
				log.Infof("ping receive error: ping != %s %v", buf, buf)
				return
			}

			if _, err := stream.Write([]byte("pong")); err != nil {
				log.Info("pond send error:", err)
				return
			}
		}
	}()
}
开发者ID:avbalu,项目名称:go-ipfs,代码行数:30,代码来源:swarm_test.go

示例2: WriteResponse

//WriteResponse writes hash of recent to con.
func WriteResponse(con net.Stream, recent key.Key) error {
	var r []byte
	if recent != "" {
		r = []byte(recent)
	}
	msg := &NodeProto{
		Id: r,
	}
	dat, err := proto.Marshal(msg)
	if log.If(err) {
		return err
	}
	_, err = con.Write(dat)
	log.If(err)
	return err
}
开发者ID:utamaro,项目名称:core,代码行数:17,代码来源:talk.go

示例3: PingHandler

func (p *PingService) PingHandler(s inet.Stream) {
	buf := make([]byte, PingSize)

	for {
		_, err := io.ReadFull(s, buf)
		if err != nil {
			log.Debug(err)
			return
		}

		_, err = s.Write(buf)
		if err != nil {
			log.Debug(err)
			return
		}
	}
}
开发者ID:heems,项目名称:go-ipfs,代码行数:17,代码来源:ping.go

示例4: WriteRequest

//WriteRequest writes hash of recent and peers DAG to con.
func WriteRequest(con net.Stream, recent key.Key, peers key.Key) error {
	var r, p []byte
	if peers != "" {
		p = []byte(peers)
	}
	if recent != "" {
		r = []byte(recent)
	}
	msg := &Msg{
		Type:   Type_Recent.Enum(),
		Recent: r,
		Peers:  p,
	}
	dat, err := proto.Marshal(msg)
	if log.If(err) {
		return err
	}
	_, err = con.Write(dat)
	log.If(err)
	return err
}
开发者ID:utamaro,项目名称:core,代码行数:22,代码来源:talk.go

示例5: 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.Now().Sub(before), nil
}
开发者ID:heems,项目名称:go-ipfs,代码行数:22,代码来源:ping.go


注:本文中的github.com/ipfs/go-ipfs/p2p/net.Stream.Write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。