<commit_before># -*- encoding: utf-8 -*-

require 'socket'
require 'timeout'
require 'io/wait'
require 'digest/sha1'

module Stomp

  class Connection

      private

      # Really read from the wire.
      def _receive(read_socket, connread = false)
        # p [ "ioscheck", @iosto, connread ]
        # _dump_callstack()
        # drdbg = true
        drdbg = false
        @read_semaphore.synchronize do
          line = nil
          if connread
            begin
              Timeout::timeout(@connread_timeout, Stomp::Error::ConnectReadTimeout) do
                line = _init_line_read(read_socket)
              end
            rescue Stomp::Error::ConnectReadTimeout => ex
              if @reliable
                _reconn_prep()
              end
              raise ex
            end
          else
            p [ "CONR01" ] if drdbg
            _dump_callstack() if drdbg
            line = _init_line_read(read_socket)
          end
          #
          p [ "nilcheck", line.nil? ] if drdbg
          return nil if line.nil?
          #An extra \n at the beginning of the frame, possibly not caught by is_ready?
          line = '' if line == "\n"
          p [ "wiredatain_01A", line, Time.now ] if drdbg
          line = _normalize_line_end(line) if @protocol >= Stomp::SPL_12
          p [ "wiredatain_01B", line, Time.now ] if drdbg
          # Reads the beginning of the message until it runs into a empty line
          message_header = ''
          begin
            message_header += line
            p [ "wiredatain_02A", line, Time.now ] if drdbg
            unless connread
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "wiredatain_02B", line, Time.now ] if drdbg
            line = read_socket.gets
            p [ "wiredatain_02C", line ] if drdbg
            raise  if line.nil?
            line = _normalize_line_end(line) if @protocol >= Stomp::SPL_12
            p [ "wiredatain_02D", line ] if drdbg
          end until line =~ /^\s?\n$/
          p [ "wiredatain_03A" ] if drdbg
          # Checks if it includes content_length header
          content_length = message_header.match(/content-length\s?:\s?(\d+)\s?\n/)
          message_body = ''

          p [ "wiredatain_03B", content_length ] if drdbg
          # If content_length is present, read the specified amount of bytes
          if content_length
            unless connread
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "CL01" ] if drdbg
            message_body = read_socket.read content_length[1].to_i
            unless connread
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            raise Stomp::Error::InvalidMessageLength unless parse_char(read_socket.getc) == "\0"
            # Else read the rest of the message until the first \0
          else
            unless connread
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "NOCL01" ] if drdbg
            message_body = read_socket.readline("\0")
            message_body.chop!
          end

          p [ "wiredatain_04" ] if drdbg
          # If the buffer isn't empty, reads trailing new lines.
          #
          # Note: experiments with JRuby seem to show that socket.ready? never
          # returns true.  It appears that in cases where Ruby returns true
          # that JRuby returns a Fixnum.  We attempt to adjust for this
          # in the _is_ready? method.
          #
          # Note 2: the draining of new lines must be done _after_ a message
          # is read.  Do _not_ leave them on the wire and attempt to drain them
          # at the start of the next read.  Attempting to do that breaks the
          # asynchronous nature of the 'poll' method.
          p [ "wiredatain_05_prep", "isr", _is_ready?(read_socket) ] if drdbg
          while _is_ready?(read_socket)
            unless connread
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "WHIR01" ] if drdbg
            last_char = read_socket.getc
            break unless last_char
            if parse_char(last_char) != "\n"
              read_socket.ungetc(last_char)
              break
            end
          end
          p [ "wiredatain_05A" ] if drdbg
          if @protocol >= Stomp::SPL_11<commit_msg>Issue 139. Add ssl checks to receive timeout logic.<commit_after># -*- encoding: utf-8 -*-

require 'socket'
require 'timeout'
require 'io/wait'
require 'digest/sha1'

module Stomp

  class Connection

      private

      # Really read from the wire.
      def _receive(read_socket, connread = false)
        # p [ "ioscheck", @iosto, connread ]
        # _dump_callstack()
        # drdbg = true
        drdbg = false
        @read_semaphore.synchronize do
          line = nil
          if connread
            begin
              Timeout::timeout(@connread_timeout, Stomp::Error::ConnectReadTimeout) do
                line = _init_line_read(read_socket)
              end
            rescue Stomp::Error::ConnectReadTimeout => ex
              if @reliable
                _reconn_prep()
              end
              raise ex
            end
          else
            p [ "CONR01" ] if drdbg
            _dump_callstack() if drdbg
            line = _init_line_read(read_socket)
          end
          #
          p [ "nilcheck", line.nil? ] if drdbg
          return nil if line.nil?
          #An extra \n at the beginning of the frame, possibly not caught by is_ready?
          line = '' if line == "\n"
          p [ "wiredatain_01A", line, Time.now ] if drdbg
          line = _normalize_line_end(line) if @protocol >= Stomp::SPL_12
          p [ "wiredatain_01B", line, Time.now ] if drdbg
          # Reads the beginning of the message until it runs into a empty line
          message_header = ''
          begin
            message_header += line
            p [ "wiredatain_02A", line, Time.now ] if drdbg
            unless connread || @ssl
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "wiredatain_02B", line, Time.now ] if drdbg
            line = read_socket.gets
            p [ "wiredatain_02C", line ] if drdbg
            raise  if line.nil?
            line = _normalize_line_end(line) if @protocol >= Stomp::SPL_12
            p [ "wiredatain_02D", line ] if drdbg
          end until line =~ /^\s?\n$/
          p [ "wiredatain_03A" ] if drdbg
          # Checks if it includes content_length header
          content_length = message_header.match(/content-length\s?:\s?(\d+)\s?\n/)
          message_body = ''

          p [ "wiredatain_03B", content_length ] if drdbg
          # If content_length is present, read the specified amount of bytes
          if content_length
            unless connread || @ssl
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "CL01" ] if drdbg
            message_body = read_socket.read content_length[1].to_i
            unless connread || @ssl
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            raise Stomp::Error::InvalidMessageLength unless parse_char(read_socket.getc) == "\0"
            # Else read the rest of the message until the first \0
          else
            unless connread || @ssl
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "NOCL01" ] if drdbg
            message_body = read_socket.readline("\0")
            message_body.chop!
          end

          p [ "wiredatain_04" ] if drdbg
          # If the buffer isn't empty, reads trailing new lines.
          #
          # Note: experiments with JRuby seem to show that socket.ready? never
          # returns true.  It appears that in cases where Ruby returns true
          # that JRuby returns a Fixnum.  We attempt to adjust for this
          # in the _is_ready? method.
          #
          # Note 2: the draining of new lines must be done _after_ a message
          # is read.  Do _not_ leave them on the wire and attempt to drain them
          # at the start of the next read.  Attempting to do that breaks the
          # asynchronous nature of the 'poll' method.
          p [ "wiredatain_05_prep", "isr", _is_ready?(read_socket) ] if drdbg
          while _is_ready?(read_socket)
            unless connread || @ssl
              raise Stomp::Error::ReceiveTimeout unless IO.select([read_socket], nil, nil, @iosto)
            end
            p [ "WHIR01" ] if drdbg
            last_char = read_socket.getc
            break unless last_char
            if parse_char(last_char) != "\n"
              read_socket.ungetc(last_char)
              break
            end
          end
          p [ "wiredatain_05A" ] if drdbg
          if @protocol >= Stomp::SPL_11
{'fraction_non_alphanumeric': 0.08347807207989578, 'fraction_numerical': 0.009661311333043855, 'mean_word_length': 1.7476886370414555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4590281', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2589, 'n_words': 818}