-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnetgo_header.go
54 lines (43 loc) · 1.02 KB
/
netgo_header.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
// Copyright (C) 2018 Kun Zhong All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package netgo
import(
"io"
"encoding/binary"
)
const (
netpackMagic = 0xabcdabcd
)
type netPackHead struct {
magic uint32
bodyLen uint32
target uint64
}
func (pack *netPackHead) Valid() bool {
return pack.magic == netpackMagic
}
func decodeHeader(reader io.Reader) (header*netPackHead, err error) {
header = &netPackHead{}
header.magic, err = getUint32(reader, binary.BigEndian)
if err != nil {
return
}
header.bodyLen, err = getUint32(reader, binary.BigEndian)
if err != nil {
return
}
header.target, err = getUint64(reader, binary.BigEndian)
return
}
func encoderHeader(writer io.Writer, header *netPackHead) (err error) {
err = putUint32(header.magic, writer, binary.BigEndian)
if err != nil {
return
}
err = putUint32(header.bodyLen, writer, binary.BigEndian)
if err != nil {
return
}
return putUint64(header.target, writer, binary.BigEndian)
}