Sha256: f66d07bce52c1f79c0bc0c03278c220fa31ac0c41d8e29c588a54af85480ea35

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'easy_sockets/basic_socket'

module EasySockets
    #
    # @author Marcos Ortiz
    # Subclass of {EasySockets::BasicSocket} that implement a UDP socket.
    #
    class UdpSocket < EasySockets::BasicSocket
        
        #
        # @param [Hash] opts the options to create a socket with.
        # @option opts [Integer] :port (2000) The udp port the server is running on.
        # @option opts [String] :host ('127.0.0.1') The hostname or IP address the server is running on.
        #
        # It also accepts all options that {EasySockets::BasicSocket#initialize} accepts
        def initialize(opts={})
            super(opts)
            
            @port = opts[:port].to_i
            @port = DEFAULT_PORT if @port <= 0
            @host = opts[:host] || DEFAULT_HOST
        end
        
        private
        
        def on_connect
            @socket = UDPSocket.new
            @socket.connect(@host, @port)
            log(:debug, "Successfully connected to udp://#{@host}:#{@port}.")
        rescue Exception => e
            @socket.close if @socket && !@socket.closed?
            raise e
        end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy_sockets-1.0.0 lib/easy_sockets/udp/udp_socket.rb