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 }
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 }
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 (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 (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 }
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 (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 }
// 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 }
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) }
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) }
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 }
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") }
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 { 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 }
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 }
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 }
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) }
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 }
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 }
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) error { c.resp.reset() _, 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 }
// 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 }
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 }
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) } }
// 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. } } }
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 }
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 }
// 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 }
func (s *codec) WriteResponse(resp *rpc.Response, pb interface{}) error { var header rpc4.Header header.Method, header.Sequence, header.MessageType = &resp.ServiceMethod, &resp.Seq, rpc4.Header_RESPONSE.Enum() var data []byte if resp.Error == "" { var err error if data, err = proto.Marshal(pb.(proto.Message)); err != nil { resp.Error = err.Error() } } if resp.Error != "" { header.MessageType = rpc4.Header_ERROR.Enum() data = []byte(resp.Error) } return s.writeHeaderData(&header, data) }
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 }
func (client *clientCodec) ReadResponseHeader(r *rpc.Response) error { client.curDelivery = <-client.message var err error if err = client.curDelivery.Headers.Validate(); err != nil { return errors.New("error while reading body: " + err.Error()) } if err, ok := client.curDelivery.Headers["error"]; ok { errStr, ok := err.(string) if !ok { return errors.New("error header not a string") } r.Error = errStr } r.ServiceMethod = client.curDelivery.ReplyTo r.Seq, err = strconv.ParseUint(client.curDelivery.MessageId, 10, 64) if err != nil { return err } return nil }