@@ -2,6 +2,7 @@ use crate::TCP_SERVER_PORT;
2
2
use bytesize:: ByteSize ;
3
3
use simplelog:: * ;
4
4
use std:: rc:: Rc ;
5
+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
5
6
use std:: sync:: Arc ;
6
7
use std:: time:: { Duration , Instant } ;
7
8
use tokio:: sync:: Notify ;
@@ -62,6 +63,7 @@ async fn copy<A: Endpoint<A>, B: Endpoint<B>>(
62
63
dbg_name : & ' static str ,
63
64
direction : & ' static str ,
64
65
stats_interval : Option < Duration > ,
66
+ bytes_written : Arc < AtomicUsize > ,
65
67
) -> Result < ( ) , std:: io:: Error > {
66
68
// For statistics
67
69
let mut bytes_out: usize = 0 ;
@@ -115,6 +117,7 @@ async fn copy<A: Endpoint<A>, B: Endpoint<B>>(
115
117
debug ! ( "{}: after write, {} bytes" , dbg_name, n) ;
116
118
// Increment byte counters for statistics
117
119
if stats_interval. is_some ( ) {
120
+ bytes_written. fetch_add ( n, Ordering :: Relaxed ) ;
118
121
bytes_out += n;
119
122
bytes_out_last += n;
120
123
}
@@ -126,6 +129,16 @@ async fn copy<A: Endpoint<A>, B: Endpoint<B>>(
126
129
}
127
130
}
128
131
132
+ async fn transfer_monitor (
133
+ stats_interval : Option < Duration > ,
134
+ _: Arc < AtomicUsize > ,
135
+ _: Arc < AtomicUsize > ,
136
+ ) -> Result < ( ) , std:: io:: Error > {
137
+ //TODO: implementation
138
+
139
+ Ok ( ( ) )
140
+ }
141
+
129
142
pub async fn io_loop (
130
143
stats_interval : Option < Duration > ,
131
144
need_restart : Arc < Notify > ,
@@ -180,7 +193,9 @@ pub async fn io_loop(
180
193
// tokio-uring runtime is single-threaded, we can use `Rc` instead of
181
194
// `Arc`.
182
195
let file = Rc :: new ( usb) ;
196
+ let file_bytes = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
183
197
let stream = Rc :: new ( stream) ;
198
+ let stream_bytes = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
184
199
185
200
// We need to copy in both directions...
186
201
let mut from_file = tokio_uring:: spawn ( copy (
@@ -189,17 +204,23 @@ pub async fn io_loop(
189
204
"USB" ,
190
205
"📲 car to phone" ,
191
206
stats_interval,
207
+ stream_bytes. clone ( ) ,
192
208
) ) ;
193
209
let mut from_stream = tokio_uring:: spawn ( copy (
194
210
stream. clone ( ) ,
195
211
file. clone ( ) ,
196
212
"TCP" ,
197
213
"📱 phone to car" ,
198
214
stats_interval,
215
+ file_bytes. clone ( ) ,
199
216
) ) ;
200
217
218
+ // Thread for monitoring transfer
219
+ let mut monitor =
220
+ tokio_uring:: spawn ( transfer_monitor ( stats_interval, file_bytes, stream_bytes) ) ;
221
+
201
222
// Stop as soon as one of them errors
202
- let res = tokio:: try_join!( & mut from_file, & mut from_stream) ;
223
+ let res = tokio:: try_join!( & mut from_file, & mut from_stream, & mut monitor ) ;
203
224
if let Err ( e) = res {
204
225
error ! ( "{} Connection error: {}" , NAME , e) ;
205
226
}
@@ -208,6 +229,7 @@ pub async fn io_loop(
208
229
// for each direction)
209
230
from_file. abort ( ) ;
210
231
from_stream. abort ( ) ;
232
+ monitor. abort ( ) ;
211
233
212
234
// stream(s) closed, notify main loop to restart
213
235
need_restart. notify_one ( ) ;
0 commit comments