예제 #1
0
파일: client.go 프로젝트: weixinhost/yar.go
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
}
예제 #2
0
파일: server.go 프로젝트: weixinhost/yar.go
func (server *Server) readRequest(header *yar.Header) (*yar.Request, *yar.Error) {
	server.log(yar.LogLevelDebug, "[readRequest] %d %s %d %d", header.Id, header.Packager, header.MagicNumber, header.BodyLength)
	bodyLen := header.BodyLength
	bodyBuffer := server.body[90 : 90+bodyLen-8]

	request := yar.NewRequest()

	err := packager.Unpack(header.Packager[:], bodyBuffer, request)

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

	return request, nil
}