class UDPRest::UDPServer

Attributes

socket[RW]

Public Class Methods

new() click to toggle source
# File lib/udp_rest/udp.rb, line 9
def initialize
    @max_packet_size = 512
    self.socket = UDPSocket.new
end

Public Instance Methods

host() click to toggle source
# File lib/udp_rest/udp.rb, line 39
def host
    @host
end
listen(port, options = {}) { |response| ... } click to toggle source
# File lib/udp_rest/udp.rb, line 18
def listen(port, options = {})
    @port = port.to_i
    @host = options[:host] || '0.0.0.0'
    self.socket.bind(@host, @port)

    loop do
        response = self.receive()
        yield(response)
    end
end
max_packet_size() click to toggle source
# File lib/udp_rest/udp.rb, line 14
def max_packet_size
    @max_packet_size
end
port() click to toggle source
# File lib/udp_rest/udp.rb, line 43
def port
    @port
end
receive() click to toggle source
# File lib/udp_rest/udp.rb, line 29
def receive
    data = self.socket.recvfrom(@max_packet_size)
    UDPPacket.new(data)
end
send(text, host, port) click to toggle source
# File lib/udp_rest/udp.rb, line 34
def send(text, host, port)
    raise "message too long (max is #{@max_packet_size}b, was #{text.bytesize})" if text.bytesize > @max_packet_size
    self.socket.send(text, 0, host, port)
end