forked from Open-Nyzo/go-nyzo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_response.go
50 lines (44 loc) · 1.16 KB
/
status_response.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
package message_content
import (
"github.com/cryptic-monk/go-nyzo/internal/nyzo/messages/message_content/message_fields"
"io"
)
type StatusResponse struct {
Lines []string
}
func NewStatusResponse(lines []string) *StatusResponse {
return &StatusResponse{lines}
}
// Serializable interface: data length when serialized
func (c *StatusResponse) GetSerializedLength() int {
size := message_fields.SizeUnnamedByte
for _, s := range c.Lines {
size += message_fields.SerializedStringLength(s, 1024)
}
return size
}
// Serializable interface: convert to bytes.
func (c *StatusResponse) ToBytes() []byte {
var serialized []byte
serialized = append(serialized, byte(len(c.Lines)))
for _, s := range c.Lines {
serialized = append(serialized, message_fields.SerializeString(s, 1024)...)
}
return serialized
}
// Serializable interface: convert from bytes.
func (c *StatusResponse) Read(r io.Reader) error {
count, err := message_fields.ReadByte(r)
if err != nil {
return err
}
c.Lines = make([]string, 0, count)
for i := 0; i < int(count); i++ {
s, err := message_fields.ReadString(r)
if err != nil {
return err
}
c.Lines = append(c.Lines, s)
}
return nil
}