]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix the mess that is the connectionLoop after messages are decoded
authorMatt Joiner <anacrolix@gmail.com>
Thu, 4 Feb 2016 14:17:06 +0000 (01:17 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 4 Feb 2016 14:17:06 +0000 (01:17 +1100)
client.go

index 3cd3d6688a868a2814dcfa02397d3ca83f313bb8..34921bd7d4b213cf5b92d2c08d6863d1221eb6c6 100644 (file)
--- a/client.go
+++ b/client.go
@@ -69,6 +69,7 @@ var (
        // Number of completed connections to a client we're already connected with.
        duplicateClientConns       = expvar.NewInt("duplicateClientConns")
        receivedMessageTypes       = expvar.NewMap("receivedMessageTypes")
+       receivedKeepalives         = expvar.NewInt("receivedKeepalives")
        supportedExtensionMessages = expvar.NewMap("supportedExtensionMessages")
 )
 
@@ -1386,21 +1387,19 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error {
                me.mu.Unlock()
                var msg pp.Message
                err := decoder.Decode(&msg)
-               receivedMessageTypes.Add(strconv.FormatInt(int64(msg.Type), 10), 1)
                me.mu.Lock()
-               c.lastMessageReceived = time.Now()
-               if c.closed.IsSet() {
+               if me.stopped() || c.closed.IsSet() || err == io.EOF {
                        return nil
                }
                if err != nil {
-                       if me.stopped() || err == io.EOF {
-                               return nil
-                       }
                        return err
                }
+               c.lastMessageReceived = time.Now()
                if msg.Keepalive {
+                       receivedKeepalives.Add(1)
                        continue
                }
+               receivedMessageTypes.Add(strconv.FormatInt(int64(msg.Type), 10), 1)
                switch msg.Type {
                case pp.Choke:
                        c.PeerChoked = true