msgping.go raw

   1  // Copyright (c) 2013-2015 The btcsuite developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package wire
   6  
   7  import (
   8  	"io"
   9  )
  10  
  11  // MsgPing implements the Message interface and represents a bitcoin ping
  12  // message.
  13  //
  14  // For versions BIP0031Version and earlier, it is used primarily to confirm
  15  // that a connection is still valid.  A transmission error is typically
  16  // interpreted as a closed connection and that the peer should be removed.
  17  // For versions AFTER BIP0031Version it contains an identifier which can be
  18  // returned in the pong message to determine network timing.
  19  //
  20  // The payload for this message just consists of a nonce used for identifying
  21  // it later.
  22  type MsgPing struct {
  23  	// Unique value associated with message that is used to identify
  24  	// specific ping message.
  25  	Nonce uint64
  26  }
  27  
  28  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
  29  // This is part of the Message interface implementation.
  30  func (msg *MsgPing) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
  31  	// There was no nonce for BIP0031Version and earlier.
  32  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
  33  	// the version unlike most others.
  34  	if pver > BIP0031Version {
  35  		nonce, err := binarySerializer.Uint64(r, littleEndian)
  36  		if err != nil {
  37  			return err
  38  		}
  39  		msg.Nonce = nonce
  40  	}
  41  
  42  	return nil
  43  }
  44  
  45  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
  46  // This is part of the Message interface implementation.
  47  func (msg *MsgPing) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
  48  	// There was no nonce for BIP0031Version and earlier.
  49  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
  50  	// the version unlike most others.
  51  	if pver > BIP0031Version {
  52  		err := binarySerializer.PutUint64(w, littleEndian, msg.Nonce)
  53  		if err != nil {
  54  			return err
  55  		}
  56  	}
  57  
  58  	return nil
  59  }
  60  
  61  // Command returns the protocol command string for the message.  This is part
  62  // of the Message interface implementation.
  63  func (msg *MsgPing) Command() string {
  64  	return CmdPing
  65  }
  66  
  67  // MaxPayloadLength returns the maximum length the payload can be for the
  68  // receiver.  This is part of the Message interface implementation.
  69  func (msg *MsgPing) MaxPayloadLength(pver uint32) uint32 {
  70  	plen := uint32(0)
  71  	// There was no nonce for BIP0031Version and earlier.
  72  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
  73  	// the version unlike most others.
  74  	if pver > BIP0031Version {
  75  		// Nonce 8 bytes.
  76  		plen += 8
  77  	}
  78  
  79  	return plen
  80  }
  81  
  82  // NewMsgPing returns a new bitcoin ping message that conforms to the Message
  83  // interface.  See MsgPing for details.
  84  func NewMsgPing(nonce uint64) *MsgPing {
  85  	return &MsgPing{
  86  		Nonce: nonce,
  87  	}
  88  }
  89