Class: Foolio::UDP

Inherits:
Handle show all
Defined in:
lib/foolio/stream.rb

Instance Method Summary collapse

Methods inherited from Handle

#callback, #initialize

Constructor Details

This class inherits a constructor from Foolio::Handle

Instance Method Details

#bind(ip, port) ⇒ Object



89
90
91
92
93
# File 'lib/foolio/stream.rb', line 89

def bind(ip,port)
  @socket = Foolio::UV.ip4_addr(ip, port)
  Foolio::UV.udp_bind(@handle, @socket, 0)
  self
end

#send(ip, port, data) ⇒ Object



104
105
106
107
# File 'lib/foolio/stream.rb', line 104

def send(ip, port, data)
  on_send = proc {|_|}
  Foolio::UV.udp_send(nil, @handle, data, Foolio::UV.ip4_addr(ip, port), on_send)
end

#start(&block) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/foolio/stream.rb', line 95

def start(&block)
  on_recv = callback do|data, addr, flags|
    ip   = Foolio::UV.ip_name addr
    port = Foolio::UV.port addr
    block.call(ip, port, data) if ip
  end
  Foolio::UV.udp_recv_start(@handle, on_recv)
end