Esempio n. 1
0
func (c *serverCodec) WriteResponse(r *rpc.Response, x interface{}) error {
	var pb proto.Message
	if x != nil {
		var ok bool
		if pb, ok = x.(proto.Message); !ok {
			if _, ok = x.(struct{}); !ok {
				c.mutex.Lock()
				delete(c.pending, r.Seq)
				c.mutex.Unlock()
				return fmt.Errorf("ServerCodec.WriteResponse: %T does not implement proto.Message", x)
			}
		}
	}

	c.mutex.Lock()
	id, ok := c.pending[r.Seq]
	if !ok {
		c.mutex.Unlock()
		return errors.New("ServerCodec.WriteResponse: invalid sequence number in response")
	}
	delete(c.pending, r.Seq)
	c.mutex.Unlock()

	var header wire.Header
	header.Id = proto.Uint64(id)
	if r.Error != "" {
		header.Error = proto.String(r.Error)
	}

	// Write the Header and Result
	if err := writeProto(c.w, &header); err != nil {
		return err
	}
	return writeProto(c.w, pb)
}
Esempio n. 2
0
func (c *clientCodec) WriteRequest(r *rpc.Request, param interface{}) error {
	var pb proto.Message
	if param != nil {
		var ok bool
		if pb, ok = param.(proto.Message); !ok {
			return fmt.Errorf("ClientCodec.WriteRequest: %T does not implement proto.Message", param)
		}
	}

	c.mutex.Lock()
	c.pending[r.Seq] = r.ServiceMethod
	c.mutex.Unlock()

	var header wire.Header
	header.Method = proto.String(r.ServiceMethod)
	header.Id = proto.Uint64(r.Seq)

	// Write the Header and Param
	if err := writeProto(c.w, &header); err != nil {
		return err
	}
	return writeProto(c.w, pb)
}