Class: Celluloid::IO::UDPSocket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/celluloid/io/udp_socket.rb

Overview

UDPSockets with combined blocking and evented support

Instance Method Summary collapse

Constructor Details

#initializeUDPSocket

Returns a new instance of UDPSocket.



8
9
10
# File 'lib/celluloid/io/udp_socket.rb', line 8

def initialize
  @socket = ::UDPSocket.new
end

Instance Method Details

#evented?Boolean

Are we inside of a Celluloid::IO actor?

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/celluloid/io/udp_socket.rb', line 13

def evented?
  actor = Thread.current[:actor]
  actor && actor.mailbox.is_a?(Celluloid::IO::Mailbox)
end

#recvfrom(maxlen, flags = nil) ⇒ Object

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.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/celluloid/io/udp_socket.rb', line 31

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

#to_ioObject



45
# File 'lib/celluloid/io/udp_socket.rb', line 45

def to_io; @socket; end

#wait_readableObject

Wait until the socket is readable



19
20
21
22
23
24
25
# File 'lib/celluloid/io/udp_socket.rb', line 19

def wait_readable
  if evented?
    Celluloid.current_actor.wait_readable(@socket)
  else
    Kernel.select([@socket])
  end
end