summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/runtime_test.rs130
1 files changed, 85 insertions, 45 deletions
diff --git a/examples/runtime_test.rs b/examples/runtime_test.rs
index 2e8aecb..04b0654 100644
--- a/examples/runtime_test.rs
+++ b/examples/runtime_test.rs
@@ -1,3 +1,4 @@
+use futures::StreamExt;
use pcap::Packet as PacketPacp;
use std::borrow::BorrowMut;
use std::cell::RefCell;
@@ -5,7 +6,7 @@ use std::rc::Rc;
use stellar_rs::session::session::Session;
use stellar_rs::event::executor::{Executor, EX};
-use stellar_rs::packet::capture::PacketCapture;
+use stellar_rs::packet::capture::{BoxCodec, PacketCapture};
use stellar_rs::packet::packet::Packet;
use stellar_rs::plugin::example::ExamplePulgin;
@@ -57,60 +58,99 @@ fn main() {
// // H
// }
// }
-
async fn test1() {
- let cap = pacp_init("eth0");
+ let cap = PacketCapture::new("eth0");
+
+ let mut stream = cap.test(BoxCodec);
+
loop {
- let (packet, session) = test2(cap.clone()).await;
- // plug_entry(packet, session);
-
- let a = Executor::spawn(async move {
- ExamplePulgin::new(
- "Example Plugin",
- Rc::new(RefCell::new(packet)),
- session.clone(),
- )
- .await
- });
+ let temp = stream.next().await.unwrap();
+
+ let packet_pacp = temp.unwrap();
+
+ // let (packet, session_rc) = packet_pacp2packet(packet_pacp);
+
+ let mut packet = Packet::new(&packet_pacp, packet_pacp.len().try_into().unwrap());
+ let result: Result<
+ Option<Rc<RefCell<stellar_rs::session::session::Session>>>,
+ stellar_rs::packet::error::PacketError,
+ > = packet.handle();
+ // let packet = packet.unwrap(); // 一定有值
}
+
+ // stream.for_each(|packet_pacp| async {
+ // let (packet, session) = packet_pacp2packet(packet_pacp);
+ // let packet = packet.unwrap(); // 一定有值
+
+ // ExamplePulgin::new(
+ // "Example Plugin",
+ // Rc::new(RefCell::new(packet)),
+ // session.clone(),
+ // )
+ // .await;
+ // })
+
+ // let s = stream! {
+ // let cap = PacketCapture::new("eth0");
+ // // while let Ok(packet_pacp) = cap.borrow_mut().capture.next_packet() {
+ // // println!("\n==================== New Packet ====================\n");
+ // // println!("Packet->header : {:?}", packet_pacp.header);
+ // // println!("Packet->data : {:?}", packet_pacp.data);
+ // // yield packet_pacp;
+ // // }
+ // };
+
+ // loop {
+ // let (packet, session) = test2(cap.clone()).await;
+ // // plug_entry(packet, session);
+
+ // let a = Executor::spawn(async move {
+ // ExamplePulgin::new(
+ // "Example Plugin",
+ // Rc::new(RefCell::new(packet)),
+ // session.clone(),
+ // )
+ // .await
+ // });
+ // }
}
-async fn test2<'a>(cap: Rc<RefCell<PacketCapture>>) -> (Packet<'a>, Option<Rc<RefCell<Session>>>) {
- // let mut cap = pacp_init("eth0");
+// async fn test2<'a>(cap: Rc<RefCell<PacketCapture>>) -> (Packet<'a>, Option<Rc<RefCell<Session>>>) {
+// // let mut cap = pacp_init("eth0");
- let cap = unsafe { &(*cap.borrow()) };
+// let cap = &(*cap.borrow());
- // 使用 unsafe 强制解引用为 &mut PacketCapture
- // let cap_ptr: *mut RefCell<PacketCapture> = Rc::into_raw(cap);
- // let cap_ref: &mut RefCell<PacketCapture> = unsafe { &mut *cap_ptr };
- // let packet_capture: &mut PacketCapture = cap_ref.get_mut().unwrap();
+// // 使用 unsafe 强制解引用为 &mut PacketCapture
+// // let cap_ptr: *mut RefCell<PacketCapture> = Rc::into_raw(cap);
+// // let cap_ref: &mut RefCell<PacketCapture> = unsafe { &mut *cap_ptr };
+// // let packet_capture: &mut PacketCapture = cap_ref.get_mut().unwrap();
- let packet_pacp: PacketPacp = capture_packet(cap).unwrap();
+// let packet_pacp: PacketPacp = capture_packet(cap).unwrap();
- let (packet, session_rc) = packet_pacp2packet(packet_pacp);
- let packet = packet.unwrap(); // 一定有值
- return (packet, session_rc);
-}
+// let (packet, session_rc) = packet_pacp2packet(packet_pacp);
+// let packet = packet.unwrap(); // 一定有值
+// return (packet, session_rc);
+// }
-fn pacp_init(device: &str) -> Rc<RefCell<PacketCapture>> {
- PacketCapture::show_devices();
- let cap = PacketCapture::new(device);
- return Rc::new(RefCell::new(cap));
-}
+// fn pacp_init(device: &str) -> Rc<RefCell<PacketCapture>> {
+// PacketCapture::show_devices();
+// let cap = PacketCapture::new(device);
+// return Rc::new(RefCell::new(cap));
+// }
-fn capture_packet<'a>(cap: &PacketCapture) -> Option<PacketPacp<'a>> {
- let cap_mut_ref: &mut PacketCapture = unsafe {
- // 在 unsafe 块中将不可变引用转换为可变引用
- &mut *(cap as *const PacketCapture as *mut PacketCapture)
- };
-
- while let Ok(packet_pacp) = cap_mut_ref.capture.next_packet() {
- println!("\n==================== New Packet ====================\n");
- println!("Packet->header : {:?}", packet_pacp.header);
- println!("Packet->data : {:?}", packet_pacp.data);
- return Some(packet_pacp);
- }
- None
-}
+// fn capture_packet<'a>(cap: &PacketCapture) -> Option<PacketPacp<'a>> {
+// let cap_mut_ref: &mut PacketCapture = unsafe {
+// // 在 unsafe 块中将不可变引用转换为可变引用
+// &mut *(cap as *const PacketCapture as *mut PacketCapture)
+// };
+
+// while let Ok(packet_pacp) = cap_mut_ref.capture.next_packet() {
+// println!("\n==================== New Packet ====================\n");
+// println!("Packet->header : {:?}", packet_pacp.header);
+// println!("Packet->data : {:?}", packet_pacp.data);
+// return Some(packet_pacp);
+// }
+// None
+// }
fn packet_pacp2packet<'a>(
packet_pacp: PacketPacp<'a>,