Disconnection reasons

pull/150/head
obscuren 11 years ago
parent f4a96ca588
commit aa9341570b
  1. 43
      peer.go

@ -17,27 +17,53 @@ const (
outputBufferSize = 50 outputBufferSize = 50
) )
// Peer capabillities type DiscReason byte
const (
DiscReRequested = 0x00
DiscReTcpSysErr = 0x01
DiscBadProto = 0x02
DiscBadPeer = 0x03
DiscTooManyPeers = 0x04
)
var discReasonToString = []string{
"Disconnect requested",
"Disconnect TCP sys error",
"Disconnect Bad protocol",
"Disconnect Useless peer",
"Disconnect Too many peers",
}
func (d DiscReason) String() string {
if len(discReasonToString) > int(d) {
return "Unknown"
}
return discReasonToString[d]
}
// Peer capabilities
type Caps byte type Caps byte
const ( const (
CapDiscoveryTy = 0x01 CapPeerDiscTy = 0x01
CapTxTy = 0x02 CapTxTy = 0x02
CapChainTy = 0x04 CapChainTy = 0x04
CapDefault = CapChainTy | CapTxTy | CapDiscoveryTy CapDefault = CapChainTy | CapTxTy | CapPeerDiscTy
) )
var capsToString = map[Caps]string{ var capsToString = map[Caps]string{
CapDiscoveryTy: "Peer discovery", CapPeerDiscTy: "Peer discovery",
CapTxTy: "Transaction relaying", CapTxTy: "Transaction relaying",
CapChainTy: "Block chain relaying", CapChainTy: "Block chain relaying",
} }
func (c Caps) String() string { func (c Caps) String() string {
var caps []string var caps []string
if c&CapDiscoveryTy > 0 { if c&CapPeerDiscTy > 0 {
caps = append(caps, capsToString[CapDiscoveryTy]) caps = append(caps, capsToString[CapPeerDiscTy])
} }
if c&CapChainTy > 0 { if c&CapChainTy > 0 {
caps = append(caps, capsToString[CapChainTy]) caps = append(caps, capsToString[CapChainTy])
@ -175,7 +201,7 @@ out:
// Service timer takes care of peer broadcasting, transaction // Service timer takes care of peer broadcasting, transaction
// posting or block posting // posting or block posting
case <-serviceTimer.C: case <-serviceTimer.C:
if p.caps&CapDiscoveryTy > 0 { if p.caps&CapPeerDiscTy > 0 {
msg := p.peersMessage() msg := p.peersMessage()
p.ethereum.BroadcastMsg(msg) p.ethereum.BroadcastMsg(msg)
} }
@ -220,6 +246,7 @@ out:
p.QueueMessage(ethwire.NewMessage(ethwire.MsgGetPeersTy, "")) p.QueueMessage(ethwire.NewMessage(ethwire.MsgGetPeersTy, ""))
case ethwire.MsgDiscTy: case ethwire.MsgDiscTy:
p.Stop() p.Stop()
log.Println("Disconnect peer:", DiscReason(msg.Data.Get(0).AsUint()))
case ethwire.MsgPingTy: case ethwire.MsgPingTy:
// Respond back with pong // Respond back with pong
p.QueueMessage(ethwire.NewMessage(ethwire.MsgPongTy, "")) p.QueueMessage(ethwire.NewMessage(ethwire.MsgPongTy, ""))
@ -381,8 +408,6 @@ func (p *Peer) Stop() {
p.writeMessage(ethwire.NewMessage(ethwire.MsgDiscTy, "")) p.writeMessage(ethwire.NewMessage(ethwire.MsgDiscTy, ""))
p.conn.Close() p.conn.Close()
} }
log.Println("Peer shutdown")
} }
func (p *Peer) pushHandshake() error { func (p *Peer) pushHandshake() error {

Loading…
Cancel
Save