<commit_before>#--
# Copyright (C)2007 <NAME>
# You can redistribute this under the terms of the Ruby license
# See file LICENSE for details
#++

require 'socket'
require 'resolv'

module Rev
  class Socket < IO    
    def self.connect(socket, *args)
      new(socket, *args).instance_eval do
        @_connector = Connector.new(self, socket)
        self
      end
    end
    
    watcher_delegate :@_connector

    def attach(evloop)
      raise RuntimeError, "connection failed" if @_failed
      
      if @_connector
        @_connector.attach(evloop)
        return self
      end
      
      super
    end

    # Called upon completion of a socket connection
    def on_connect; end
    event_callback :on_connect
    
    # Called if a socket connection failed to complete
    def on_connect_failed; end
    event_callback :on_connect_failed
    
    # Called if a hostname failed to resolve when connecting
    # Defaults to being aliased to on_connect_failed
    alias_method :on_resolve_failed, :on_connect_failed
    
    #########
    protected
    #########
    
    class Connector < IOWatcher
      def initialize(rev_socket, ruby_socket)
        @rev_socket, @ruby_socket = rev_socket, ruby_socket
        super(ruby_socket, :w)
      end
      
      def on_writable
        evl = evloop
        detach

        if connect_successful?
          @rev_socket.instance_eval { @_connector = nil }
          @rev_socket.attach(evl)
          @rev_socket.__send__(:on_connect)
        else
          @rev_socket.instance_eval { @_failed = true }
          @rev_socket.__send__(:on_connect_failed)
        end
      end      

      #######
      private
      #######

      def connect_successful?
        @ruby_socket.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_ERROR).unpack('i').first == 0
      end
    end
  end
  
  class TCPSocket < Socket<commit_msg>Handle IOErrors thrown by getsockopt<commit_after>#--
# Copyright (C)2007 <NAME>
# You can redistribute this under the terms of the Ruby license
# See file LICENSE for details
#++

require 'socket'
require 'resolv'

module Rev
  class Socket < IO    
    def self.connect(socket, *args)
      new(socket, *args).instance_eval do
        @_connector = Connector.new(self, socket)
        self
      end
    end
    
    watcher_delegate :@_connector

    def attach(evloop)
      raise RuntimeError, "connection failed" if @_failed
      
      if @_connector
        @_connector.attach(evloop)
        return self
      end
      
      super
    end

    # Called upon completion of a socket connection
    def on_connect; end
    event_callback :on_connect
    
    # Called if a socket connection failed to complete
    def on_connect_failed; end
    event_callback :on_connect_failed
    
    # Called if a hostname failed to resolve when connecting
    # Defaults to being aliased to on_connect_failed
    alias_method :on_resolve_failed, :on_connect_failed
    
    #########
    protected
    #########
    
    class Connector < IOWatcher
      def initialize(rev_socket, ruby_socket)
        @rev_socket, @ruby_socket = rev_socket, ruby_socket
        super(ruby_socket, :w)
      end
      
      def on_writable
        evl = evloop
        detach

        if connect_successful?
          @rev_socket.instance_eval { @_connector = nil }
          @rev_socket.attach(evl)
          @rev_socket.__send__(:on_connect)
        else
          @rev_socket.instance_eval { @_failed = true }
          @rev_socket.__send__(:on_connect_failed)
        end
      end      

      #######
      private
      #######

      def connect_successful?
        @ruby_socket.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_ERROR).unpack('i').first == 0
      rescue IOError
        false
      end
    end
  end
  
  class TCPSocket < Socket
{'fraction_non_alphanumeric': 0.08241469816272966, 'fraction_numerical': 0.0026246719160104987, 'mean_word_length': 2.285344827586207, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '2801052', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1120, 'n_words': 300}