Beispiel #1
0
func (ms *monitors) onPeerMsg(handle Handle, msg btcmsg.Message) {
	ms.mutex.RLock()
	defer ms.mutex.RUnlock()

	m, ok := ms.msgReceivers[msg.Command()]
	if ok {
		m.OnPeerMsg(handle, msg)
	}
}
Beispiel #2
0
// Some of the messages are handled here instead of being sent to upper level
// Returns true if we don't want send this message to upper level
func (p *Peer) handleMessage(msg btcmsg.Message) bool {
	// Update timers
	p.keepAliveTimer.Reset(timeToPing)
	p.kickTimer.Reset(timeToKick)
	// Handle msg
	switch msg.Command() {
	case "ping":
		ping := msg.(*btcmsg.Message_ping)
		pong := btcmsg.NewPongMsg().(*btcmsg.Message_pong)
		pong.Nonce = ping.Nonce
		p.sendMsg(pong, 0, nil)
		return true
	case "pong":
		pong := msg.(*btcmsg.Message_pong)
		if pong.Nonce != uint64(p.handle) {
			//    log.Infof("Bad pong nonce from: %d!=%d", p.handle, pong.Nonce)
		}
		return true
	default:
		p.expMutex.Lock()
		defer p.expMutex.Unlock()
		exps := p.expectors
		for i, e := range exps {
			if accept, stop := e.filter(msg); accept {
				e.retChan <- struct {
					btcmsg.Message
					Error error
				}{msg, nil}
				// Delete the expector
				if stop {
					close(e.done)
					p.expectors = append(exps[:i], exps[i+1:]...)
				}
				return true
			}
		}
	}
	return false
}