// 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.).
}
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)
}
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)
}
--- /dev/null
+// 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) + ")"
+ }
+}
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
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