Class: EventMachine::EvmaUDPSocket

Inherits:
DatagramObject show all
Defined in:
lib/pr_eventmachine.rb

Instance Attribute Summary

Attributes inherited from Selectable

#io, #uuid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DatagramObject

#get_outbound_data_size, #initialize, #select_for_reading?, #select_for_writing?, #send_datagram

Methods inherited from Selectable

#close_scheduled?, #get_peername, #heartbeat, #initialize, #select_for_reading?, #select_for_writing?, #set_inactivity_timeout

Constructor Details

This class inherits a constructor from EventMachine::DatagramObject

Class Method Details

.create(host, port) ⇒ Object



920
921
922
923
924
# File 'lib/pr_eventmachine.rb', line 920

def create host, port
  sd = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
  sd.bind Socket::pack_sockaddr_in( port, host )
  EvmaUDPSocket.new sd
end

Instance Method Details

#eventable_readObject

Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006. If we have it, then we can read multiple times safely to improve performance.



954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# File 'lib/pr_eventmachine.rb', line 954

def eventable_read
  begin
    if io.respond_to?(:recvfrom_nonblock)
      40.times {
        data,@return_address = io.recvfrom_nonblock(16384)
        EventMachine::event_callback uuid, ConnectionData, data
        @return_address = nil
      }
    else
      raise "unimplemented datagram-read operation on this Ruby"
    end
  rescue Errno::EAGAIN
    # no-op
  rescue Errno::ECONNRESET, EOFError
    @close_scheduled = true
    EventMachine::event_callback uuid, ConnectionUnbound, nil
  end

end

#eventable_writeObject

#eventable_write This really belongs in DatagramObject, but there is some UDP-specific stuff.



929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# File 'lib/pr_eventmachine.rb', line 929

def eventable_write
  40.times {
    break if @outbound_q.empty?
    begin
      data,target = @outbound_q.first

      # This damn better be nonblocking.
      io.send data.to_s, 0, target

      @outbound_q.shift
    rescue Errno::EAGAIN
      # It's not been observed in testing that we ever get here.
      # True to the definition, packets will be accepted and quietly dropped
      # if the system is under pressure.
      break
    rescue EOFError, Errno::ECONNRESET
      @close_scheduled = true
      @outbound_q.clear
    end
  }
end

#send_data(data) ⇒ Object



975
976
977
# File 'lib/pr_eventmachine.rb', line 975

def send_data data
  send_datagram data, @return_address
end