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!
  • 相关阅读:
    多态的使用
    抽象类与具体类
    对象应该长什么样子
    方法的重载overload
    遵守合约:覆盖的规则
    Android 自定义Dialog
    less 之Extend 及 Extend all用法
    github常见错误整理!
    js获取元素宽高
    解决 Error: Access denied for user 'root'@'localhost' (using password: YES)
  • 原文地址:https://www.cnblogs.com/linguoguo/p/14677834.html
Copyright © 2011-2022 走看看