Example #1
0
func (client *Client) packRequest(r *yar.Request) ([]byte, *yar.Error) {

	var sendPackager []byte
	packagerName := client.Opt.Packager

	if len(packagerName) < yar.PackagerLength {
		sendPackager = []byte(packagerName)
	} else {
		sendPackager = []byte(packagerName[0:yar.PackagerLength])
	}

	var p [8]byte
	for i, s := range sendPackager {
		p[i] = s
	}

	r.Protocol.Packager = p
	pack, err := packager.Pack(sendPackager, r)

	if err != nil {
		return nil, yar.NewError(yar.ErrorPackager, err.Error())
	}

	return pack, nil
}
Example #2
0
func (client *Client) readResponse(reader io.Reader, ret interface{}) *yar.Error {

	allBody, err := ioutil.ReadAll(reader)

	if err != nil {
		return yar.NewError(yar.ErrorResponse, "Read Response Error:"+err.Error())
	}

	if len(allBody) < (yar.ProtocolLength + yar.PackagerLength) {
		return yar.NewError(yar.ErrorResponse, "Response Parse Error:"+string(allBody))
	}

	protocolBuffer := allBody[0 : yar.ProtocolLength+yar.PackagerLength]

	protocol := yar.NewHeader()

	protocol.Init(bytes.NewBuffer(protocolBuffer))

	bodyLength := protocol.BodyLength - yar.PackagerLength

	if uint32(len(allBody)-(yar.ProtocolLength+yar.PackagerLength)) < uint32(bodyLength) {
		return yar.NewError(yar.ErrorResponse, "Response Content Error:"+string(allBody))
	}

	bodyBuffer := allBody[yar.ProtocolLength+yar.PackagerLength:]

	response := new(yar.Response)
	err = packager.Unpack([]byte(client.Opt.Packager), bodyBuffer, &response)

	if err != nil {
		return yar.NewError(yar.ErrorPackager, "Unpack Error:"+err.Error())
	}

	if response.Status != yar.ERR_OKEY {
		return yar.NewError(yar.ErrorResponse, response.Error)
	}

	if ret != nil {

		packData, err := packager.Pack([]byte(client.Opt.Packager), response.Retval)

		if err != nil {
			return yar.NewError(yar.ErrorPackager, "pack response retval error:"+err.Error()+" "+string(allBody))
		}

		err = packager.Unpack([]byte(client.Opt.Packager), packData, ret)

		if err != nil {
			return yar.NewError(yar.ErrorPackager, "pack response retval error:"+err.Error()+" "+string(allBody))
		}
	}

	return nil
}
Example #3
0
func (server *Server) sendResponse(response *yar.Response) *yar.Error {
	server.log(yar.LogLevelDebug, "[sendResponse] %d %d %s", response.Id, response.Status, fmt.Sprint(response.Retval))
	sendPackData, err := packager.Pack(response.Protocol.Packager[:], response)
	if err != nil {
		return yar.NewError(yar.ErrorResponse, err.Error())
	}
	response.Protocol.BodyLength = uint32(len(sendPackData) + 8)
	server.writer.Write(response.Protocol.Bytes().Bytes())
	server.writer.Write(sendPackData)
	return nil

}