@@ -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,37 @@ 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
+
124
150
struct PutHandler < ' a >
125
151
{
126
152
done_cb : & ' a mut ( dyn FnMut ( bool ) )
@@ -165,6 +191,14 @@ extern fn listen_handler_done(ptr: *mut c_void) {
165
191
}
166
192
}
167
193
194
+ struct VerboseDrop < ' a , T > ( T , & ' a str ) ;
195
+
196
+ impl < ' a , T > VerboseDrop < ' a , T > {
197
+ fn drop ( & mut self ) {
198
+ println ! ( "{}" , self . 1 ) ;
199
+ }
200
+ }
201
+
168
202
impl DhtRunner {
169
203
pub fn new ( ) -> Box < DhtRunner > {
170
204
unsafe {
@@ -187,11 +221,30 @@ impl DhtRunner {
187
221
pub fn bootstrap ( & mut self , host : & str , service : u16 ) {
188
222
unsafe {
189
223
dht_runner_bootstrap ( & mut * self ,
190
- CString :: new ( host) . unwrap ( ) . as_ptr ( ) ,
191
- CString :: new ( service. to_string ( ) ) . unwrap ( ) . as_ptr ( ) )
224
+ CString :: new ( host) . unwrap ( ) . as_ptr ( ) ,
225
+ CString :: new ( service. to_string ( ) ) . unwrap ( ) . as_ptr ( ) )
192
226
}
193
227
}
194
228
229
+ pub async fn bootstrap_async < A : Iterator < Item =SocketAddr > > ( & mut self , addrs : A ) -> std:: io:: Result < bool > {
230
+ let socks: Vec < OsSocketAddr > = addrs. map ( |a| a. into ( ) ) . collect ( ) ;
231
+ let sizes: Vec < libc:: socklen_t > = socks. iter ( ) . map ( |s| s. len ( ) ) . collect ( ) ;
232
+
233
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
234
+
235
+ let done_cb = Box :: new ( move |success| tx. send ( success) ) ;
236
+ let handler = Box :: into_raw ( done_cb) as * mut c_void ;
237
+
238
+ unsafe {
239
+ dht_runner_bootstrap2 ( & mut * self , socks. as_ptr ( ) as * const * const _ ,
240
+ sizes. as_ptr ( ) as * const * const _ , handler) ;
241
+ }
242
+
243
+ let success = rx. await . expect ( "bootstrap_async() sender was dropped unexpectedly" ) ;
244
+
245
+ Ok ( success)
246
+ }
247
+
195
248
pub fn node_id ( & self ) -> InfoHash {
196
249
unsafe {
197
250
dht_runner_get_node_id ( & * self )
@@ -217,6 +270,19 @@ impl DhtRunner {
217
270
}
218
271
}
219
272
273
+ pub fn get_async ( & mut self , h : & InfoHash )
274
+ -> impl TryStream < Ok =Box < Value > , Error =std:: io:: Error > + Unpin {
275
+ let ( tx, rx) = mpsc:: unbounded ( ) ;
276
+ let tx = Box :: new ( tx) ;
277
+ let tx = Box :: into_raw ( tx) as * mut c_void ;
278
+
279
+ unsafe {
280
+ dht_runner_get ( & mut * self , h, get_async_handler_cb, done_async_handler_cb, tx)
281
+ }
282
+ rx. take_while ( |item : & Option < _ > | futures:: future:: ready ( item. is_some ( ) ) )
283
+ . filter_map ( |item| futures:: future:: ready ( item) )
284
+ }
285
+
220
286
pub fn put < ' a > ( & mut self , h : & InfoHash , v : Box < Value > ,
221
287
done_cb : & ' a mut ( dyn FnMut ( bool ) ) , permanent : bool ) {
222
288
let handler = Box :: new ( PutHandler {
@@ -228,6 +294,21 @@ impl DhtRunner {
228
294
}
229
295
}
230
296
297
+ pub async fn put_async ( & mut self , h : & InfoHash , v : Box < Value > , permanent : bool ) -> bool {
298
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
299
+ let mut tx = Some ( tx) ;
300
+
301
+ let mut done_cb = move |success| {
302
+ if let Some ( tx) = tx. take ( ) {
303
+ tx. send ( success) . expect ( "put_async() receiver was dropped unexpectedly" ) ;
304
+ }
305
+ } ;
306
+
307
+ self . put ( h, v, & mut done_cb, permanent) ;
308
+
309
+ rx. await . expect ( "put_async() sender was dropped unexpectedly" )
310
+ }
311
+
231
312
pub fn put_signed < ' a > ( & mut self , h : & InfoHash , v : Box < Value > ,
232
313
done_cb : & ' a mut ( dyn FnMut ( bool ) ) , permanent : bool ) {
233
314
let handler = Box :: new ( PutHandler {
@@ -239,6 +320,21 @@ impl DhtRunner {
239
320
}
240
321
}
241
322
323
+ pub async fn put_signed_async ( & mut self , h : & InfoHash , v : Box < Value > , permanent : bool ) -> bool {
324
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
325
+ let mut tx = Some ( tx) ;
326
+
327
+ let mut done_cb = move |success| {
328
+ if let Some ( tx) = tx. take ( ) {
329
+ tx. send ( success) . expect ( "put_signed_async() receiver was dropped unexpectedly" ) ;
330
+ }
331
+ } ;
332
+
333
+ self . put_signed ( h, v, & mut done_cb, permanent) ;
334
+
335
+ rx. await . expect ( "put_signed_async() sender was dropped unexpectedly" )
336
+ }
337
+
242
338
pub fn put_encrypted < ' a > ( & mut self , h : & InfoHash , to : & InfoHash , v : Box < Value > ,
243
339
done_cb : & ' a mut ( dyn FnMut ( bool ) ) , permanent : bool ) {
244
340
let handler = Box :: new ( PutHandler {
@@ -250,6 +346,22 @@ impl DhtRunner {
250
346
}
251
347
}
252
348
349
+ pub async fn put_encrypted_async ( & mut self , h : & InfoHash , to : & InfoHash , v : Box < Value > ,
350
+ permanent : bool ) -> bool {
351
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
352
+ let mut tx = Some ( tx) ;
353
+
354
+ let mut done_cb = move |success| {
355
+ if let Some ( tx) = tx. take ( ) {
356
+ tx. send ( success) . expect ( "put_encrypted_async() receiver was dropped unexpectedly" ) ;
357
+ }
358
+ } ;
359
+
360
+ self . put_encrypted ( h, to, v, & mut done_cb, permanent) ;
361
+
362
+ rx. await . expect ( "put_encrypted_async() sender was dropped unexpectedly" )
363
+ }
364
+
253
365
pub fn cancel_put < ' a > ( & mut self , h : & InfoHash , vid : u64 ) {
254
366
unsafe {
255
367
dht_runner_cancel_put ( & mut * self , h, vid)
@@ -267,6 +379,20 @@ impl DhtRunner {
267
379
}
268
380
}
269
381
382
+ pub fn listen_async ( & mut self , h : & InfoHash )
383
+ -> impl Stream < Item =( Box < Value > , bool ) > + Unpin
384
+ {
385
+ let ( mut tx, rx) = mpsc:: unbounded ( ) ;
386
+
387
+ let mut value_cb = move |v, expired| {
388
+ futures:: executor:: block_on ( tx. send ( ( v, expired) ) ) . is_ok ( )
389
+ } ;
390
+
391
+ let _token = self . listen ( h, & mut value_cb) ;
392
+
393
+ return Box :: pin ( rx) ;
394
+ }
395
+
270
396
pub fn cancel_listen ( & mut self , h : & InfoHash , token : Box < OpToken > ) {
271
397
unsafe {
272
398
dht_runner_cancel_listen ( & mut * self , h, & * token)
@@ -282,6 +408,16 @@ impl DhtRunner {
282
408
}
283
409
}
284
410
411
+ pub async fn shutdown_async < ' a > ( & ' a mut self ) -> bool {
412
+ let ( tx, rx) = futures:: channel:: oneshot:: channel ( ) ;
413
+ let tx = Box :: new ( tx) ;
414
+ let ptr = Box :: into_raw ( tx) as * mut c_void ;
415
+
416
+ self . shutdown ( done_async_handler_cb, ptr) ;
417
+
418
+ rx. await . expect ( "shutdown_async() sender was dropped unexpectedly" )
419
+ }
420
+
285
421
pub fn public_addresses ( & self ) -> Vec < SocketAddr > {
286
422
let mut result = Vec :: new ( ) ;
287
423
unsafe {
0 commit comments