Use stringer to generate peer_protocol.MessageType strings

This commit is contained in:
Matt Joiner 2018-02-05 15:30:35 +11:00
parent 20316e5af4
commit 93e8d9bfaa
3 changed files with 35 additions and 18 deletions

View File

@ -252,7 +252,7 @@ func (cn *connection) PeerHasPiece(piece int) bool {
// Writes a message into the write buffer.
func (cn *connection) Post(msg pp.Message) {
messageTypesPosted.Add(strconv.FormatInt(int64(msg.Type), 10), 1)
messageTypesPosted.Add(msg.Type.String(), 1)
// We don't need to track bytes here because a connection.w Writer wrapper
// takes care of that (although there's some delay between us recording
// the message, and the connection writer flushing it out.).
@ -808,7 +808,7 @@ func (c *connection) requestPendingMetadata() {
}
func (cn *connection) wroteMsg(msg *pp.Message) {
messageTypesSent.Add(strconv.FormatInt(int64(msg.Type), 10), 1)
messageTypesSent.Add(msg.Type.String(), 1)
cn.stats.wroteMsg(msg)
cn.t.stats.wroteMsg(msg)
}
@ -945,7 +945,7 @@ func (c *connection) mainReadLoop() error {
receivedKeepalives.Add(1)
continue
}
messageTypesReceived.Add(strconv.FormatInt(int64(msg.Type), 10), 1)
messageTypesReceived.Add(msg.Type.String(), 1)
if msg.Type.FastExtension() && !c.fastEnabled() {
return fmt.Errorf("received fast extension message (type=%v) but extension is disabled", msg.Type)
}

View File

@ -0,0 +1,25 @@
// Code generated by "stringer -type=MessageType"; DO NOT EDIT.
package peer_protocol
import "strconv"
const (
_MessageType_name_0 = "ChokeUnchokeInterestedNotInterestedHaveBitfieldRequestPieceCancelPort"
_MessageType_name_1 = "Suggest"
)
var (
_MessageType_index_0 = [...]uint8{0, 5, 12, 22, 35, 39, 47, 54, 59, 65, 69}
)
func (i MessageType) String() string {
switch {
case 0 <= i && i <= 9:
return _MessageType_name_0[_MessageType_index_0[i]:_MessageType_index_0[i+1]]
case i == 23:
return _MessageType_name_1
default:
return "MessageType(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

View File

@ -1,20 +1,12 @@
package peer_protocol
import "strconv"
const (
Protocol = "\x13BitTorrent protocol"
)
type (
MessageType byte
)
type MessageType byte
// Hopefully uncaught panics format using this so we don't just see a pair of
// unhelpful uintptrs.
func (me MessageType) String() string {
return strconv.FormatInt(int64(me), 10)
}
//go:generate stringer -type=MessageType
func (mt MessageType) FastExtension() bool {
return mt >= Suggest && mt <= AllowedFast
@ -33,7 +25,7 @@ const (
Port // 9
// BEP 6
Suggest = 0xd // 13
Suggest MessageType = iota + 0xd // 13
HaveAll = 0xe // 14
HaveNone = 0xf // 15
Reject = 0x10 // 16