]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Allocate peer_protocol.Message on the stack in connectionLoop
authorMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2014 07:48:44 +0000 (17:48 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2014 07:48:44 +0000 (17:48 +1000)
client.go

index 206cdd7c31268eb9add3743b9d08f72937b0f4c5..3ca57e10a8f3ae12446a871b723d0c13dd6f38cb 100644 (file)
--- a/client.go
+++ b/client.go
@@ -376,9 +376,8 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error {
        }
        for {
                me.mu.Unlock()
-               // TODO: Can this be allocated on the stack?
-               msg := new(peer_protocol.Message)
-               err := decoder.Decode(msg)
+               var msg peer_protocol.Message
+               err := decoder.Decode(&msg)
                me.mu.Lock()
                if err != nil {
                        if me.stopped() || err == io.EOF {
@@ -446,13 +445,7 @@ func (me *Client) connectionLoop(torrent *torrent, conn *connection) error {
                                }
                        }
                case peer_protocol.Piece:
-                       request_ := request{msg.Index, chunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}}
-                       if _, ok := conn.Requests[request_]; !ok {
-                               err = fmt.Errorf("unexpected piece: %s", request_)
-                               break
-                       }
-                       delete(conn.Requests, request_)
-                       err = me.downloadedChunk(torrent, msg)
+                       err = me.downloadedChunk(torrent, conn, &msg)
                default:
                        err = fmt.Errorf("received unknown message type: %#v", msg.Type)
                }