gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

cancel_request.go (1300B)


      1 package pgproto3
      2 
      3 import (
      4 	"encoding/binary"
      5 	"encoding/json"
      6 	"errors"
      7 
      8 	"github.com/jackc/pgio"
      9 )
     10 
     11 const cancelRequestCode = 80877102
     12 
     13 type CancelRequest struct {
     14 	ProcessID uint32
     15 	SecretKey uint32
     16 }
     17 
     18 // Frontend identifies this message as sendable by a PostgreSQL frontend.
     19 func (*CancelRequest) Frontend() {}
     20 
     21 func (dst *CancelRequest) Decode(src []byte) error {
     22 	if len(src) != 12 {
     23 		return errors.New("bad cancel request size")
     24 	}
     25 
     26 	requestCode := binary.BigEndian.Uint32(src)
     27 
     28 	if requestCode != cancelRequestCode {
     29 		return errors.New("bad cancel request code")
     30 	}
     31 
     32 	dst.ProcessID = binary.BigEndian.Uint32(src[4:])
     33 	dst.SecretKey = binary.BigEndian.Uint32(src[8:])
     34 
     35 	return nil
     36 }
     37 
     38 // Encode encodes src into dst. dst will include the 4 byte message length.
     39 func (src *CancelRequest) Encode(dst []byte) []byte {
     40 	dst = pgio.AppendInt32(dst, 16)
     41 	dst = pgio.AppendInt32(dst, cancelRequestCode)
     42 	dst = pgio.AppendUint32(dst, src.ProcessID)
     43 	dst = pgio.AppendUint32(dst, src.SecretKey)
     44 	return dst
     45 }
     46 
     47 // MarshalJSON implements encoding/json.Marshaler.
     48 func (src CancelRequest) MarshalJSON() ([]byte, error) {
     49 	return json.Marshal(struct {
     50 		Type      string
     51 		ProcessID uint32
     52 		SecretKey uint32
     53 	}{
     54 		Type:      "CancelRequest",
     55 		ProcessID: src.ProcessID,
     56 		SecretKey: src.SecretKey,
     57 	})
     58 }