예제 #1
0
func (self *ProtobufRequestHandler) WriteResponse(conn net.Conn, response *protocol.Response) error {
	data, err := response.Encode()
	if err != nil {
		log.Error("error encoding response: %s", err)
		return err
	}
	if len(data) >= MAX_RESPONSE_SIZE {
		pointCount := len(response.Series.Points)
		firstHalfPoints := response.Series.Points[:pointCount]
		secondHalfPoints := response.Series.Points[pointCount:]
		response.Series.Points = firstHalfPoints
		err := self.WriteResponse(conn, response)
		if err != nil {
			return err
		}
		response.Series.Points = secondHalfPoints
		return self.WriteResponse(conn, response)
	}

	buff := bytes.NewBuffer(make([]byte, 0, len(data)+8))
	binary.Write(buff, binary.LittleEndian, uint32(len(data)))
	_, err = conn.Write(append(buff.Bytes(), data...))
	if err != nil {
		log.Error("error writing response: %s", err)
		return err
	}
	return nil
}
예제 #2
0
func (self *ProtobufRequestHandler) WriteResponse(conn net.Conn, response *protocol.Response) error {
	data, err := response.Encode()
	if err != nil {
		log.Error("error encoding response: %s", err)
		return err
	}
	err = binary.Write(conn, binary.LittleEndian, uint32(len(data)))
	if err != nil {
		log.Error("error writing response length: %s", err)
		return err
	}
	_, err = conn.Write(data)
	return err
}