Sha256: fa03bbdae5ba81fca53f4ccedeafb2bf3eb4308d62f5efb90231119f8f1608e7

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 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[:celluloid_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

3 entries across 3 versions & 1 rubygems

Version Path
celluloid-io-0.13.0 lib/celluloid/io/udp_socket.rb
celluloid-io-0.13.0.pre2 lib/celluloid/io/udp_socket.rb
celluloid-io-0.13.0.pre lib/celluloid/io/udp_socket.rb