def recvData(peer)
      msg = nil

      @logger.debug "Got data from peer #{peer}"

      if peer.state == :handshaking
        # Read handshake message
        begin
          @logger.debug "Reading handshake from #{peer}"
          msg = PeerHandshake.unserializeFrom currentIo
        rescue
          @logger.warn "Peer #{peer} failed handshake: #{$!}"
          setPeerDisconnected(peer)
          close
          return
        end
      else
        begin
          @logger.debug "Reading wire-message from #{peer}"
          msg = peer.peerMsgSerializer.unserializeFrom currentIo
          #msg = PeerWireMessage.unserializeFrom currentIo
        rescue EOFError
          @logger.info "Peer #{peer} disconnected."
          setPeerDisconnected(peer)
          close
          return
        rescue
          @logger.warn "Unserializing message from peer #{peer} failed: #{$!}"
          @logger.warn $!.backtrace.join "\n"
          setPeerDisconnected(peer)
          close
          return
        end

        peer.updateUploadRate msg
        torrentData = @torrentData[peer.infoHash]
        torrentData.bytesDownloaded += msg.length if torrentData
        @logger.debug "Peer #{peer} upload rate: #{peer.uploadRate.value}  data only: #{peer.uploadRateDataOnly.value}"
      end


      if msg.is_a? PeerHandshake
        # This is a remote peer that we connected to returning our handshake.
        processHandshake(msg, peer)
        peer.state = :established
        peer.amChoked = true
        peer.peerChoked = true
        peer.amInterested = false
        peer.peerInterested = false
      elsif msg.is_a? BitfieldMessage
        @logger.debug "Received bitfield message from peer."
        handleBitfield(msg, peer)
      elsif msg.is_a? Unchoke
        @logger.debug "Received unchoke message from peer."
        peer.amChoked = false
      elsif msg.is_a? Choke
        @logger.debug "Received choke message from peer."
        peer.amChoked = true
      elsif msg.is_a? Interested
        @logger.debug "Received interested message from peer."
        peer.peerInterested = true
      elsif msg.is_a? Uninterested
        @logger.debug "Received uninterested message from peer."
        peer.peerInterested = false
      elsif msg.is_a? Piece
        @logger.debug "Received piece message from peer for torrent #{QuartzTorrent.bytesToHex(peer.infoHash)}: piece #{msg.pieceIndex} offset #{msg.blockOffset} length #{msg.data.length}."
        handlePieceReceive(msg, peer)
      elsif msg.is_a? Request
        @logger.debug "Received request message from peer for torrent #{QuartzTorrent.bytesToHex(peer.infoHash)}: piece #{msg.pieceIndex} offset #{msg.blockOffset} length #{msg.blockLength}."
        handleRequest(msg, peer)
      elsif msg.is_a? Have
        @logger.debug "Received have message from peer for torrent #{QuartzTorrent.bytesToHex(peer.infoHash)}: piece #{msg.pieceIndex}"
        handleHave(msg, peer)
      elsif msg.is_a? KeepAlive
        @logger.debug "Received keep alive message from peer."
      elsif msg.is_a? ExtendedHandshake
        @logger.debug "Received extended handshake message from peer."
        handleExtendedHandshake(msg, peer)
      elsif msg.is_a? ExtendedMetaInfo
        @logger.debug "Received extended metainfo message from peer."
        handleExtendedMetainfo(msg, peer)
      else
        @logger.warn "Received a #{msg.class} message but handler is not implemented"
      end
    end
def recvData(peer)
      msg = nil

      @logger.debug "Got data from peer #{peer}"

      if peer.state == :handshaking
        # Read handshake message
        begin
          @logger.debug "Reading handshake from #{peer}"
          msg = PeerHandshake.unserializeFrom currentIo
        rescue
          @logger.warn "Peer #{peer} failed handshake: #{$!}"
          setPeerDisconnected(peer)
          close
          return
        end
      else
        begin
          @logger.debug "Reading wire-message from #{peer}"
          msg = peer.peerMsgSerializer.unserializeFrom currentIo
          #msg = PeerWireMessage.unserializeFrom currentIo
        rescue EOFError
          @logger.info "Peer #{peer} disconnected."
          setPeerDisconnected(peer)
          close
          return
        rescue
          @logger.warn "Unserializing message from peer #{peer} failed: #{$!}"
          @logger.warn $!.backtrace.join "\n"
          setPeerDisconnected(peer)
          close
          return
        end

        peer.updateUploadRate msg
        torrentData = @torrentData[peer.infoHash]
        torrentData.bytesDownloaded += msg.length if torrentData
        @logger.debug "Peer #{peer} upload rate: #{peer.uploadRate.value}  data only: #{peer.uploadRateDataOnly.value}"
      end


      if msg.is_a? PeerHandshake
        # This is a remote peer that we connected to returning our handshake.
        processHandshake(msg, peer)
        peer.state = :established
        peer.amChoked = true
        peer.peerChoked = true
        peer.amInterested = false
        peer.peerInterested = false
      elsif msg.is_a? BitfieldMessage
        @logger.debug "Received bitfield message from peer."
        handleBitfield(msg, peer)
      elsif msg.is_a? Unchoke
        @logger.debug "Received unchoke message from peer."
        peer.amChoked = false
      elsif msg.is_a? Choke
        @logger.debug "Received choke message from peer."
        peer.amChoked = true
      elsif msg.is_a? Interested
        @logger.debug "Received interested message from peer."
        peer.peerInterested = true
      elsif msg.is_a? Uninterested
        @logger.debug "Received uninterested message from peer."
        peer.peerInterested = false
      elsif msg.is_a? Piece
        @logger.debug "Received piece message from peer for torrent #{QuartzTorrent.bytesToHex(peer.infoHash)}: piece #{msg.pieceIndex} offset #{msg.blockOffset} length #{msg.data.length}."
        handlePieceReceive(msg, peer)
      elsif msg.is_a? Request
        @logger.debug "Received request message from peer for torrent #{QuartzTorrent.bytesToHex(peer.infoHash)}: piece #{msg.pieceIndex} offset #{msg.blockOffset} length #{msg.blockLength}."
        handleRequest(msg, peer)
      elsif msg.is_a? Have
        @logger.debug "Received have message from peer for torrent #{QuartzTorrent.bytesToHex(peer.infoHash)}: piece #{msg.pieceIndex}"
        handleHave(msg, peer)
      elsif msg.is_a? KeepAlive
        @logger.debug "Received keep alive message from peer."
      elsif msg.is_a? ExtendedHandshake
        @logger.debug "Received extended handshake message from peer."
        handleExtendedHandshake(msg, peer)
      elsif msg.is_a? ExtendedMetaInfo
        @logger.debug "Received extended metainfo message from peer."
        handleExtendedMetainfo(msg, peer)
      else
        @logger.warn "Received a #{msg.class} message but handler is not implemented"
      end
    end
Reactor method called when there is data ready to be read from a socket