<commit_before>require 'ionian/extension/io'
require 'socket'
require 'ipaddr'

module Ionian
  module Extension
    # A mixin for Socket objects.
    # 
    # This module was designed to be extended by instantiated objects
    # that implement the standard library Socket class.
    # my_socket.extend Ionian::Socket
    # 
    # Extending this module also extends Ionian::IO.
    module Socket
      
      # Called automaticallly when the object is extended with #extend.
      def self.extended obj
        obj.extend Ionian::Extension::IO
        obj.initialize_ionian_socket
      end
      
      # Initialize the Ionian Socket variables.
      # This is called automatically if #extend is called on an object.
      def initialize_ionian_socket
      end
      
      # Returns true if sending broadcast datagrams is permitted.
      # ( SO_BROADCAST )
      def broadcast
        param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST)
          .data.unpack('i').first
        param > 0 ? true : false
      end
      
      # Permit sending broadcast datagrams if true.
      # ( SO_BROADCAST )
      def broadcast= value
        param = (!!value && value != 0) ? 1 : 0
        self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, [param].pack('i')
      end
      
      alias_method :broadcast?, :broadcast
      
      # For connection-oriented protocols, prevent #close from returning
      # immediately and try to deliver any data in the send buffer if value
      # is true.
      # ( SO_LINGER )
      def linger
        param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_LINGER)
          .data.unpack('i').first
        param > 0 ? true : false
      end
      
      # For connection-oriented protocols, prevent #close from returning
      # immediately and try to deliver any data in the send buffer if value
      # is true.
      # 
      # Args:
      #   Time: Time in seconds to remain open before discarding data and
      #         sending a RST packet.
      # ( SO_LINGER )
      def linger= enable, time: 60
        # TODO: Passing a kwarg doesn't work here because of the
        #       assignment operator. Causes parser error.
        param = (!!enable && enable != 0) ? 1 : 0
        self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_LINGER, [param, time.to_i].pack('ii')
      end
      
      alias_method :linger?, :linger
      
      # Returns true if local address reuse is allowed.
      # ( SO_REUSEADDR )
      def reuse_addr
        param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR)
          .data.unpack('i').first
        param > 0 ? true : false
      end
      
      alias_method :reuse_addr?, :reuse_addr
      
      # Allows local address reuse if true.
      # ( SO_REUSEADDR )<commit_msg>Removed #linger= todo.<commit_after>require 'ionian/extension/io'
require 'socket'
require 'ipaddr'

module Ionian
  module Extension
    # A mixin for Socket objects.
    # 
    # This module was designed to be extended by instantiated objects
    # that implement the standard library Socket class.
    # my_socket.extend Ionian::Socket
    # 
    # Extending this module also extends Ionian::IO.
    module Socket
      
      # Called automaticallly when the object is extended with #extend.
      def self.extended obj
        obj.extend Ionian::Extension::IO
        obj.initialize_ionian_socket
      end
      
      # Initialize the Ionian Socket variables.
      # This is called automatically if #extend is called on an object.
      def initialize_ionian_socket
      end
      
      # Returns true if sending broadcast datagrams is permitted.
      # ( SO_BROADCAST )
      def broadcast
        param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST)
          .data.unpack('i').first
        param > 0 ? true : false
      end
      
      # Permit sending broadcast datagrams if true.
      # ( SO_BROADCAST )
      def broadcast= value
        param = (!!value && value != 0) ? 1 : 0
        self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, [param].pack('i')
      end
      
      alias_method :broadcast?, :broadcast
      
      # For connection-oriented protocols, prevent #close from returning
      # immediately and try to deliver any data in the send buffer if value
      # is true.
      # ( SO_LINGER )
      def linger
        param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_LINGER)
          .data.unpack('i').first
        param > 0 ? true : false
      end
      
      # For connection-oriented protocols, prevent #close from returning
      # immediately and try to deliver any data in the send buffer if value
      # is true.
      # 
      # @param time [Fixnum] Time in seconds to remain open before discarding
      #   data and sending a RST packet.
      # 
      # ( SO_LINGER )
      def linger= enable, time: 60
        param = (!!enable && enable != 0) ? 1 : 0
        self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_LINGER, [param, time.to_i].pack('ii')
      end
      
      alias_method :linger?, :linger
      
      # Returns true if local address reuse is allowed.
      # ( SO_REUSEADDR )
      def reuse_addr
        param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR)
          .data.unpack('i').first
        param > 0 ? true : false
      end
      
      alias_method :reuse_addr?, :reuse_addr
      
      # Allows local address reuse if true.
      # ( SO_REUSEADDR )
{'fraction_non_alphanumeric': 0.08676631425446592, 'fraction_numerical': 0.004010207801676996, 'mean_word_length': 2.206896551724138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '902067', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1567, 'n_words': 506}