Skip to content

Commit f44e188

Browse files
committed
feat: v0.0.1
0 parents  commit f44e188

File tree

37 files changed

+706
-0
lines changed

37 files changed

+706
-0
lines changed

.github/workflows/rust.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: Build
20+
run: cargo build --verbose
21+
- name: Run tests
22+
run: cargo test --verbose

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[package]
2+
name = "udp-request"
3+
version = "0.0.1"
4+
edition = "2021"
5+
authors = ["ltpp-universe <root@ltpp.vip>"]
6+
license = "MIT"
7+
description = """A simple UDP request library for sending and receiving UDP packets, designed to handle network communication in Rust applications."""
8+
keywords = ["udp", "net", "response", "request", "tool"]
9+
repository = "https://github.com/ltpp-universe/udp-request.git"
10+
categories = ["network-programming", "web-programming"]
11+
exclude = [
12+
"target",
13+
"Cargo.lock",
14+
"sh",
15+
".github"
16+
]
17+
18+
[dependencies]
19+
lombok-macros = "1.7.4"
20+
21+
[profile.dev]
22+
incremental = false
23+
opt-level = 3
24+
lto = true
25+
panic = "unwind"
26+
debug = false
27+
codegen-units = 1
28+
strip = "debuginfo"
29+
backtrace = "off"
30+
31+
[profile.release]
32+
incremental = false
33+
opt-level = 3
34+
lto = true
35+
panic = "unwind"
36+
debug = false
37+
codegen-units = 1
38+
strip = "debuginfo"
39+
backtrace = "off"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 尤雨东
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

