generated from bep/golibtemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
67 lines (59 loc) · 1.45 KB
/
message.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
package execrpc
import (
"encoding/binary"
"io"
)
// Message is what gets sent to and from the server.
type Message struct {
Header Header
Body []byte
}
func (m *Message) Read(r io.Reader) error {
if err := m.Header.Read(r); err != nil {
return err
}
m.Body = make([]byte, m.Header.Size)
_, err := io.ReadFull(r, m.Body)
return err
}
func (m *Message) Write(w io.Writer) error {
m.Header.Size = uint32(len(m.Body))
if err := m.Header.Write(w); err != nil {
return err
}
_, err := w.Write(m.Body)
return err
}
// Header is the header of a message.
// ID and Size are set by the system.
// Status may be set by the system.
type Header struct {
ID uint32
Version uint16
Status uint16
Size uint32
}
const headerSize = 12
// Read reads the header from the reader.
func (h *Header) Read(r io.Reader) error {
buf := make([]byte, headerSize)
_, err := io.ReadFull(r, buf)
if err != nil {
return err
}
h.ID = binary.BigEndian.Uint32(buf[0:4])
h.Version = binary.BigEndian.Uint16(buf[4:6])
h.Status = binary.BigEndian.Uint16(buf[6:8])
h.Size = binary.BigEndian.Uint32(buf[8:])
return nil
}
// Write writes the header to the writer.
func (h Header) Write(w io.Writer) error {
buff := make([]byte, headerSize)
binary.BigEndian.PutUint32(buff[0:4], h.ID)
binary.BigEndian.PutUint16(buff[4:6], h.Version)
binary.BigEndian.PutUint16(buff[6:8], h.Status)
binary.BigEndian.PutUint32(buff[8:], h.Size)
_, err := w.Write(buff)
return err
}