Class: Libuv::UDP
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
-
#bind(ip, port) ⇒ Object
-
#disable_broadcast ⇒ Object
-
#disable_multicast_reactor ⇒ Object
-
#enable_broadcast ⇒ Object
-
#enable_multicast_reactor ⇒ Object
-
#initialize(reactor, progress: nil) ⇒ UDP
constructor
-
#join(multicast_address, interface_address) ⇒ Object
-
#leave(multicast_address, interface_address) ⇒ Object
-
#multicast_ttl=(ttl) ⇒ Object
-
#open(fd, binding = true, callback = nil, &blk) ⇒ Object
-
#progress(callback = nil, &blk) ⇒ Object
-
#send(ip, port, data, wait: false) ⇒ Object
-
#sockname ⇒ Object
-
#start_read ⇒ Object
Starts reading from the handle Renamed to match Stream.
-
#stop_read ⇒ Object
Stops reading from the handle Renamed to match Stream.
-
#try_send(ip, port, data) ⇒ Object
-
#ttl=(ttl) ⇒ Object
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
#resolved?, #then
Methods inherited from Q::Promise
#catch, #finally, #ruby_catch, #value
Constructor Details
#initialize(reactor, progress: nil) ⇒ UDP
Returns a new instance of UDP.
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/libuv/udp.rb', line 23
def initialize(reactor, progress: nil)
@reactor = reactor
@progress = progress
udp_ptr = ::Libuv::Ext.allocate_handle_udp
error = check_result(::Libuv::Ext.udp_init(reactor.handle, udp_ptr))
@request_refs = {}
super(udp_ptr, error)
end
|
Instance Method Details
#bind(ip, port) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/libuv/udp.rb', line 34
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_broadcast ⇒ Object
201
202
203
204
205
206
|
# File 'lib/libuv/udp.rb', line 201
def disable_broadcast
return if @closed
error = check_result ::Libuv::Ext.udp_set_broadcast(handle, 0)
reject(error) if error
self
end
|
#disable_multicast_reactor ⇒ Object
179
180
181
182
183
184
|
# File 'lib/libuv/udp.rb', line 179
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_broadcast ⇒ Object
194
195
196
197
198
199
|
# File 'lib/libuv/udp.rb', line 194
def enable_broadcast
return if @closed
error = check_result ::Libuv::Ext.udp_set_broadcast(handle, 1)
reject(error) if error
self
end
|
#enable_multicast_reactor ⇒ Object
172
173
174
175
176
177
|
# File 'lib/libuv/udp.rb', line 172
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
61
62
63
64
65
66
67
68
69
|
# File 'lib/libuv/udp.rb', line 61
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
71
72
73
74
75
76
77
78
79
|
# File 'lib/libuv/udp.rb', line 71
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
186
187
188
189
190
191
192
|
# File 'lib/libuv/udp.rb', line 186
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, callback = nil, &blk) ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/libuv/udp.rb', line 46
def open(fd, binding = true, callback = nil, &blk)
return if @closed
error = check_result UV.udp_open(handle, fd)
reject(error) if error
self
end
|
#progress(callback = nil, &blk) ⇒ Object
216
217
218
219
|
# File 'lib/libuv/udp.rb', line 216
def progress(callback = nil, &blk)
@progress = callback || blk
self
end
|
#send(ip, port, data, wait: false) ⇒ Object
122
123
124
125
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
|
# File 'lib/libuv/udp.rb', line 122
def send(ip, port, data, wait: false)
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)
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]
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) 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
|
54
55
56
57
58
59
|
# File 'lib/libuv/udp.rb', line 54
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_read ⇒ Object
Starts reading from the handle Renamed to match Stream
83
84
85
86
87
88
|
# File 'lib/libuv/udp.rb', line 83
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_read ⇒ Object
Stops reading from the handle Renamed to match Stream
92
93
94
95
96
97
|
# File 'lib/libuv/udp.rb', line 92
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/libuv/udp.rb', line 99
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
208
209
210
211
212
213
214
|
# File 'lib/libuv/udp.rb', line 208
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
|