neo-go/pkg/network/payload/ping.go
Vsevolod Brekelov 4e6ed9021c network: add ping pong processing
add pingInterval same as used in ref C# implementation with the same logic
add pingTimeout which is used to check whether pong received. If not -- drop the peer.
add pingLimit which is hardcoded to 4 in TCPPeer. It's limit for unsuccessful ping/pong calls (where pong wasn't received in pingTimeout interval)
2020-01-17 13:24:14 +03:00

40 lines
874 B
Go

package payload
import (
"time"
"github.com/CityOfZion/neo-go/pkg/io"
)
// Ping payload for ping/pong payloads.
type Ping struct {
// Index of the last block.
LastBlockIndex uint32
// Timestamp.
Timestamp uint32
// Nonce of the server.
Nonce uint32
}
// NewPing creates new Ping payload.
func NewPing(blockIndex uint32, nonce uint32) *Ping {
return &Ping{
LastBlockIndex: blockIndex,
Timestamp: uint32(time.Now().UTC().Unix()),
Nonce: nonce,
}
}
// DecodeBinary implements Serializable interface.
func (p *Ping) DecodeBinary(br *io.BinReader) {
p.LastBlockIndex = br.ReadU32LE()
p.Timestamp = br.ReadU32LE()
p.Nonce = br.ReadU32LE()
}
// EncodeBinary implements Serializable interface.
func (p *Ping) EncodeBinary(bw *io.BinWriter) {
bw.WriteU32LE(p.LastBlockIndex)
bw.WriteU32LE(p.Timestamp)
bw.WriteU32LE(p.Nonce)
}