Sha256: b02e3f4072fe42bf66974e41a498021773400aff7c403f3cbd008843489b4a77

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

module Celluloid
  module IO
    # UDPSockets with combined blocking and evented support
    class UDPSocket
      extend Forwardable
      def_delegators :@socket, :bind, :send, :recvfrom_nonblock, :close, :closed?

      def initialize
        @socket = ::UDPSocket.new
      end

      # Are we inside of a Celluloid::IO actor?
      def evented?
        actor = Thread.current[:actor]
        actor && actor.mailbox.is_a?(Celluloid::IO::Mailbox)
      end

      # Wait until the socket is readable
      def wait_readable
        if evented?
          Celluloid.current_actor.wait_readable(@socket)
        else
          Kernel.select([@socket])
        end
      end

      # Receives up to maxlen bytes from socket. flags is zero or more of the
      # MSG_ options. The first element of the results, mesg, is the data
      # received. The second element, sender_addrinfo, contains
      # protocol-specific address information of the sender.
      def recvfrom(maxlen, flags = nil)
        begin
          if @socket.respond_to? :recvfrom_nonblock
            @socket.recvfrom_nonblock(maxlen, flags)
          else
            # FIXME: hax for JRuby
            @socket.recvfrom(maxlen, flags)
          end
        rescue ::IO::WaitReadable
          wait_readable
          retry
        end
      end

      def to_io; @socket; end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
celluloid-io-0.12.1 lib/celluloid/io/udp_socket.rb
celluloid-io-0.12.0 lib/celluloid/io/udp_socket.rb
celluloid-io-0.11.0 lib/celluloid/io/udp_socket.rb
celluloid-io-0.10.0 lib/celluloid/io/udp_socket.rb
celluloid-io-0.9.0 lib/celluloid/io/udp_socket.rb