zoukankan      html  css  js  c++  java
  • go-micro codec 编码

      codec 用于各种信息的加解密,具体接口如下:

    // Codec encodes/decodes various types of messages used within go-micro.
    // ReadHeader and ReadBody are called in pairs to read requests/responses
    // from the connection. Close is called when finished with the
    // connection. ReadBody may be called with a nil argument to force the
    // body to be read and discarded.
    type Codec interface {
    	ReadHeader(*Message, MessageType) error
    	ReadBody(interface{}) error
    	Write(*Message, interface{}) error
    	Close() error
    	String() string
    }
    
    // Message represents detailed information about
    // the communication, likely followed by the body.
    // In the case of an error, body may be nil.
    type Message struct {
    	Id     uint64
    	Type   MessageType
    	Target string
    	Method string
    	Error  string
    	Header map[string]string
    }
    

      

      读写接口处理;

    // WriteNetString writes data to a big-endian netstring on a Writer.
    // Size is always a 32-bit unsigned int.
    func WriteNetString(w io.Writer, data []byte) (written int, err error) {
    	size := make([]byte, 4)
    	binary.BigEndian.PutUint32(size, uint32(len(data)))
    	if written, err = w.Write(size); err != nil {
    		return
    	}
    	return w.Write(data)
    }
    
    // ReadNetString reads data from a big-endian netstring.
    func ReadNetString(r io.Reader) (data []byte, err error) {
    	sizeBuf := make([]byte, 4)
    	_, err = r.Read(sizeBuf)
    	if err != nil {
    		return nil, err
    	}
    	size := binary.BigEndian.Uint32(sizeBuf)
    	if size == 0 {
    		return nil, nil
    	}
    	data = make([]byte, size)
    	_, err = r.Read(data)
    	if err != nil {
    		return nil, err
    	}
    	return
    }
    

      

    you are the best!
  • 相关阅读:
    课堂检测求一个数的各个位
    猜数字
    登录界面
    课后作业01
    java语言基础问题
    大道至简第一张用java撰写伪代码
    阅读“大道至简”的读后感
    iOS 瀑布流的基本原理
    iOS 生成二维码
    CNContact对通讯录的基本使用(第二篇)
  • 原文地址:https://www.cnblogs.com/linguoguo/p/14677834.html
Copyright © 2011-2022 走看看