Beispiel #1
0
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	data, err := ReadRpc(c.rwc)
	if err != nil {
		return err
	}

	rsp := &PbRpcResponse{}
	err = proto.Unmarshal(data, rsp)
	if err != nil {
		return err
	}

	c.rsp = rsp

	r.Seq = rsp.GetId()
	if rsp.GetResult() == nil {
		if rsp.GetError() == "" {
			r.Error = "Unknown error occurred"
		} else {
			r.Error = rsp.GetError()
		}
	}

	return nil
}
func (self *RPCServer) Error(s string) error {
	var r rpc.Response
	r.ServiceMethod = self.lastServiceMethod
	r.Seq = self.lastSeq
	r.Error = s
	return self.codec.WriteResponse(&r, nil)
}
Beispiel #3
0
func (c *clientCodec) ReadResponseHeader(rpcres *rpc.Response) error {
	f, err := read(c.r)
	if err != nil {
		return err
	}

	res := &Response{}
	if err := proto.Unmarshal(f, res); err != nil {
		return err
	}

	c.mutex.Lock()
	p, ok := c.pending[res.GetId()]
	if !ok {
		c.mutex.Unlock()
		return errors.New("invalid sequence number in response")
	}
	c.res = res
	delete(c.pending, res.GetId())
	c.mutex.Unlock()

	rpcres.Seq = p.Seq
	rpcres.ServiceMethod = p.ServiceMethod
	rpcres.Error = res.GetError()

	return nil
}
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	c.resp.reset()
	if err := c.dec.Decode(&c.resp); err != nil {
		return err
	}

	c.mutex.Lock()
	r.ServiceMethod = c.pending[c.resp.Id]
	delete(c.pending, c.resp.Id)
	c.mutex.Unlock()

	r.Error = ""
	r.Seq = c.resp.Id
	if c.resp.Error != nil {
		x, ok := c.resp.Error.(string)
		if !ok {
			return fmt.Errorf("invalid error %v", c.resp.Error)
		}
		if x == "" {
			x = "unspecified error"
		}
		r.Error = x
	}
	return nil
}
Beispiel #5
0
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	msg := <-c.msgs
	c.resp.reset()
	if err := json.Unmarshal(msg.Body, &c.resp); err != nil {
		return err
	}
	seq, err := strconv.ParseUint(msg.CorrelationId, 0, 64)
	if err != nil {
		return err
	}
	c.Lock()
	r.Seq = seq
	r.ServiceMethod = c.pending[seq]
	delete(c.pending, seq)
	c.Unlock()

	r.Error = ""
	if c.resp.Error != nil || c.resp.Result == nil {
		x, ok := c.resp.Error.(string)
		if !ok {
			return fmt.Errorf("invalid error %v", c.resp.Error)
		}
		if x == "" {
			x = "unspecified error"
		}
		r.Error = x
	}
	return nil
}
func (self *RPCServer) Send(obj interface{}) error {
	var r rpc.Response
	r.ServiceMethod = self.lastServiceMethod
	r.Seq = self.lastSeq
	r.Error = ""
	return self.codec.WriteResponse(&r, obj)
}
Beispiel #7
0
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) (err error) {

	c.bufs.Reset()

	// Read message
	parts, err := c.sock.RecvMultipart(0)
	if err != nil {
		return
	} else if len(parts) != 2 {
		err = errors.New("Unexpected number of parts to reply: " + fmt.Sprintf("%d", len(parts)))
		return
	}
	// log.Println("Client recieved multipart mesage:", Stringify(parts))

	hdr := parts[0]
	data := parts[1]

	c.bufs.body.SetBuf(data)

	h := new(Header)
	c.bufs.header.SetBuf(hdr)
	err = c.bufs.header.Unmarshal(h)
	if err != nil {
		return
	}

	r.Seq = h.GetId()
	r.ServiceMethod = h.GetServiceMethod()
	r.Error = h.GetError()

	// log.Println("Client recieved service response:", r)

	return nil
}
Beispiel #8
0
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	// If return err:
	// - io.EOF will became ErrShutdown or io.ErrUnexpectedEOF
	// - it will be returned as is for all pending calls
	// - client will be shutdown
	// So, return io.EOF as is, return *Error for all other errors.
	if err := c.dec.Decode(&c.resp); err != nil {
		if err == io.EOF {
			return err
		}
		return NewError(errInternal.Code, err.Error())
	}
	if c.resp.ID == nil {
		return c.resp.Error
	}

	c.mutex.Lock()
	r.ServiceMethod = c.pending[*c.resp.ID]
	delete(c.pending, *c.resp.ID)
	c.mutex.Unlock()

	r.Error = ""
	r.Seq = *c.resp.ID
	if c.resp.Error != nil {
		r.Error = c.resp.Error.Error()
	}
	return nil
}
Beispiel #9
0
func (c *clientCodec) ReadResponseHeader(response *rpc.Response) error {
	if c.enableOneway {
		select {
		case ow := <-c.onewayRequests:
			response.ServiceMethod = ow.method
			response.Seq = ow.seq
			return nil
		case _ = <-c.twowayRequests:
		}
	}

	name, messageType, seq, err := c.conn.ReadMessageBegin()
	if err != nil {
		return err
	}
	response.ServiceMethod = name
	response.Seq = uint64(seq)
	if messageType == MessageTypeException {
		exception := &ApplicationException{}
		if err := DecodeStruct(c.conn, exception); err != nil {
			return err
		}
		response.Error = exception.String()
		return c.conn.ReadMessageEnd()
	}
	return nil
}
func (self *RPCServer) SendOkay() error {
	var r rpc.Response
	r.ServiceMethod = self.lastServiceMethod
	r.Seq = self.lastSeq
	r.Error = ""
	return self.codec.WriteResponse(&r, "OK")
}
Beispiel #11
0
func (codec *clientCodec) ReadResponseHeader(response *rpc.Response) (err error) {
	seq := <-codec.ready
	httpResponse := codec.responses[seq]

	if httpResponse.StatusCode < 200 || httpResponse.StatusCode >= 300 {
		return fmt.Errorf("request error: bad status code - %d", httpResponse.StatusCode)
	}

	respData, err := ioutil.ReadAll(httpResponse.Body)

	if err != nil {
		return err
	}

	httpResponse.Body.Close()

	resp := NewResponse(respData)

	if resp.Failed() {
		response.Error = fmt.Sprintf("%v", resp.Err())
	}

	codec.response = resp

	response.Seq = seq
	delete(codec.responses, seq)

	return nil
}
Beispiel #12
0
func writeResponse(c *serverCodec) error {
	head := rpc.Response{}
	head.ServiceMethod = "TestFunc"
	head.Seq = 2
	head.Error = "call failed"

	body := testpb.Result{}
	body.Title = proto.String("www.zhihu.com")
	return c.WriteResponse(&head, &body)
}
Beispiel #13
0
func (mux *multiplexCodec) ReadResponseHeader(res *rpc.Response) error {
	select {
	case err := <-mux.errorChan:
		return err
	case msg := <-mux.responseChan:
		res.ServiceMethod = responseMethod
		res.Seq = *msg.Sequence
	}
	return nil
}
Beispiel #14
0
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	var rsp protorpcpb.Response
	if err := readFrame(c.rwc, &rsp); err != nil {
		return err
	}
	r.ServiceMethod = rsp.GetMethod()
	r.Seq = rsp.GetSeq()
	r.Error = rsp.GetErr()
	return nil
}
Beispiel #15
0
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	if err := c.readResponseHeader(&c.respHeader); err != nil {
		return err
	}

	r.Seq = c.respHeader.Id
	r.ServiceMethod = c.respHeader.GetMethod()
	r.Error = c.respHeader.Error
	return nil
}
Beispiel #16
0
func (c *codec) ReadResponseHeader(r *rpc.Response) error {
	c.header = internal.Header{} // reset
	if err := c.decode(&c.header); err != nil {
		return err
	}

	r.ServiceMethod = c.header.Method
	r.Seq = c.header.SeqID
	r.Error = c.header.Error
	return nil
}
Beispiel #17
0
// ReadResponseHeader receives and decodes a net/rpc response header r.
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	var err error
	var hdr ProtoRPCResponseHeader
	if err = c.m.ReadMessage(&hdr); err != nil {
		return util.Logged(err)
	}
	r.Seq = *hdr.Seq
	r.ServiceMethod = *hdr.Op
	if hdr.Error != nil {
		r.Error = *hdr.Error
	}
	return nil
}
Beispiel #18
0
func (c *HTTPCodec) ReadResponseHeader(resp *rpc.Response) (err error) {
	req := <-c.replyQueue
	resp.Seq = req.seq
	if req.err != nil {
		resp.Error = req.err.Error()
	} else {
		if e2 := req.codec.ReadStatus(req.reply, resp); e2 != nil {
			resp.Error = e2.Error()
		}
	}
	c.bodyQueue <- req
	return
}
Beispiel #19
0
func (s *ServerImpl) sendResponse(sending *sync.Mutex, req *rpc.Request, resp *rpc.Response, reply interface{}, codec rpc.ServerCodec, errmsg string) {
	resp.ServiceMethod = req.ServiceMethod
	if errmsg != "" {
		resp.Error = errmsg
		reply = invalidRequest
	}
	resp.Seq = req.Seq
	sending.Lock()
	defer sending.Unlock()
	err := codec.WriteResponse(resp, reply)
	if err != nil {
		log.Println("rpc: writing response:", err)
	}
}
Beispiel #20
0
// sendResponses sends a stream of responses on a connection, and
// exits when the channel is closed.
func (s *Server) sendResponses(codec rpc.ServerCodec, responses <-chan serverResponse) {
	for resp := range responses {
		rpcResp := rpc.Response{
			ServiceMethod: resp.req.ServiceMethod,
			Seq:           resp.req.Seq,
		}
		if resp.err != nil {
			rpcResp.Error = resp.err.Error()
		}
		if err := codec.WriteResponse(&rpcResp, resp.reply); err != nil {
			log.Warningf("rpc: write response failed: %s", err)
			// TODO(bdarnell): what to do at this point? close the connection?
			// net/rpc just swallows the error.
		}
	}
}
Beispiel #21
0
func (c *serverCodec) WriteResponse(response *rpc.Response, thriftStruct interface{}) error {
	c.mu.Lock()
	methodName := c.methodName[response.Seq]
	delete(c.methodName, response.Seq)
	c.mu.Unlock()
	response.ServiceMethod = methodName

	mtype := byte(MessageTypeReply)
	if response.Error != "" {
		mtype = MessageTypeException
		etype := int32(ExceptionInternalError)
		if strings.HasPrefix(response.Error, "rpc: can't find") {
			etype = ExceptionUnknownMethod
		}
		thriftStruct = &ApplicationException{response.Error, etype}
	}
	if err := c.protocol.WriteMessageBegin(c.transport, response.ServiceMethod, mtype, int32(response.Seq)); err != nil {
		return err
	}
	if err := EncodeStruct(c.transport, c.protocol, thriftStruct); err != nil {
		return err
	}
	if err := c.protocol.WriteMessageEnd(c.transport); err != nil {
		return err
	}
	if flusher, ok := c.transport.(Flusher); ok {
		return flusher.Flush()
	}
	return nil
}
Beispiel #22
0
// Returns an error if response code != 200
func StandardStatusDecoder(resp *http.Response, rpcResp *rpc.Response) error {
	if resp.StatusCode != 200 {
		rpcResp.Error = fmt.Sprintf("Server returned HTTP response: %s", resp.Status)
	}

	return nil
}
Beispiel #23
0
func (this *clientCodec) ReadResponseHeader(r *rpc.Response) (e error) {
	data, e := ioutil.ReadAll(this.conn)
	if e != nil {
		return
	}
	fmt.Println("receive:", string(data))
	e = json.Unmarshal(data, &this.resp)
	fmt.Println("receive:", this.resp)
	if e != nil {
		return
	}
	r.Seq = this.resp.Id
	if this.resp.Error != nil {
		r.Error = this.resp.Error.(string)
	}
	return
}
Beispiel #24
0
func (c *clientCodec) ReadResponseHeader(response *rpc.Response) error {
	name, messageType, seq, err := c.protocol.ReadMessageBegin(c.transport)
	if err != nil {
		return err
	}
	response.ServiceMethod = name
	response.Seq = uint64(seq)
	if messageType == messageTypeException {
		exception := &ApplicationException{}
		if err := DecodeStruct(c.transport, c.protocol, exception); err != nil {
			return err
		}
		response.Error = exception.String()
		return c.protocol.ReadMessageEnd(c.transport)
	}
	return nil
}
Beispiel #25
0
// ReadResponseHeader reads the header protobuf (which is prefixed by a uvarint
// indicating its size) from the connection, decodes it, and stores the fields
// in the given request.
func (c *ClientCodec) ReadResponseHeader(resp *rpc.Response) error {
	var header wire.Header
	if err := ReadProto(c.r, &header); err != nil {
		return err
	}
	if header.Method == nil {
		return fmt.Errorf("header missing method: %s", header)
	}
	if header.Seq == nil {
		return fmt.Errorf("header missing seq: %s", header)
	}
	resp.ServiceMethod = *header.Method
	resp.Seq = *header.Seq
	if header.Error != nil {
		resp.Error = *header.Error
	}
	return nil
}
Beispiel #26
0
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error {
	var header wire.Header
	if err := readProto(c.r, &header); err != nil {
		return err
	}

	c.mutex.Lock()
	r.ServiceMethod = c.pending[header.GetId()]
	delete(c.pending, header.GetId())
	c.mutex.Unlock()

	r.Error = ""
	r.Seq = header.GetId()
	if header.Error != nil {
		r.Error = header.GetError()
	}
	return nil
}
Beispiel #27
0
func (s *codec) ReadResponseHeader(resp *rpc.Response) error {
	if s.Shutdown {
		return rpc.ErrShutdown
	}

	var header rpc4.Header
	if err := readProto(s.r, &header); err != nil {
		return err
	}
	if header.GetMethod() == "" {
		return fmt.Errorf("header missing method: %s", header)
	}

	resp.ServiceMethod = header.GetMethod()
	resp.Seq = header.GetSequence()

	s.responsePayload = header.GetPayloadPresent()
	return nil
}
Beispiel #28
0
func (c *clientHttpCodec) ReadResponseHeader(r *rpc.Response) error {
	c.resp.reset()

	//read http header
	tp := textproto.NewReader(c.r)
	line, err := tp.ReadLine()
	if err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return err
	}
	f := strings.SplitN(line, " ", 3)
	if len(f) < 2 {
		return errors.New("malformed HTTP response")
	}
	_, err = tp.ReadMIMEHeader()
	if err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		return err
	}
	_, err = readPack(c.r, &c.resp)
	if err != nil {
		return err
	}

	c.mutex.Lock()
	Id := (uint64)(c.resp.Id)
	r.ServiceMethod = c.pending[Id]
	delete(c.pending, Id)
	c.mutex.Unlock()

	r.Error = ""
	r.Seq = Id
	if c.resp.Error != "" || c.resp.Result == nil {
		fmt.Errorf("invalid error %v", c.resp.Error)
		r.Error = c.resp.Error
	}
	return nil
}
Beispiel #29
0
func (codec *clientCodec) ReadResponseHeader(response *rpc.Response) (err error) {
	seq := <-codec.ready
	httpResponse := codec.responses[seq]

	codec.responseBody, err = ioutil.ReadAll(httpResponse.Body)

	if err != nil {
		return err
	}

	httpResponse.Body.Close()

	if fault, _ := responseFailed(codec.responseBody); fault {
		response.Error = fmt.Sprintf("%v", parseFailedResponse(codec.responseBody))
	}

	response.Seq = seq
	delete(codec.responses, seq)

	return nil
}
Beispiel #30
0
func (cdc *HrpcClientCodec) ReadResponseHeader(resp *rpc.Response) error {
	hdr := common.HrpcResponseHeader{}
	err := binary.Read(cdc.rwc, binary.LittleEndian, &hdr)
	if err != nil {
		return errors.New(fmt.Sprintf("Error reading response header "+
			"bytes: %s", err.Error()))
	}
	resp.ServiceMethod = common.HrpcMethodIdToMethodName(hdr.MethodId)
	if resp.ServiceMethod == "" {
		return errors.New(fmt.Sprintf("Error reading response header: "+
			"invalid method ID %d.", hdr.MethodId))
	}
	resp.Seq = hdr.Seq
	if hdr.ErrLength > 0 {
		if hdr.ErrLength > common.MAX_HRPC_ERROR_LENGTH {
			return errors.New(fmt.Sprintf("Error reading response header: "+
				"error message was %d bytes long, but "+
				"MAX_HRPC_ERROR_LENGTH is %d.",
				hdr.ErrLength, common.MAX_HRPC_ERROR_LENGTH))
		}
		buf := make([]byte, hdr.ErrLength)
		var nread int
		nread, err = cdc.rwc.Read(buf)
		if uint32(nread) != hdr.ErrLength {
			return errors.New(fmt.Sprintf("Error reading response header: "+
				"failed to read %d bytes of error message.", nread))
		}
		if err != nil {
			return errors.New(fmt.Sprintf("Error reading response header: "+
				"failed to read %d bytes of error message: %s",
				nread, err.Error()))
		}
		resp.Error = string(buf)
	} else {
		resp.Error = ""
	}
	cdc.length = hdr.Length
	return nil
}