msgpong.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  	"fmt"
   9  	"io"
  10  )
  11  
  12  // MsgPong implements the Message interface and represents a bitcoin pong
  13  // message which is used primarily to confirm that a connection is still valid
  14  // in response to a bitcoin ping message (MsgPing).
  15  //
  16  // This message was not added until protocol versions AFTER BIP0031Version.
  17  type MsgPong struct {
  18  	// Unique value associated with message that is used to identify
  19  	// specific ping message.
  20  	Nonce uint64
  21  }
  22  
  23  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
  24  // This is part of the Message interface implementation.
  25  func (msg *MsgPong) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
  26  	// NOTE: <= is not a mistake here.  The BIP0031 was defined as AFTER
  27  	// the version unlike most others.
  28  	if pver <= BIP0031Version {
  29  		str := fmt.Sprintf("pong message invalid for protocol "+
  30  			"version %d", pver)
  31  		return messageError("MsgPong.BtcDecode", str)
  32  	}
  33  
  34  	nonce, err := binarySerializer.Uint64(r, littleEndian)
  35  	if err != nil {
  36  		return err
  37  	}
  38  	msg.Nonce = nonce
  39  
  40  	return nil
  41  }
  42  
  43  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
  44  // This is part of the Message interface implementation.
  45  func (msg *MsgPong) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
  46  	// NOTE: <= is not a mistake here.  The BIP0031 was defined as AFTER
  47  	// the version unlike most others.
  48  	if pver <= BIP0031Version {
  49  		str := fmt.Sprintf("pong message invalid for protocol "+
  50  			"version %d", pver)
  51  		return messageError("MsgPong.BtcEncode", str)
  52  	}
  53  
  54  	return binarySerializer.PutUint64(w, littleEndian, msg.Nonce)
  55  }
  56  
  57  // Command returns the protocol command string for the message.  This is part
  58  // of the Message interface implementation.
  59  func (msg *MsgPong) Command() string {
  60  	return CmdPong
  61  }
  62  
  63  // MaxPayloadLength returns the maximum length the payload can be for the
  64  // receiver.  This is part of the Message interface implementation.
  65  func (msg *MsgPong) MaxPayloadLength(pver uint32) uint32 {
  66  	plen := uint32(0)
  67  	// The pong message did not exist for BIP0031Version and earlier.
  68  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
  69  	// the version unlike most others.
  70  	if pver > BIP0031Version {
  71  		// Nonce 8 bytes.
  72  		plen += 8
  73  	}
  74  
  75  	return plen
  76  }
  77  
  78  // NewMsgPong returns a new bitcoin pong message that conforms to the Message
  79  // interface.  See MsgPong for details.
  80  func NewMsgPong(nonce uint64) *MsgPong {
  81  	return &MsgPong{
  82  		Nonce: nonce,
  83  	}
  84  }
  85