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"
TTL_ARGUMENT_ERROR =
"ttl must be an Integer"
MULTICAST_ARGUMENT_ERROR =
"multicast_address must be a String"
INTERFACE_ARGUMENT_ERROR =
"interface_address must be a String"
HANDLE_CLOSED_ERROR =
"unable to send as handle closed"

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, #reactor, #storage

Attributes inherited from Q::Promise

#trace

Instance Method Summary collapse

Methods inherited from Handle

#active?, #close, #closed?, #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, #ruby_catch, #value

Constructor Details

#initialize(reactor, progress: nil, flags: nil) ⇒ UDP

Returns a new instance of UDP.



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

def initialize(reactor, progress: nil, flags: nil)
    @reactor = reactor
    @progress = progress

    udp_ptr = ::Libuv::Ext.allocate_handle_udp
    error = if flags
        check_result(::Libuv::Ext.udp_init_ex(reactor.handle, udp_ptr, flags))
    else
        check_result(::Libuv::Ext.udp_init(reactor.handle, udp_ptr))
    end
    @request_refs = {}

    super(udp_ptr, error)
end

Instance Method Details

#bind(ip, port) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/libuv/udp.rb', line 38

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

    self
end

#disable_broadcastObject



205
206
207
208
209
210
# File 'lib/libuv/udp.rb', line 205

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

#disable_multicast_reactorObject



183
184
185
186
187
188
# File 'lib/libuv/udp.rb', line 183

def disable_multicast_reactor
    return if @closed
    error = check_result ::Libuv::Ext.udp_set_multicast_reactor(handle, 0)
    reject(error) if error
    self
end

#enable_broadcastObject



198
199
200
201
202
203
# File 'lib/libuv/udp.rb', line 198

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

#enable_multicast_reactorObject



176
177
178
179
180
181
# File 'lib/libuv/udp.rb', line 176

def enable_multicast_reactor
    return if @closed
    error = check_result ::Libuv::Ext.udp_set_multicast_reactor(handle, 1)
    reject(error) if error
    self
end

#join(multicast_address, interface_address) ⇒ Object



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

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
    self
end

#leave(multicast_address, interface_address) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/libuv/udp.rb', line 75

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
    self
end

#multicast_ttl=(ttl) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/libuv/udp.rb', line 190

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
    self
end

#open(fd, binding = true) ⇒ Object



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

def open(fd, binding = true)
    return if @closed
    error = check_result ::Libuv::Ext.udp_open(handle, fd)
    reject(error) if error

    self
end

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



220
221
222
223
# File 'lib/libuv/udp.rb', line 220

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

#send(ip, port, data, wait: false) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/libuv/udp.rb', line 126

def send(ip, port, data, wait: false)
    # NOTE:: Similar to stream.rb -> write
    deferred = @reactor.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)

            # Save a reference to this request
            req = send_req
            buffer1 = ::FFI::MemoryPointer.from_string(data)
            buffer  = ::Libuv::Ext.buf_init(buffer1, data.respond_to?(:bytesize) ? data.bytesize : data.size)
            @request_refs[req.address] = [deferred, buffer1]

            # Save the callback and return the promise
            error = check_result ::Libuv::Ext.udp_send(
                req,
                handle,
                buffer,
                1,
                sockaddr,
                callback(:send_complete, req.address)
            )
            if error
                @request_refs.delete req.address
                cleanup_callbacks req.address
                ::Libuv::Ext.free(req)
                buffer1.free
                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

    if wait
        return deferred.promise if wait == :promise
        co deferred.promise
    end

    self
end

#socknameObject



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

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

#start_readObject

Starts reading from the handle Renamed to match Stream



87
88
89
90
91
92
# File 'lib/libuv/udp.rb', line 87

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
    self
end

#stop_readObject

Stops reading from the handle Renamed to match Stream



96
97
98
99
100
101
# File 'lib/libuv/udp.rb', line 96

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

#try_send(ip, port, data) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/libuv/udp.rb', line 103

def try_send(ip, port, data)
    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)

    buffer1 = ::FFI::MemoryPointer.from_string(data)
    buffer  = ::Libuv::Ext.buf_init(buffer1, data.respond_to?(:bytesize) ? data.bytesize : data.size)

    result = ::Libuv::Ext.udp_try_send(
        handle,
        buffer,
        1,
        sockaddr
    )
    buffer1.free

    error = check_result result
    raise error if error
    return result
end

#ttl=(ttl) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/libuv/udp.rb', line 212

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
    self
end