func (c *codec) Write(meta *metapb.RpcMeta, x interface{}, cw compressWriter) error {
	buffer := writeBufferPool.Get().(*bytes.Buffer)
	buffer.Reset()
	defer writeBufferPool.Put(buffer)

	metasize := proto.Size(meta)
	h := rpcHeader{
		Magic: MagicStr,
	}
	h.X.MetaSize = int32(metasize)
	h.X.PacketSize = int32(metasize)

	// write header, for placeholder, we will change PacketSize later if no error in meta
	binary.Write(buffer, binary.BigEndian, &h)

	// write meta
	buf, err := proto.Marshal(meta)
	if err != nil {
		return err
	}
	buffer.Write(buf)

	// skip write body if error
	if meta.Response != nil && meta.Response.ErrorCode != 0 {
		buffer.WriteTo(c.w)
		return c.w.Flush()
	}

	// write body
	msg := x.(proto.Message)
	buf, err = proto.Marshal(msg)
	if err != nil {
		return err
	}
	// record the offset before we write data
	len1 := buffer.Len()
	if cw != nil {
		wc, err := cw(buffer)
		if err != nil {
			return err
		}
		wc.Write(buf)
		wc.Close()
	} else {
		buffer.Write(buf)
	}

	dataSize := buffer.Len() - len1
	h.X.PacketSize = int32(metasize + dataSize)
	// write new header
	w := bytes.NewBuffer(buffer.Bytes()[:0])
	binary.Write(w, binary.BigEndian, &h)

	buffer.WriteTo(c.w)
	return c.w.Flush()
}
// Write send rpc header and body to peer
Write send rpc header and body to peer
// Write send rpc header and body to peer
func (c *codec) Write(meta *metapb.RpcMeta, x interface{}, cw compressWriter) error {
	buffer := writeBufferPool.Get().(*bytes.Buffer)
	buffer.Reset()
	defer writeBufferPool.Put(buffer)
	metasize := proto.Size(meta)
	h := rpcHeader{
		Magic: MagicStr,
	}
	h.X.MetaSize = int32(metasize)
	h.X.PacketSize = int32(metasize)
	binary.Write(buffer, binary.BigEndian, &h)
	buf, err := proto.Marshal(meta)
	if err != nil {
		return err
	}
	buffer.Write(buf)
	if meta.Response != nil && meta.Response.ErrorCode != 0 {
		buffer.WriteTo(c.w)
		return c.w.Flush()
	}
	msg := x.(proto.Message)
	buf, err = proto.Marshal(msg)
	if err != nil {
		return err
	}
	len1 := buffer.Len()
	if cw != nil {
		wc, err := cw(buffer)
		if err != nil {
			return err
		}
		wc.Write(buf)
		wc.Close()
	} else {
		buffer.Write(buf)
	}
	dataSize := buffer.Len() - len1
	h.X.PacketSize = int32(metasize + dataSize)
	w := bytes.NewBuffer(buffer.Bytes()[:0])
	binary.Write(w, binary.BigEndian, &h)
	buffer.WriteTo(c.w)
	return c.w.Flush()
}
func ( c * codec ) Write ( meta * metapb . RpcMeta , x interface { } , cw compressWriter ) error { buffer := writeBufferPool . Get ( ) . ( * bytes . Buffer ) buffer . Reset ( ) defer writeBufferPool . Put ( buffer ) metasize := proto . Size ( meta ) h := rpcHeader { Magic : MagicStr , } h . X . MetaSize = int32 ( metasize ) h . X . PacketSize = int32 ( metasize ) binary . Write ( buffer , binary . BigEndian , & h ) buf , err := proto . Marshal ( meta ) if err != nil { return err } buffer . Write ( buf ) if meta . Response != nil && meta . Response . ErrorCode != 0 { buffer . WriteTo ( c . w ) return c . w . Flush ( ) } msg := x . ( proto . Message ) buf , err = proto . Marshal ( msg ) if err != nil { return err } len1 := buffer . Len ( ) if cw != nil { wc , err := cw ( buffer ) if err != nil { return err } wc . Write ( buf ) wc . Close ( ) } else { buffer . Write ( buf ) } dataSize := buffer . Len ( ) - len1 h . X . PacketSize = int32 ( metasize + dataSize ) w := bytes . NewBuffer ( buffer . Bytes ( ) [ : 0 ] ) binary . Write ( w , binary . BigEndian , & h ) buffer . WriteTo ( c . w ) return c . w . Flush ( ) }
Write send rpc header and body to peer
Write send rpc header and body to peer
// write header, for placeholder, we will change PacketSize later if no error in meta // write meta // skip write body if error // write body // record the offset before we write data // write new header