Class: Libuv::UDP

Inherits:
Handle show all
Includes:
Net
Defined in:
lib/libuv/udp.rb

Defined Under Namespace

Modules: SocketMethods Classes: Socket4, Socket6

Constant Summary collapse

SEND_DATA_ERROR =
"data must be a String".freeze
TTL_ARGUMENT_ERROR =
"ttl must be an Integer".freeze
MULTICAST_ARGUMENT_ERROR =
"multicast_address must be a String".freeze
INTERFACE_ARGUMENT_ERROR =
"interface_address must be a String".freeze
HANDLE_CLOSED_ERROR =
"unable to send as handle closed".freeze

Constants included from Net

Net::IP_ARGUMENT_ERROR, Net::PORT_ARGUMENT_ERROR

Constants included from Assertions

Assertions::MSG_NO_PROC

Constants inherited from Q::Promise

Q::Promise::MAKE_PROMISE

Instance Attribute Summary

Attributes inherited from Handle

#closed, #storage

Instance Method Summary collapse

Methods inherited from Handle

#active?, #close, #closing?, #ref, #unref

Methods included from Assertions

#assert_block, #assert_boolean, #assert_type

Methods included from Resource

#check_result, #check_result!, #resolve, #to_ptr

Methods inherited from Q::DeferredPromise

#then

Methods inherited from Q::Promise

#catch, #finally, #progress

Constructor Details

#initialize(loop) ⇒ UDP

Returns a new instance of UDP.



13
14
15
16
17
18
19
20
# File 'lib/libuv/udp.rb', line 13

def initialize(loop)
    @loop = loop

    udp_ptr = ::Libuv::Ext.create_handle(:uv_udp)
    error = check_result(::Libuv::Ext.udp_init(loop.handle, udp_ptr))

    super(udp_ptr, error)
end

Instance Method Details

#bind(ip, port, ipv6_only = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/libuv/udp.rb', line 22

def bind(ip, port, ipv6_only = false)
    return if @closed
    assert_type(String, ip, IP_ARGUMENT_ERROR)
    assert_type(Integer, port, PORT_ARGUMENT_ERROR)

    begin
        @udp_socket = create_socket(IPAddr.new(ip), port)
        @udp_socket.bind(ipv6_only)
    rescue Exception => e
        reject(e)
    end
end

#disable_broadcast ⇒ Object



143
144
145
146
147
# File 'lib/libuv/udp.rb', line 143

def disable_broadcast
    return if @closed
    error = check_result ::Libuv::Ext.udp_set_broadcast(handle, 0)
    reject(error) if error
end

#disable_multicast_loop ⇒ Object



124
125
126
127
128
# File 'lib/libuv/udp.rb', line 124

def disable_multicast_loop
    return if @closed
    error = check_result ::Libuv::Ext.udp_set_multicast_loop(handle, 0)
    reject(error) if error
end

#enable_broadcast ⇒ Object



137
138
139
140
141
# File 'lib/libuv/udp.rb', line 137

def enable_broadcast
    return if @closed
    error = check_result ::Libuv::Ext.udp_set_broadcast(handle, 1)
    reject(error) if error
end

#enable_multicast_loop ⇒ Object



118
119
120
121
122
# File 'lib/libuv/udp.rb', line 118

def enable_multicast_loop
    return if @closed
    error = check_result ::Libuv::Ext.udp_set_multicast_loop(handle, 1)
    reject(error) if error
end

#join(multicast_address, interface_address) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/libuv/udp.rb', line 42

def join(multicast_address, interface_address)
    return if @closed
    assert_type(String, multicast_address, MULTICAST_ARGUMENT_ERROR)
    assert_type(String, interface_address, INTERFACE_ARGUMENT_ERROR)

    error = check_result ::Libuv::Ext.udp_set_membership(handle, multicast_address, interface_address, :uv_join_group)
    reject(error) if error
end

#leave(multicast_address, interface_address) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/libuv/udp.rb', line 51

def leave(multicast_address, interface_address)
    return if @closed
    assert_type(String, multicast_address, MULTICAST_ARGUMENT_ERROR)
    assert_type(String, interface_address, INTERFACE_ARGUMENT_ERROR)

    error = check_result ::Libuv::Ext.udp_set_membership(handle, multicast_address, interface_address, :uv_leave_group)
    reject(error) if error
end

#multicast_ttl=(ttl) ⇒ Object



130
131
132
133
134
135
# File 'lib/libuv/udp.rb', line 130

def multicast_ttl=(ttl)
    return if @closed
    assert_type(Integer, ttl, TTL_ARGUMENT_ERROR)
    error = check_result ::Libuv::Ext.udp_set_multicast_ttl(handle, ttl)
    reject(error) if error
end

#send(ip, port, data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/libuv/udp.rb', line 72

def send(ip, port, data)
    # NOTE:: Similar to stream.rb -> write
    deferred = @loop.defer
    if !@closed
        begin
            assert_type(String, ip, IP_ARGUMENT_ERROR)
            assert_type(Integer, port, PORT_ARGUMENT_ERROR)
            assert_type(String, data, SEND_DATA_ERROR)

            @udp_socket = create_socket(IPAddr.new(ip), port)

            # local as this variable will be avaliable until the handle is closed
            @sent_callbacks = @sent_callbacks || []

            #
            # create the curried callback
            #
            callback = FFI::Function.new(:void, [:pointer, :int]) do |req, status|
                ::Libuv::Ext.free(req)
                # remove the callback from the array
                # assumes sends are done in order
                promise = @sent_callbacks.shift[0]
                resolve promise, status
            end

            #
            # Save the callback and return the promise
            #
            begin
                @sent_callbacks << [deferred, callback]
                @udp_socket.send(data, callback)
            rescue Exception => e
                @sent_callbacks.pop
                deferred.reject(e)

                reject(e)       # close the handle
            end
        rescue Exception => e
            deferred.reject(e)
        end
    else
        deferred.reject(RuntimeError.new(HANDLE_CLOSED_ERROR))
    end
    deferred.promise
end

#sockname ⇒ Object



35
36
37
38
39
40
# File 'lib/libuv/udp.rb', line 35

def sockname
    return [] if @closed
    sockaddr, len = get_sockaddr_and_len
    check_result! ::Libuv::Ext.udp_getsockname(handle, sockaddr, len)
    get_ip_and_port(UV::Sockaddr.new(sockaddr), len.get_int(0))
end

#start_recv ⇒ Object



60
61
62
63
64
# File 'lib/libuv/udp.rb', line 60

def start_recv
    return if @closed
    error = check_result ::Libuv::Ext.udp_recv_start(handle, callback(:on_allocate), callback(:on_recv))
    reject(error) if error
end

#stop_recv ⇒ Object



66
67
68
69
70
# File 'lib/libuv/udp.rb', line 66

def stop_recv
    return if @closed
    error = check_result ::Libuv::Ext.udp_recv_stop(handle)
    reject(error) if error
end

#ttl=(ttl) ⇒ Object



149
150
151
152
153
154
# File 'lib/libuv/udp.rb', line 149

def ttl=(ttl)
    return if @closed
    assert_type(Integer, ttl, TTL_ARGUMENT_ERROR)
    error = check_result ::Libuv::Ext.udp_set_ttl(handle, Integer(ttl))
    reject(error) if error
end