github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/core/rcv_pkt.go (about) 1 package core 2 3 import ( 4 "github.com/15mga/kiwi" 5 "github.com/15mga/kiwi/util" 6 "sync/atomic" 7 ) 8 9 func NewRcvPusPkt() *RcvPusPkt { 10 return &RcvPusPkt{} 11 } 12 13 func NewRcvReqPkt() *RcvReqPkt { 14 return &RcvReqPkt{} 15 } 16 17 func NewRcvNtfPkt() *RcvNtfPkt { 18 return &RcvNtfPkt{} 19 } 20 21 type rcvPkt struct { 22 headId string 23 senderId int64 24 msgType uint8 25 tid int64 26 svc kiwi.TSvc 27 code kiwi.TCode 28 head util.M 29 json bool 30 msg util.IMsg 31 workerType kiwi.EWorker 32 workerKey string 33 completed int32 34 } 35 36 func (p *rcvPkt) Worker() kiwi.EWorker { 37 return p.workerType 38 } 39 40 func (p *rcvPkt) WorkerKey() string { 41 return p.workerKey 42 } 43 44 func (p *rcvPkt) SetWorker(typ kiwi.EWorker, key string) { 45 p.workerType = typ 46 p.workerKey = key 47 } 48 49 func (p *rcvPkt) InitWithBytes(msgType uint8, tid int64, head util.M, json bool, payload []byte) *util.Err { 50 var ( 51 msg util.IMsg 52 err *util.Err 53 ) 54 p.svc, _ = util.MGet[kiwi.TSvc](head, HeadSvc) 55 p.code, _ = util.MGet[kiwi.TCode](head, HeadCode) 56 if json { 57 msg, err = kiwi.Codec().JsonUnmarshal2(p.svc, p.code, payload) 58 } else { 59 msg, err = kiwi.Codec().PbUnmarshal2(p.svc, p.code, payload) 60 } 61 if err != nil { 62 return err 63 } 64 p.msgType = msgType 65 p.tid = tid 66 p.head = head 67 p.json = json 68 p.msg = msg 69 p.headId, _ = util.MGet[string](p.head, HeadId) 70 p.senderId, _ = util.MGet[int64](p.head, HeadSndId) 71 atomic.AddUint64(&_ReceivePktCount, 1) 72 return nil 73 } 74 75 func (p *rcvPkt) InitWithMsg(msgType uint8, tid int64, head util.M, json bool, msg util.IMsg) { 76 p.msgType = msgType 77 p.tid = tid 78 p.head = head 79 p.json = json 80 p.msg = msg 81 p.headId, _ = util.MGet[string](p.head, HeadId) 82 p.svc, _ = util.MGet[kiwi.TSvc](p.head, HeadSvc) 83 p.code, _ = util.MGet[kiwi.TCode](p.head, HeadCode) 84 p.senderId, _ = util.MGet[int64](p.head, HeadSndId) 85 atomic.AddUint64(&_ReceivePktCount, 1) 86 } 87 88 func (p *rcvPkt) SenderId() int64 { 89 return p.senderId 90 } 91 92 func (p *rcvPkt) Tid() int64 { 93 return p.tid 94 } 95 96 func (p *rcvPkt) Svc() kiwi.TSvc { 97 return p.svc 98 } 99 100 func (p *rcvPkt) Code() kiwi.TCode { 101 return p.code 102 } 103 104 func (p *rcvPkt) Head() util.M { 105 return p.head 106 } 107 108 func (p *rcvPkt) HeadId() string { 109 return p.headId 110 } 111 112 func (p *rcvPkt) Json() bool { 113 return p.json 114 } 115 116 func (p *rcvPkt) Msg() util.IMsg { 117 return p.msg 118 } 119 120 func (p *rcvPkt) Complete() { 121 if atomic.CompareAndSwapInt32(&p.completed, 0, 1) { 122 atomic.AddUint64(&_CompletePktCount, 1) 123 } 124 } 125 126 func (p *rcvPkt) Err(err *util.Err) { 127 if err != nil { 128 kiwi.TE(p.tid, err) 129 } 130 p.Complete() 131 } 132 133 func (p *rcvPkt) Err2(code util.TErrCode, m util.M) { 134 kiwi.TE2(p.tid, code, m) 135 p.Complete() 136 } 137 138 func (p *rcvPkt) Err3(code util.TErrCode, e error) { 139 kiwi.TE3(p.tid, code, e) 140 p.Complete() 141 } 142 143 var ( 144 _ReceivePktCount uint64 145 _CompletePktCount uint64 146 _ResponseSendFailCount uint64 147 ) 148 149 func ReceivePktCount() uint64 { 150 return atomic.LoadUint64(&_ReceivePktCount) 151 } 152 153 func CompletePktCount() uint64 { 154 return atomic.LoadUint64(&_CompletePktCount) 155 } 156 157 func ResponseSendFailCount() uint64 { 158 return atomic.LoadUint64(&_ResponseSendFailCount) 159 }