github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/core/packer.go (about) 1 package core 2 3 import ( 4 "github.com/15mga/kiwi" 5 "github.com/15mga/kiwi/util" 6 ) 7 8 func InitPacker() { 9 kiwi.SetPacker(&packer{}) 10 } 11 12 type packer struct { 13 } 14 15 func (p *packer) PackWatchNotify(id int64, codes []kiwi.TCode) []byte { 16 var buffer util.ByteBuffer 17 buffer.InitCap(11 + len(codes)*2) 18 buffer.WUint8(HdWatch) 19 buffer.WInt64(id) 20 buffer.WUint8s(codes) 21 return buffer.All() 22 } 23 24 func (p *packer) UnpackWatchNotify(bytes []byte) (id int64, codes []kiwi.TCode, err *util.Err) { 25 var buffer util.ByteBuffer 26 buffer.InitBytes(bytes) 27 buffer.SetPos(1) 28 id, err = buffer.RInt64() 29 if err != nil { 30 return 31 } 32 codes, err = buffer.RUint8s() 33 return 34 } 35 36 func (p *packer) PackPush(tid int64, pus kiwi.ISndPush) ([]byte, *util.Err) { 37 var buffer util.ByteBuffer 38 buffer.InitCap(256) 39 buffer.WUint8(HdPush) 40 buffer.WInt64(tid) 41 err := buffer.WMAny(pus.Head()) 42 if err != nil { 43 return nil, err 44 } 45 buffer.WBool(pus.Json()) 46 _, e := buffer.Write(pus.Payload()) 47 if e != nil { 48 return nil, util.NewErr(util.EcWriteFail, util.M{ 49 "error": e, 50 }) 51 } 52 return buffer.All(), nil 53 } 54 55 func (p *packer) UnpackPush(bytes []byte, pkg kiwi.IRcvPush) (err *util.Err) { 56 var buffer util.ByteBuffer 57 buffer.InitBytes(bytes) 58 buffer.SetPos(1) 59 tid, err := buffer.RInt64() 60 if err != nil { 61 return 62 } 63 head := make(util.M) 64 err = buffer.RMAny(head) 65 if err != nil { 66 return 67 } 68 json, err := buffer.RBool() 69 if err != nil { 70 return 71 } 72 bs := buffer.RAvailable() 73 return pkg.InitWithBytes(HdPush, tid, head, json, bs) 74 } 75 76 func (p *packer) UnpackPushBytes(bytes []byte, head util.M) (tid int64, json bool, payload []byte, err *util.Err) { 77 var buffer util.ByteBuffer 78 buffer.InitBytes(bytes) 79 buffer.SetPos(1) 80 tid, err = buffer.RInt64() 81 if err != nil { 82 return 83 } 84 err = buffer.RMAny(head) 85 if err != nil { 86 return 87 } 88 json, err = buffer.RBool() 89 if err != nil { 90 return 91 } 92 payload = buffer.RAvailable() 93 return 94 } 95 96 func (p *packer) PackRequest(tid int64, req kiwi.ISndRequest) ([]byte, *util.Err) { 97 var buffer util.ByteBuffer 98 buffer.InitCap(256) 99 buffer.WUint8(HdRequest) 100 buffer.WInt64(kiwi.GetNodeMeta().NodeId) 101 buffer.WInt64(tid) 102 err := buffer.WMAny(req.Head()) 103 if err != nil { 104 return nil, err 105 } 106 buffer.WBool(req.Json()) 107 _, e := buffer.Write(req.Payload()) 108 if e != nil { 109 return nil, util.NewErr(util.EcWriteFail, util.M{ 110 "error": e, 111 }) 112 } 113 return buffer.All(), nil 114 } 115 116 func (p *packer) UnpackRequest(bytes []byte, pkg kiwi.IRcvRequest) (err *util.Err) { 117 var buffer util.ByteBuffer 118 buffer.InitBytes(bytes) 119 buffer.SetPos(1) 120 tid, err := buffer.RInt64() 121 if err != nil { 122 return 123 } 124 head := make(util.M) 125 err = buffer.RMAny(head) 126 if err != nil { 127 return 128 } 129 json, err := buffer.RBool() 130 if err != nil { 131 return 132 } 133 payload := buffer.RAvailable() 134 return pkg.InitWithBytes(HdRequest, tid, head, json, payload) 135 } 136 137 func (p *packer) PackResponseOk(tid int64, head util.M, pkt []byte) ([]byte, *util.Err) { 138 var buffer util.ByteBuffer 139 buffer.InitCap(256) 140 buffer.WUint8(HdOk) 141 buffer.WInt64(tid) 142 err := buffer.WMAny(head) 143 if err != nil { 144 return nil, err 145 } 146 _, e := buffer.Write(pkt) 147 if e != nil { 148 return nil, util.NewErr(util.EcWriteFail, util.M{ 149 "error": e, 150 }) 151 } 152 return buffer.All(), nil 153 } 154 155 func (p *packer) UnpackResponseOk(bytes []byte, head util.M) (tid int64, payload []byte, err *util.Err) { 156 var buffer util.ByteBuffer 157 buffer.InitBytes(bytes) 158 buffer.SetPos(1) 159 tid, err = buffer.RInt64() 160 if err != nil { 161 return 162 } 163 err = buffer.RMAny(head) 164 if err != nil { 165 return 166 } 167 payload = buffer.RAvailable() 168 return 169 } 170 171 func (p *packer) PackResponseFail(tid int64, head util.M, code uint16) ([]byte, *util.Err) { 172 var buffer util.ByteBuffer 173 buffer.InitCap(13) 174 buffer.WUint8(HdFail) 175 buffer.WInt64(tid) 176 err := buffer.WMAny(head) 177 if err != nil { 178 return nil, err 179 } 180 buffer.WUint16(code) 181 return buffer.All(), nil 182 } 183 184 func (p *packer) UnpackResponseFail(bytes []byte, head util.M) (tid int64, code uint16, err *util.Err) { 185 var buffer util.ByteBuffer 186 buffer.InitBytes(bytes) 187 buffer.SetPos(1) 188 tid, err = buffer.RInt64() 189 if err != nil { 190 return 191 } 192 err = buffer.RMAny(head) 193 if err != nil { 194 return 195 } 196 code, err = buffer.RUint16() 197 return 198 } 199 200 func (p *packer) PackNotify(tid int64, ntf kiwi.ISndNotice) ([]byte, *util.Err) { 201 var buffer util.ByteBuffer 202 buffer.InitCap(256) 203 buffer.WUint8(HdNotify) 204 buffer.WInt64(kiwi.GetNodeMeta().NodeId) 205 buffer.WInt64(tid) 206 err := buffer.WMAny(ntf.Head()) 207 if err != nil { 208 return nil, err 209 } 210 buffer.WBool(ntf.Json()) 211 _, e := buffer.Write(ntf.Payload()) 212 if e != nil { 213 return nil, util.NewErr(util.EcWriteFail, util.M{ 214 "error": e, 215 }) 216 } 217 return buffer.All(), nil 218 } 219 220 func (p *packer) UnpackNotify(bytes []byte, pkg kiwi.IRcvNotice) (err *util.Err) { 221 var buffer util.ByteBuffer 222 buffer.InitBytes(bytes) 223 buffer.SetPos(1) 224 tid, err := buffer.RInt64() 225 if err != nil { 226 return 227 } 228 head := make(util.M) 229 err = buffer.RMAny(head) 230 if err != nil { 231 return 232 } 233 json, err := buffer.RBool() 234 if err != nil { 235 return 236 } 237 bs := buffer.RAvailable() 238 return pkg.InitWithBytes(HdNotify, tid, head, json, bs) 239 } 240 241 func (p *packer) PackM(m util.M) ([]byte, *util.Err) { 242 var buffer util.ByteBuffer 243 buffer.InitCap(256) 244 err := buffer.WMAny(m) 245 if err != nil { 246 return nil, err 247 } 248 return buffer.All(), nil 249 } 250 251 func (p *packer) UnpackM(bytes []byte, m util.M) *util.Err { 252 var buffer util.ByteBuffer 253 buffer.InitBytes(bytes) 254 return buffer.RMAny(m) 255 }