@@ -25,6 +25,8 @@ use std::ptr;
25
25
pub use crate :: ffi:: * ;
26
26
use std:: net:: SocketAddr ;
27
27
use os_socketaddr:: OsSocketAddr ;
28
+ use futures:: prelude:: * ;
29
+ use futures:: channel:: mpsc;
28
30
29
31
impl DhtRunnerConfig {
30
32
@@ -114,13 +116,49 @@ extern fn get_handler_cb(v: *mut Value, ptr: *mut c_void) -> bool {
114
116
}
115
117
}
116
118
119
+ extern fn get_async_handler_cb ( v : * mut Value , ptr : * mut c_void ) -> bool {
120
+ if ptr. is_null ( ) {
121
+ return true ;
122
+ }
123
+ let f = unsafe {
124
+ let tx = ptr as * mut mpsc:: UnboundedSender < Option < std:: io:: Result < Box < Value > > > > ;
125
+ ( * tx) . send ( Some ( Ok ( ( * v) . boxed ( ) ) ) )
126
+ } ;
127
+ futures:: executor:: block_on ( f) . is_ok ( )
128
+ }
129
+
117
130
extern fn done_handler_cb ( ok : bool , ptr : * mut c_void ) {
118
131
unsafe {
119
132
let handler = Box :: from_raw ( ptr as * mut GetHandler ) ;
120
133
( * handler. done_cb ) ( ok)
121
134
}
122
135
}
123
136
137
+ extern fn done_async_handler_cb ( ok : bool , ptr : * mut c_void ) {
138
+ if ptr. is_null ( ) {
139
+ return ;
140
+ }
141
+
142
+ let mut tx = unsafe {
143
+ let ptr = ptr as * mut mpsc:: UnboundedSender < Option < std:: io:: Result < Box < Value > > > > ;
144
+ Box :: from_raw ( ptr)
145
+ } ;
146
+ let item = if ok { None } else { Some ( Err ( std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , "get failed" ) ) ) } ;
147
+ let _ = futures:: executor:: block_on ( ( * tx) . send ( item) ) ;
148
+ }
149
+
150
+ extern fn bootstrap_done_async_handler_cb ( ok : bool , ptr : * mut c_void ) {
151
+ if ptr. is_null ( ) {
152
+ return ;
153
+ }
154
+
155
+ let tx = unsafe {
156
+ let ptr = ptr as * mut futures:: channel:: oneshot:: Sender < bool > ;
157
+ Box :: from_raw ( ptr)
158
+ } ;
159
+ let _ = ( * tx) . send ( ok) ;
160
+ }
161
+
124
162
struct PutHandler < ' a >
125
163
{
126
164
done_cb : & ' a mut ( dyn FnMut ( bool ) )
@@ -165,6 +203,14 @@ extern fn listen_handler_done(ptr: *mut c_void) {
165
203
}
166
204
}
167
205
206
+ struct VerboseDrop < ' a , T > ( T , & ' a str ) ;
207
+
208
+ impl < ' a , T > VerboseDrop < ' a , T > {
209
+ fn drop ( & mut self ) {
210
+ println ! ( "{}" , self . 1 ) ;
211
+ }
212
+ }
213
+
168
214
impl DhtRunner {
169
215
pub fn new ( ) -> Box < DhtRunner > {
170
216
unsafe {
@@ -187,11 +233,30 @@ impl DhtRunner {
187
233
pub fn bootstrap ( & mut self , host : & str , service : u16 ) {
188
234
unsafe {
189
235
dht_runner_bootstrap ( & mut * self ,
190
- CString :: new ( host) . unwrap ( ) . as_ptr ( ) ,
191
- CString :: new ( service. to_string ( ) ) . unwrap ( ) . as_ptr ( ) )
236
+ CString :: new ( host) . unwrap ( ) . as_ptr ( ) ,
237
+ CString :: new ( service. to_string ( ) ) . unwrap ( ) . as_ptr ( ) )
192
238
}
193
239
}
194
240
241
+ pub async fn bootstrap_async < A : Iterator < Item =SocketAddr > > ( & mut self , addrs : A ) -> std:: io:: Result < bool > {
242
+ let socks: Vec < OsSocketAddr > = addrs. map ( |a| a. into ( ) ) . collect ( ) ;
243
+ let sizes: Vec < libc:: socklen_t > = socks. iter ( ) . map ( |s| s. len ( ) ) . collect ( ) ;
244
+
245
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
246
+
247
+ let tx = Box :: new ( tx) ;
248
+ let tx = Box :: into_raw ( tx) as * mut c_void ;
249
+
250
+ unsafe {
251
+ dht_runner_bootstrap2 ( & mut * self , socks. as_ptr ( ) as * const * const _ ,
252
+ sizes. as_ptr ( ) as * const * const _ , bootstrap_done_async_handler_cb as * mut c_void , tx) ;
253
+ }
254
+
255
+ let success = rx. await . expect ( "bootstrap_async() sender was dropped unexpectedly" ) ;
256
+
257
+ Ok ( success)
258
+ }
259
+
195
260
pub fn node_id ( & self ) -> InfoHash {
196
261
unsafe {
197
262
dht_runner_get_node_id ( & * self )
@@ -217,6 +282,19 @@ impl DhtRunner {
217
282
}
218
283
}
219
284
285
+ pub fn get_async ( & mut self , h : & InfoHash )
286
+ -> impl TryStream < Ok =Box < Value > , Error =std:: io:: Error > + Unpin {
287
+ let ( tx, rx) = mpsc:: unbounded ( ) ;
288
+ let tx = Box :: new ( tx) ;
289
+ let tx = Box :: into_raw ( tx) as * mut c_void ;
290
+
291
+ unsafe {
292
+ dht_runner_get ( & mut * self , h, get_async_handler_cb, done_async_handler_cb, tx)
293
+ }
294
+ rx. take_while ( |item : & Option < _ > | futures:: future:: ready ( item. is_some ( ) ) )
295
+ . filter_map ( |item| futures:: future:: ready ( item) )
296
+ }
297
+
220
298
pub fn put < ' a > ( & mut self , h : & InfoHash , v : Box < Value > ,
221
299
done_cb : & ' a mut ( dyn FnMut ( bool ) ) , permanent : bool ) {
222
300
let handler = Box :: new ( PutHandler {
@@ -228,6 +306,21 @@ impl DhtRunner {
228
306
}
229
307
}
230
308
309
+ pub async fn put_async ( & mut self , h : & InfoHash , v : Box < Value > , permanent : bool ) -> bool {
310
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
311
+ let mut tx = Some ( tx) ;
312
+
313
+ let mut done_cb = move |success| {
314
+ if let Some ( tx) = tx. take ( ) {
315
+ tx. send ( success) . expect ( "put_async() receiver was dropped unexpectedly" ) ;
316
+ }
317
+ } ;
318
+
319
+ self . put ( h, v, & mut done_cb, permanent) ;
320
+
321
+ rx. await . expect ( "put_async() sender was dropped unexpectedly" )
322
+ }
323
+
231
324
pub fn put_signed < ' a > ( & mut self , h : & InfoHash , v : Box < Value > ,
232
325
done_cb : & ' a mut ( dyn FnMut ( bool ) ) , permanent : bool ) {
233
326
let handler = Box :: new ( PutHandler {
@@ -239,6 +332,21 @@ impl DhtRunner {
239
332
}
240
333
}
241
334
335
+ pub async fn put_signed_async ( & mut self , h : & InfoHash , v : Box < Value > , permanent : bool ) -> bool {
336
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
337
+ let mut tx = Some ( tx) ;
338
+
339
+ let mut done_cb = move |success| {
340
+ if let Some ( tx) = tx. take ( ) {
341
+ tx. send ( success) . expect ( "put_signed_async() receiver was dropped unexpectedly" ) ;
342
+ }
343
+ } ;
344
+
345
+ self . put_signed ( h, v, & mut done_cb, permanent) ;
346
+
347
+ rx. await . expect ( "put_signed_async() sender was dropped unexpectedly" )
348
+ }
349
+
242
350
pub fn put_encrypted < ' a > ( & mut self , h : & InfoHash , to : & InfoHash , v : Box < Value > ,
243
351
done_cb : & ' a mut ( dyn FnMut ( bool ) ) , permanent : bool ) {
244
352
let handler = Box :: new ( PutHandler {
@@ -250,6 +358,22 @@ impl DhtRunner {
250
358
}
251
359
}
252
360
361
+ pub async fn put_encrypted_async ( & mut self , h : & InfoHash , to : & InfoHash , v : Box < Value > ,
362
+ permanent : bool ) -> bool {
363
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
364
+ let mut tx = Some ( tx) ;
365
+
366
+ let mut done_cb = move |success| {
367
+ if let Some ( tx) = tx. take ( ) {
368
+ tx. send ( success) . expect ( "put_encrypted_async() receiver was dropped unexpectedly" ) ;
369
+ }
370
+ } ;
371
+
372
+ self . put_encrypted ( h, to, v, & mut done_cb, permanent) ;
373
+
374
+ rx. await . expect ( "put_encrypted_async() sender was dropped unexpectedly" )
375
+ }
376
+
253
377
pub fn cancel_put < ' a > ( & mut self , h : & InfoHash , vid : u64 ) {
254
378
unsafe {
255
379
dht_runner_cancel_put ( & mut * self , h, vid)
@@ -267,6 +391,20 @@ impl DhtRunner {
267
391
}
268
392
}
269
393
394
+ pub fn listen_async ( & mut self , h : & InfoHash )
395
+ -> impl Stream < Item =( Box < Value > , bool ) > + Unpin
396
+ {
397
+ let ( mut tx, rx) = mpsc:: unbounded ( ) ;
398
+
399
+ let mut value_cb = move |v, expired| {
400
+ futures:: executor:: block_on ( tx. send ( ( v, expired) ) ) . is_ok ( )
401
+ } ;
402
+
403
+ let _token = self . listen ( h, & mut value_cb) ;
404
+
405
+ return Box :: pin ( rx) ;
406
+ }
407
+
270
408
pub fn cancel_listen ( & mut self , h : & InfoHash , token : Box < OpToken > ) {
271
409
unsafe {
272
410
dht_runner_cancel_listen ( & mut * self , h, & * token)
@@ -282,6 +420,16 @@ impl DhtRunner {
282
420
}
283
421
}
284
422
423
+ pub async fn shutdown_async < ' a > ( & ' a mut self ) -> bool {
424
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
425
+ let tx = Box :: new ( tx) ;
426
+ let ptr = Box :: into_raw ( tx) as * mut c_void ;
427
+
428
+ self . shutdown ( done_async_handler_cb, ptr) ;
429
+
430
+ rx. await . expect ( "shutdown_async() sender was dropped unexpectedly" )
431
+ }
432
+
285
433
pub fn public_addresses ( & self ) -> Vec < SocketAddr > {
286
434
let mut result = Vec :: new ( ) ;
287
435
unsafe {
0 commit comments