github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/network/payload/ping.go (about)

     1  package payload
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/io"
     7  )
     8  
     9  // Ping payload for ping/pong payloads.
    10  type Ping struct {
    11  	// Index of the last block.
    12  	LastBlockIndex uint32
    13  	// Timestamp.
    14  	Timestamp uint32
    15  	// Nonce of the server.
    16  	Nonce uint32
    17  }
    18  
    19  // NewPing creates new Ping payload.
    20  func NewPing(blockIndex uint32, nonce uint32) *Ping {
    21  	return &Ping{
    22  		LastBlockIndex: blockIndex,
    23  		Timestamp:      uint32(time.Now().UTC().Unix()),
    24  		Nonce:          nonce,
    25  	}
    26  }
    27  
    28  // DecodeBinary implements the Serializable interface.
    29  func (p *Ping) DecodeBinary(br *io.BinReader) {
    30  	p.LastBlockIndex = br.ReadU32LE()
    31  	p.Timestamp = br.ReadU32LE()
    32  	p.Nonce = br.ReadU32LE()
    33  }
    34  
    35  // EncodeBinary implements the Serializable interface.
    36  func (p *Ping) EncodeBinary(bw *io.BinWriter) {
    37  	bw.WriteU32LE(p.LastBlockIndex)
    38  	bw.WriteU32LE(p.Timestamp)
    39  	bw.WriteU32LE(p.Nonce)
    40  }