readme.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<center>
2+
3+
## udp-request
4+
5+
[![](https://img.shields.io/crates/v/udp-request.svg)](https://crates.io/crates/udp-request)
6+
[![](https://img.shields.io/crates/d/udp-request.svg)](https://img.shields.io/crates/d/udp-request.svg)
7+
[![](https://docs.rs/udp-request/badge.svg)](https://docs.rs/udp-request)
8+
[![](https://github.com/ltpp-universe/udp-request/workflows/Rust/badge.svg)](https://github.com/ltpp-universe/udp-request/actions?query=workflow:Rust)
9+
[![](https://img.shields.io/crates/l/udp-request.svg)](./LICENSE)
10+
11+
</center>
12+
13+
[Official Documentation](https://docs.ltpp.vip/udp-request/)
14+
15+
[Api Docs](https://docs.rs/udp-request/latest/udp_request/)
16+
17+
> A simple UDP request library for sending and receiving UDP packets, designed to handle network communication in Rust applications.
18+
19+
## Installation
20+
21+
To use this crate, you can run cmd:
22+
23+
```shell
24+
cargo add udp-request
25+
```
26+
27+
## Use
28+
29+
#### Send Text
30+
31+
```rs
32+
use udp_request::*;
33+
let mut request_builder = RequestBuilder::new()
34+
.host("127.0.0.1")
35+
.port(80)
36+
.build();
37+
request_builder
38+
.send("udp send".as_bytes())
39+
.and_then(|response| {
40+
println!("ResponseTrait => {:?}", response.text());
41+
Ok(())
42+
})
43+
.unwrap_or_else(|e| println!("Error => {:?}", e));
44+
```
45+
46+
#### Send Binary
47+
48+
```rs
49+
use udp_request::*;
50+
let mut request_builder = RequestBuilder::new()
51+
.host("127.0.0.1")
52+
.port(80)
53+
.build();
54+
request_builder
55+
.send("udp send".as_bytes())
56+
.and_then(|response| {
57+
println!("ResponseTrait => {:?}", response.text());
58+
Ok(())
59+
})
60+
.unwrap_or_else(|e| println!("Error => {:?}", e));
61+
```
62+
63+
## Help
64+
65+
Ensure that CMake is installed on the system
66+
67+
## License
68+
69+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
70+
71+
## Contributing
72+
73+
Contributions are welcome! Please open an issue or submit a pull request.
74+
75+
## Contact
76+
77+
For any inquiries, please reach out to the author at [ltpp-universe <root@ltpp.vip>](mailto:root@ltpp.vip).

sh/publish.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
cargo publish --allow-dirty;

sh/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
cargo test -- --nocapture

src/cfg.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use crate::*;
2+
use std::{
3+
sync::Mutex,
4+
thread::{spawn, JoinHandle},
5+
time::Instant,
6+
};
7+
8+
#[test]
9+
fn test_http_post_request() {
10+
let mut request_builder = RequestBuilder::new().host("127.0.0.1").port(60000).build();
11+
request_builder
12+
.send("udp send".as_bytes())
13+
.and_then(|response| {
14+
println!("ResponseTrait => {}", response.text());
15+
Ok(())
16+
})
17+
.unwrap_or_else(|e| println!("Error => {}", e));
18+
}
19+
20+
#[test]
21+
fn test_readme_text() {
22+
let mut request_builder = RequestBuilder::new().host("127.0.0.1").port(60000).build();
23+
request_builder
24+
.send("udp send".as_bytes())
25+
.and_then(|response| {
26+
println!("ResponseTrait => {:?}", response.text());
27+
Ok(())
28+
})
29+
.unwrap_or_else(|e| println!("Error => {:?}", e));
30+
let mut request_builder = RequestBuilder::new().host("127.0.0.1").port(60000).build();
31+
request_builder
32+
.send("udp send".as_bytes())
33+
.and_then(|response| {
34+
println!("ResponseTrait => {:?}", response.text());
35+
Ok(())
36+
})
37+
.unwrap_or_else(|e| println!("Error => {:?}", e));
38+
}
39+
40+
#[test]
41+
fn test_readme_binary() {
42+
let mut request_builder = RequestBuilder::new().host("127.0.0.1").port(60000).build();
43+
request_builder
44+
.send("udp send".as_bytes())
45+
.and_then(|response| {
46+
println!("ResponseTrait => {:?}", response.text());
47+
Ok(())
48+
})
49+
.unwrap_or_else(|e| println!("Error => {:?}", e));
50+
}
51+
52+
#[test]
53+
fn test_thread_http_get_request() {
54+
let num_threads: i32 = 10;
55+
let mut handles: Vec<JoinHandle<()>> = Vec::new();
56+
let request_builder: Arc<Mutex<BoxRequestTrait>> = Arc::new(Mutex::new(
57+
RequestBuilder::new()
58+
.host("127.0.0.1")
59+
.port(60000)
60+
.timeout(10)
61+
.buffer(1_024_000)
62+
.build(),
63+
));
64+
for _ in 0..num_threads {
65+
let request_builder: Arc<
66+
Mutex<
67+
Box<
68+
dyn RequestTrait<
69+
RequestResult = Result<
70+
Box<dyn ResponseTrait<OutputText = String, OutputBinary = Vec<u8>>>,
71+
RequestError,
72+
>,
73+
>,
74+
>,
75+
>,
76+
> = Arc::clone(&request_builder);
77+
let handle: JoinHandle<()> = spawn(move || {
78+
let mut request_builder = request_builder.lock().unwrap();
79+
let start_time: Instant = Instant::now();
80+
match request_builder.send("test".as_bytes()) {
81+
Ok(response) => {
82+
let duration: std::time::Duration = start_time.elapsed();
83+
println!("{:?}", duration);
84+
let response_text = response.text();
85+
println!("ResponseTrait => {}", response_text);
86+
}
87+
Err(e) => {
88+
let duration: std::time::Duration = start_time.elapsed();
89+
println!("{:?}", duration);
90+
println!("Error => {}", e);
91+
}
92+
}
93+
});
94+
handles.push(handle);
95+
}
96+
97+
for handle in handles {
98+
handle.join().unwrap();
99+
}
100+
}

src/common/constant.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub static EMPTY_STR: &str = "";
2+
pub static DEFAULT_WEB_PORT: usize = 80;
3+
pub static SPLIT_REQUEST: &str = "\r\n\r\n";
4+
pub static SPLIT_REQUEST_BYTES: &[u8] = SPLIT_REQUEST.as_bytes();
5+
pub static DEFAULT_BUFFER_SIZE: usize = 512_000;
6+
pub const DEFAULT_TIMEOUT: u64 = u64::MAX;

0 commit comments

Comments
 (0)