msgping.go raw

   1  package wire
   2  
   3  import (
   4  	"io"
   5  )
   6  
   7  // MsgPing implements the Message interface and represents a bitcoin ping message. For versions BIP0031Version and
   8  // earlier, it is used primarily to confirm that a connection is still valid. A transmission error is typically
   9  // interpreted as a closed connection and that the peer should be removed. For versions AFTER BIP0031Version it contains
  10  // an identifier which can be returned in the pong message to determine network timing. The payload for this message
  11  // just consists of a nonce used for identifying it later.
  12  type MsgPing struct {
  13  	// Unique value associated with message that is used to identify specific ping message.
  14  	Nonce uint64
  15  }
  16  
  17  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver. This is part of the Message interface
  18  // implementation.
  19  func (msg *MsgPing) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) (e error) {
  20  	// There was no nonce for BIP0031Version and earlier. NOTE: > is not a mistake here. The BIP0031 was defined as
  21  	// AFTER the version unlike most others.
  22  	if pver > BIP0031Version {
  23  		if e = readElement(r, &msg.Nonce); E.Chk(e) {
  24  			return
  25  		}
  26  	}
  27  	return
  28  }
  29  
  30  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding. This is part of the Message interface
  31  // implementation.
  32  func (msg *MsgPing) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) (e error) {
  33  	// There was no nonce for BIP0031Version and earlier. NOTE: > is not a mistake here. The BIP0031 was defined as
  34  	// AFTER the version unlike most others.
  35  	if pver > BIP0031Version {
  36  		if e = writeElement(w, msg.Nonce); E.Chk(e) {
  37  			return
  38  		}
  39  	}
  40  	return
  41  }
  42  
  43  // Command returns the protocol command string for the message. This is part of the Message interface implementation.
  44  func (msg *MsgPing) Command() string {
  45  	return CmdPing
  46  }
  47  
  48  // MaxPayloadLength returns the maximum length the payload can be for the receiver. This is part of the Message
  49  // interface implementation.
  50  func (msg *MsgPing) MaxPayloadLength(pver uint32) uint32 {
  51  	plen := uint32(0)
  52  	// There was no nonce for BIP0031Version and earlier. NOTE: > is not a mistake here. The BIP0031 was defined as
  53  	// AFTER the version unlike most others.
  54  	if pver > BIP0031Version {
  55  		// Nonce 8 bytes.
  56  		plen += 8
  57  	}
  58  	return plen
  59  }
  60  
  61  // NewMsgPing returns a new bitcoin ping message that conforms to the Message interface.  See MsgPing for details.
  62  func NewMsgPing(nonce uint64) *MsgPing {
  63  	return &MsgPing{
  64  		Nonce: nonce,
  65  	}
  66  }
  67