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!
  • 相关阅读:
    第二十章 springboot + consul(1)
    附2 hystrix详述(2)- 配置
    附1 hystrix详述(1)
    第十九章 springboot + hystrix(1)
    第十八章 springboot + thymeleaf
    第十七章 springboot + devtools(热部署)
    Nginx(二):虚拟主机配置
    SpringMVC中异常处理详解
    五分钟读懂UML类图
    Java web中WEB-INF目录理解
  • 原文地址:https://www.cnblogs.com/linguoguo/p/14677834.html
Copyright © 2011-2022 走看看