forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.go
57 lines (44 loc) · 1.09 KB
/
addr.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
// Copyright (c) 2017-2019 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//
package msg
import (
"io"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/p2p"
)
const (
// maxHostLength defines the maximum host name length.
maxHostLength = 253
)
// Ensure Ping implement p2p.Message interface.
var _ p2p.Message = (*Addr)(nil)
// Addr represents a DPoS network address for connect to a peer.
type Addr struct {
Host string
Port uint16
}
func NewAddr(host string, port uint16) *Addr {
return &Addr{Host: host, Port: port}
}
func (msg *Addr) CMD() string {
return p2p.CmdAddr
}
func (msg *Addr) MaxLength() uint32 {
return maxHostLength + 2
}
func (msg *Addr) Serialize(w io.Writer) error {
if err := common.WriteVarString(w, msg.Host); err != nil {
return err
}
return common.WriteElement(w, msg.Port)
}
func (msg *Addr) Deserialize(r io.Reader) error {
var err error
if msg.Host, err = common.ReadVarString(r); err != nil {
return err
}
msg.Port, err = common.ReadUint16(r)
return err
}