package pgproto3

import (
	
	
	
	

	
)

const cancelRequestCode = 80877102

type CancelRequest struct {
	ProcessID uint32
	SecretKey []byte
}

// Frontend identifies this message as sendable by a PostgreSQL frontend.
func (*CancelRequest) () {}

func ( *CancelRequest) ( []byte) error {
	if len() < 12 {
		return errors.New("cancel request too short")
	}
	if len() > 264 {
		return errors.New("cancel request too long")
	}

	 := binary.BigEndian.Uint32()
	if  != cancelRequestCode {
		return errors.New("bad cancel request code")
	}

	.ProcessID = binary.BigEndian.Uint32([4:])
	.SecretKey = make([]byte, len()-8)
	copy(.SecretKey, [8:])

	return nil
}

// Encode encodes src into dst. dst will include the 4 byte message length.
func ( *CancelRequest) ( []byte) ([]byte, error) {
	if len(.SecretKey) > 256 {
		return nil, errors.New("secret key too long")
	}
	 := int32(12 + len(.SecretKey))
	 = pgio.AppendInt32(, )
	 = pgio.AppendInt32(, cancelRequestCode)
	 = pgio.AppendUint32(, .ProcessID)
	 = append(, .SecretKey...)
	return , nil
}

// MarshalJSON implements encoding/json.Marshaler.
func ( CancelRequest) () ([]byte, error) {
	return json.Marshal(struct {
		      string
		 uint32
		 string
	}{
		:      "CancelRequest",
		: .ProcessID,
		: hex.EncodeToString(.SecretKey),
	})
}

// UnmarshalJSON implements encoding/json.Unmarshaler.
func ( *CancelRequest) ( []byte) error {
	var  struct {
		 uint32
		 string
	}
	if  := json.Unmarshal(, &);  != nil {
		return 
	}

	.ProcessID = .
	,  := hex.DecodeString(.)
	if  != nil {
		return 
	}
	.SecretKey = 
	return nil
}