]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use stringer to generate peer_protocol.MessageType strings
authorMatt Joiner <anacrolix@gmail.com>
Mon, 5 Feb 2018 04:30:35 +0000 (15:30 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 5 Feb 2018 04:30:35 +0000 (15:30 +1100)
connection.go
peer_protocol/messagetype_string.go [new file with mode: 0644]
peer_protocol/protocol.go

index 16888aa0ca104f96721bca889ffb2b319aededf8..947adc5ffbbbb37a4dbdb96a77157e25cf2ca0d3 100644 (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)
                }
diff --git a/peer_protocol/messagetype_string.go b/peer_protocol/messagetype_string.go
new file mode 100644 (file)
index 0000000..a098b5c
--- /dev/null
@@ -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) + ")"
+       }
+}
index aa53ffdffdc9ab60a20155265ca2dae9c829a1a3..67aa5a619f734be64b3cfc48309c05ae12d95da6 100644 (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,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