diff --git a/client.go b/client.go index 8cfbba0..b6d507e 100644 --- a/client.go +++ b/client.go @@ -41,18 +41,20 @@ func (c *ClientCodec) WriteRequest(r *rpc.Request, body interface{}) error { // writeServiceMethod to the buffer buf.WriteString(r.ServiceMethod) // Initialize gob - enc := gob.NewEncoder(buf) - // write data to the gob - err := enc.Encode(body) - if err != nil { - return errors.E(op, err) + if body != nil { + enc := gob.NewEncoder(buf) + // write data to the gob + err := enc.Encode(body) + if err != nil { + return errors.E(op, err) + } } frame.WritePayloadLen(uint32(buf.Len())) frame.WritePayload(buf.Bytes()) frame.WriteCRC() - err = c.relay.Send(frame) + err := c.relay.Send(frame) if err != nil { return errors.E(op, err) } diff --git a/decoders.go b/decoders.go index 8e6e1f4..687873f 100644 --- a/decoders.go +++ b/decoders.go @@ -14,18 +14,26 @@ func decodeJSON(out interface{}, frame *Frame) error { if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } - return json.Unmarshal(frame.Payload()[opts[1]:], out) + payload := frame.Payload()[opts[1]:] + if len(payload) == 0 { + return nil + } + return json.Unmarshal(payload, out) } func decodeGob(out interface{}, frame *Frame) error { const op = errors.Op("client: decode GOB") - buf := new(bytes.Buffer) - dec := gob.NewDecoder(buf) opts := frame.ReadOptions() if len(opts) != 2 { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } payload := frame.Payload()[opts[1]:] + if len(payload) == 0 { + return nil + } + + buf := new(bytes.Buffer) + dec := gob.NewDecoder(buf) buf.Write(payload) return dec.Decode(out) @@ -38,6 +46,9 @@ func decodeRaw(out interface{}, frame *Frame) error { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } payload := frame.Payload()[opts[1]:] + if len(payload) == 0 { + return nil + } if raw, ok := out.(*[]byte); ok { *raw = append(*raw, payload...) @@ -54,5 +65,9 @@ func decodeMsgPack(out interface{}, frame *Frame) error { return errors.E(op, errors.Str("should be 2 options. SEQ_ID and METHOD_LEN")) } payload := frame.Payload()[opts[1]:] + if len(payload) == 0 { + return nil + } + return msgpack.Unmarshal(payload, out) }