This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
msg.go
97 lines (85 loc) · 2.48 KB
/
msg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package rony
import (
"fmt"
"github.com/ronaksoft/rony/pools"
"google.golang.org/protobuf/proto"
)
/*
Creation Time: 2020 - Jan - 27
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
//go:generate protoc -I=. --go_out=paths=source_relative:. imsg.proto msg.proto options.proto
//go:generate protoc -I=. --gorony_out=paths=source_relative:. imsg.proto msg.proto
func init() {}
func ErrorMessage(out *MessageEnvelope, reqID uint64, errCode, errItem string) {
errMessage := PoolError.Get()
errMessage.Code = errCode
errMessage.Items = errItem
out.Fill(reqID, C_Error, errMessage)
PoolError.Put(errMessage)
return
}
func (x *MessageEnvelope) Clone() *MessageEnvelope {
c := PoolMessageEnvelope.Get()
c.Constructor = x.Constructor
c.RequestID = x.RequestID
c.Message = append(c.Message[:0], x.Message...)
c.Auth = append(c.Auth[:0], x.Auth...)
if cap(c.Header) >= len(x.Header) {
c.Header = c.Header[:len(x.Header)]
} else {
c.Header = make([]*KeyValue, len(x.Header))
}
for idx, kv := range x.Header {
if c.Header[idx] == nil {
c.Header[idx] = &KeyValue{}
}
kv.DeepCopy(c.Header[idx])
}
return c
}
func (x *MessageEnvelope) Fill(reqID uint64, constructor int64, p proto.Message, kvs ...*KeyValue) {
x.RequestID = reqID
x.Constructor = constructor
x.Header = append(x.Header[:0], kvs...)
mo := proto.MarshalOptions{
UseCachedSize: true,
}
buf := pools.Buffer.GetCap(mo.Size(p))
b, _ := mo.MarshalAppend(*buf.Bytes(), p)
x.Message = append(x.Message[:0], b...)
pools.Buffer.Put(buf)
}
func (x *MessageEnvelope) Get(key, defaultVal string) string {
for _, kv := range x.Header {
if kv.Key == key {
return kv.Value
}
}
return defaultVal
}
func (x *MessageEnvelope) Set(KVs ...*KeyValue) {
x.Header = append(x.Header[:0], KVs...)
}
func (x *RaftCommand) Fill(senderID []byte, e *MessageEnvelope, kvs ...*KeyValue) {
x.Sender = append(x.Sender[:0], senderID...)
x.Store = append(x.Store[:0], kvs...)
e.DeepCopy(x.Envelope)
}
func (x *TunnelMessage) Fill(senderID []byte, senderReplicaSet uint64, e *MessageEnvelope, kvs ...*KeyValue) {
x.SenderID = append(x.SenderID[:0], senderID...)
x.SenderReplicaSet = senderReplicaSet
x.Store = append(x.Store[:0], kvs...)
e.DeepCopy(x.Envelope)
}
func (x *Error) Error() string {
if len(x.Description) > 0 {
return fmt.Sprintf("%s:%s (%s)", x.Code, x.Items, x.Description)
} else {
return fmt.Sprintf("%s:%s", x.Code, x.Items)
}
}