Class: Libuv::UDP

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

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::INET6_ADDRSTRLEN, Net::INET_ADDRSTRLEN, 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, #loop, #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

#resolved?, #then

Methods inherited from Q::Promise

#catch, #finally

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) ⇒ Object



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

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

    sockaddr = create_sockaddr(ip, port)
    error = check_result ::Libuv::Ext.udp_bind(handle, sockaddr, 0)
    reject(error) if error
end

#disable_broadcastObject



155
156
157
158
159
# File 'lib/libuv/udp.rb', line 155

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

#disable_multicast_loopObject



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

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

#enable_broadcastObject



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

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

#enable_multicast_loopObject



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

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



45
46
47
48
49
50
51
52
# File 'lib/libuv/udp.rb', line 45

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



54
55
56
57
58
59
60
61
# File 'lib/libuv/udp.rb', line 54

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



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

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

#open(fd, binding = true, callback = nil, &blk) ⇒ Object



32
33
34
35
36
# File 'lib/libuv/udp.rb', line 32

def open(fd, binding = true, callback = nil, &blk)
    return if @closed
    error = check_result UV.udp_open(handle, fd)
    reject(error) if error
end

#progress(callback = nil, &blk) ⇒ Object



168
169
170
# File 'lib/libuv/udp.rb', line 168

def progress(callback = nil, &blk)
    @progress = callback || blk
end

#send(ip, port, data) ⇒ Object



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
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/libuv/udp.rb', line 79

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)

            sockaddr = create_sockaddr(ip, port)

            # local as this variable will be avaliable until the handle is closed
            @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
            #
            @sent_callbacks << [deferred, callback]
            error = check_result ::Libuv::Ext.udp_send(
                send_req,
                handle,
                buf_init(data),
                1,
                sockaddr,
                callback
            )
            if error
                @sent_callbacks.pop
                deferred.reject(error)
                reject(error)       # close the handle
            end
        rescue StandardError => e
            deferred.reject(e)
        end
    else
        deferred.reject(RuntimeError.new(HANDLE_CLOSED_ERROR))
    end
    deferred.promise
end

#socknameObject



38
39
40
41
42
43
# File 'lib/libuv/udp.rb', line 38

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_readObject

Starts reading from the handle Renamed to match Stream



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

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

#stop_readObject

Stops reading from the handle Renamed to match Stream



73
74
75
76
77
# File 'lib/libuv/udp.rb', line 73

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

#ttl=(ttl) ⇒ Object



161
162
163
164
165
166
# File 'lib/libuv/udp.rb', line 161

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