Exemple #1
0
// The first thing to do after a connection is established is to exchange version.
// For kaiju it goes as follows:
// 1. Send out my version message, if outgoing == true
// 2. Expect the remote peer send it's version message
// 3. Once the message from remote peer is received, send verack
// 4. Send out my version message, if outgoing == false
// 5. Done, knet.Peer starts to work
func (p *Peer) versionHankshake() error {
	// Step 1
	if p.outgoing {
		err := p.sendVersionMsg()
		if err != nil {
			return err
		}
	}
	// Step 2
	msg, err := btcmsg.ReadMsg(p.conn)
	if err != nil {
		return err
	} else {
		if ver, ok := msg.(*btcmsg.Message_version); ok {
			// TODO: more check
			p.info = ver.Addr_from
		} else {
			return errors.New("Wrong message type when doing versionHankshake")
		}
	}
	// Step 3
	vamsg := btcmsg.NewVerAckMsg()
	err = btcmsg.WriteMsg(p.conn, vamsg)
	if err != nil {
		return err
	}
	// Step 4
	if !p.outgoing {
		err := p.sendVersionMsg()
		if err != nil {
			return err
		}
	}
	return nil
}
Exemple #2
0
func (p *Peer) loopReceiveMsg() {
	for {
		msg, err := btcmsg.ReadMsg(p.conn)
		if err != nil {
			//log.Infof("loopReceiveMsg error: %s", err.Error())
			break
		} else {
			if !p.handleMessage(msg) {
				p.onPeerMsg(p.handle, msg)
			}
		}
	}
	p.cleanUp()
}