-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmsg_ping.go
More file actions
42 lines (33 loc) · 803 Bytes
/
msg_ping.go
File metadata and controls
42 lines (33 loc) · 803 Bytes
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
package gofast
import "encoding/binary"
// pingMsg is predefined message to ping-pong with remote.
type pingMsg struct {
echo string
}
func newPing(echo string) *pingMsg {
return &pingMsg{echo: echo}
}
func (msg *pingMsg) ID() uint64 {
return msgPing
}
func (msg *pingMsg) Encode(out []byte) []byte {
out = fixbuffer(out, msg.Size())
binary.BigEndian.PutUint16(out, uint16(len(msg.echo)))
n := 2
n += copy(out[n:], msg.echo)
return out[:n]
}
func (msg *pingMsg) Decode(in []byte) int64 {
ln, n := int64(binary.BigEndian.Uint16(in)), int64(2)
msg.echo, n = string(in[n:n+ln]), n+ln
return n
}
func (msg *pingMsg) Size() int64 {
return 2 + int64(len(msg.echo))
}
func (msg *pingMsg) String() string {
return "pingMsg"
}
func (msg *pingMsg) Repr() string {
return string(msg.echo)
}