|
@@ -20,51 +20,67 @@ type request struct {
|
|
|
body protocolBody
|
|
|
}
|
|
|
|
|
|
-func (r *request) encode(pe packetEncoder) (err error) {
|
|
|
+func (r *request) encode(pe packetEncoder) error {
|
|
|
pe.push(&lengthField{})
|
|
|
pe.putInt16(r.body.key())
|
|
|
pe.putInt16(r.body.version())
|
|
|
pe.putInt32(r.correlationID)
|
|
|
- err = pe.putString(r.clientID)
|
|
|
+
|
|
|
+ err := pe.putString(r.clientID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
err = r.body.encode(pe)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
return pe.pop()
|
|
|
}
|
|
|
|
|
|
func (r *request) decode(pd packetDecoder) (err error) {
|
|
|
- var key int16
|
|
|
- if key, err = pd.getInt16(); err != nil {
|
|
|
+ key, err := pd.getInt16()
|
|
|
+ if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- var version int16
|
|
|
- if version, err = pd.getInt16(); err != nil {
|
|
|
+
|
|
|
+ version, err := pd.getInt16()
|
|
|
+ if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- if r.correlationID, err = pd.getInt32(); err != nil {
|
|
|
+
|
|
|
+ r.correlationID, err = pd.getInt32()
|
|
|
+ if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
r.clientID, err = pd.getString()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
r.body = allocateBody(key, version)
|
|
|
if r.body == nil {
|
|
|
return PacketDecodingError{fmt.Sprintf("unknown request key (%d)", key)}
|
|
|
}
|
|
|
+
|
|
|
return r.body.decode(pd, version)
|
|
|
}
|
|
|
|
|
|
-func decodeRequest(r io.Reader) (req *request, bytesRead int, err error) {
|
|
|
- lengthBytes := make([]byte, 4)
|
|
|
+func decodeRequest(r io.Reader) (*request, int, error) {
|
|
|
+ var (
|
|
|
+ bytesRead int
|
|
|
+ lengthBytes = make([]byte, 4)
|
|
|
+ )
|
|
|
+
|
|
|
if _, err := io.ReadFull(r, lengthBytes); err != nil {
|
|
|
return nil, bytesRead, err
|
|
|
}
|
|
|
- bytesRead += len(lengthBytes)
|
|
|
|
|
|
+ bytesRead += len(lengthBytes)
|
|
|
length := int32(binary.BigEndian.Uint32(lengthBytes))
|
|
|
+
|
|
|
if length <= 4 || length > MaxRequestSize {
|
|
|
return nil, bytesRead, PacketDecodingError{fmt.Sprintf("message of length %d too large or too small", length)}
|
|
|
}
|
|
@@ -73,12 +89,14 @@ func decodeRequest(r io.Reader) (req *request, bytesRead int, err error) {
|
|
|
if _, err := io.ReadFull(r, encodedReq); err != nil {
|
|
|
return nil, bytesRead, err
|
|
|
}
|
|
|
+
|
|
|
bytesRead += len(encodedReq)
|
|
|
|
|
|
- req = &request{}
|
|
|
+ req := &request{}
|
|
|
if err := decode(encodedReq, req); err != nil {
|
|
|
return nil, bytesRead, err
|
|
|
}
|
|
|
+
|
|
|
return req, bytesRead, nil
|
|
|
}
|
|
|
|