-
Notifications
You must be signed in to change notification settings - Fork 101
Async api, take 2 #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3b154f8
Implemented most of async pool api
TheButlah b165387
Updated async example
TheButlah 0117662
Removed some out of date documentation, cleaned things up
TheButlah 6089ca7
Implemented everything but drop strategy
TheButlah 66b3778
Now resubmitting transfers
TheButlah 65242ea
Now using no read timeout (timeout=0)
TheButlah af44e5d
Added AsyncPool.size()
TheButlah e553b67
Renamed Transfer to PinnedTransfer, exported AsyncError
TheButlah 781b6c3
Made AsyncPool Send + Sync
TheButlah 900531e
Implemented transfer cancellation
TheButlah c6b98b1
Added overflow error variant
TheButlah 258fdcf
Made read_async more useful of a program
TheButlah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| [package] | ||
| name = "rusb" | ||
| version = "0.8.0" | ||
| authors = ["David Cuddeback <[email protected]>", "Ilya Averyanov <[email protected]>"] | ||
| authors = [ | ||
| "David Cuddeback <[email protected]>", | ||
| "Ilya Averyanov <[email protected]>", | ||
| ] | ||
| description = "Rust library for accessing USB devices." | ||
| license = "MIT" | ||
| homepage = "https://github.com/a1ien/rusb" | ||
|
|
@@ -15,14 +18,16 @@ build = "build.rs" | |
| travis-ci = { repository = "a1ien/rusb" } | ||
|
|
||
| [features] | ||
| vendored = [ "libusb1-sys/vendored" ] | ||
| vendored = ["libusb1-sys/vendored"] | ||
|
|
||
| [workspace] | ||
| members = ["libusb1-sys"] | ||
|
|
||
| [dependencies] | ||
| libusb1-sys = { path = "libusb1-sys", version = "0.5.0" } | ||
| libc = "0.2" | ||
| log = "0.4" | ||
| thiserror = "1" | ||
|
|
||
| [dev-dependencies] | ||
| regex = "1" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| use rusb::{AsyncPool, Context, UsbContext}; | ||
|
|
||
| use std::str::FromStr; | ||
| use std::time::Duration; | ||
|
|
||
| fn main() { | ||
| let args: Vec<String> = std::env::args().collect(); | ||
|
|
||
| if args.len() < 4 { | ||
| eprintln!("Usage: read_async <vendor-id> <product-id> <endpoint>"); | ||
| return; | ||
| } | ||
|
|
||
| let vid: u16 = FromStr::from_str(args[1].as_ref()).unwrap(); | ||
| let pid: u16 = FromStr::from_str(args[2].as_ref()).unwrap(); | ||
| let endpoint: u8 = FromStr::from_str(args[3].as_ref()).unwrap(); | ||
|
|
||
| let ctx = Context::new().expect("Could not initialize libusb"); | ||
| let device = ctx | ||
| .open_device_with_vid_pid(vid, pid) | ||
| .expect("Could not find device"); | ||
|
|
||
| const NUM_TRANSFERS: usize = 32; | ||
| const BUF_SIZE: usize = 1024; | ||
|
|
||
| let mut buffers = Vec::new(); | ||
| for _ in 0..NUM_TRANSFERS { | ||
| let buf = Vec::with_capacity(BUF_SIZE); | ||
| buffers.push(buf); | ||
| } | ||
|
|
||
| let mut async_pool = | ||
| AsyncPool::new_bulk(device, endpoint, buffers).expect("Failed to create async pool!"); | ||
|
|
||
| let mut swap_vec = Vec::with_capacity(BUF_SIZE); | ||
| let timeout = Duration::from_secs(10); | ||
|
|
||
| let mut num_bytes = 0u64; | ||
| let mut num_transfers = 0u64; | ||
| let mut last_time = std::time::Instant::now(); | ||
| loop { | ||
| let poll_result = async_pool.poll(timeout, swap_vec); | ||
| match poll_result { | ||
| Ok(data) => { | ||
| num_bytes += data.len() as u64; | ||
| swap_vec = data | ||
| } | ||
| Err((err, buf)) => { | ||
| eprintln!("Error: {}", err); | ||
| swap_vec = buf | ||
| } | ||
| } | ||
| num_transfers += 1; | ||
|
|
||
| let elapsed = last_time.elapsed(); | ||
| if elapsed >= Duration::from_millis(1000) { | ||
| let elapsed = elapsed.as_secs_f32(); | ||
| println!( | ||
| "KiB per second: {}, \tTx per second: {}", | ||
| num_bytes as f32 / 1024. / elapsed, | ||
| num_transfers as f32 / elapsed | ||
| ); | ||
| num_bytes = 0; | ||
| num_transfers = 0; | ||
| last_time = std::time::Instant::now(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.