From 93e8d9bfaae837efc8fe378f7654431503e10e59 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 5 Feb 2018 15:30:35 +1100 Subject: [PATCH] Use stringer to generate peer_protocol.MessageType strings --- connection.go | 6 +++--- peer_protocol/messagetype_string.go | 25 +++++++++++++++++++++++++ peer_protocol/protocol.go | 22 +++++++--------------- 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 peer_protocol/messagetype_string.go diff --git a/connection.go b/connection.go index 16888aa0..947adc5f 100644 --- a/connection.go +++ b/connection.go @@ -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) } diff --git a/peer_protocol/messagetype_string.go b/peer_protocol/messagetype_string.go new file mode 100644 index 00000000..a098b5c3 --- /dev/null +++ b/peer_protocol/messagetype_string.go @@ -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) + ")" + } +} diff --git a/peer_protocol/protocol.go b/peer_protocol/protocol.go index aa53ffdf..67aa5a61 100644 --- a/peer_protocol/protocol.go +++ b/peer_protocol/protocol.go @@ -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,11 +25,11 @@ const ( Port // 9 // BEP 6 - Suggest = 0xd // 13 - HaveAll = 0xe // 14 - HaveNone = 0xf // 15 - Reject = 0x10 // 16 - AllowedFast = 0x11 // 17 + Suggest MessageType = iota + 0xd // 13 + HaveAll = 0xe // 14 + HaveNone = 0xf // 15 + Reject = 0x10 // 16 + AllowedFast = 0x11 // 17 Extended = 20 -- 2.50.0