forked from tolsen/mongonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire_update.go
83 lines (63 loc) · 1.56 KB
/
wire_update.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
package mongonet
func (m *UpdateMessage) HasResponse() bool {
return false
}
func (m *UpdateMessage) IsExhaust() bool {
return false
}
func (m *UpdateMessage) Header() MessageHeader {
return m.header
}
func (m *UpdateMessage) Serialize() []byte {
size := 16 /* header */ + 8 /* update header */
size += len(m.Namespace) + 1
size += int(m.Filter.Size)
size += int(m.Update.Size)
m.header.Size = int32(size)
buf := make([]byte, size)
m.header.WriteInto(buf)
loc := 16
writeInt32(0, buf, loc)
loc += 4
writeCString(m.Namespace, buf, &loc)
writeInt32(m.Flags, buf, loc)
loc += 4
m.Filter.Copy(&loc, buf)
m.Update.Copy(&loc, buf)
return buf
}
func parseUpdateMessage(header MessageHeader, buf []byte) (Message, error) {
m := &UpdateMessage{}
m.header = header
var err error
loc := 0
if len(buf) < 4 {
return m, NewStackErrorf("invalid update message -- message must have length of at least 4 bytes.")
}
m.Reserved = readInt32(buf[loc:])
loc += 4
m.Namespace, err = readCString(buf[loc:])
if err != nil {
return m, err
}
loc += len(m.Namespace) + 1
if len(buf) < (loc + 4) {
return m, NewStackErrorf("invalid update message -- message length is too short.")
}
m.Flags = readInt32(buf[loc:])
loc += 4
m.Filter, err = parseSimpleBSON(buf[loc:])
if err != nil {
return m, err
}
loc += int(m.Filter.Size)
if len(buf) < loc {
return m, NewStackErrorf("invalid update message -- message length is too short.")
}
m.Update, err = parseSimpleBSON(buf[loc:])
if err != nil {
return m, err
}
loc += int(m.Filter.Size)
return m, nil
}