-
Notifications
You must be signed in to change notification settings - Fork 21
Add UDP Packet Mode endpoint #82
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
base: main
Are you sure you want to change the base?
Changes from all commits
1644b6d
6a9a2a0
c13c127
51328fa
3d05550
77f0261
6f59da9
e2b5208
ea826cf
9cc591b
f58e082
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -136,6 +136,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||
| log::info!("#{number1} TCP closed, session count {c}"); | ||||||
| }); | ||||||
| } | ||||||
| #[cfg(not(feature = "udp_packet"))] | ||||||
| IpStackStream::Udp(mut udp) => { | ||||||
| let mut s = match UdpStream::connect(server_addr).await { | ||||||
| Ok(s) => s, | ||||||
|
|
@@ -159,6 +160,50 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||
| log::info!("#{number2} UDP closed, session count {c}"); | ||||||
| }); | ||||||
| } | ||||||
| #[cfg(feature = "udp_packet")] | ||||||
| IpStackStream::Udp(mut endpoint) => { | ||||||
| let c = count.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; | ||||||
| let number2 = number; | ||||||
| log::info!("#{number2} UDP Packet Endpoint starting, session count {c}"); | ||||||
|
|
||||||
| tokio::spawn(async move { | ||||||
| loop { | ||||||
| tokio::select! { | ||||||
| res = endpoint.recv() => { | ||||||
| match res { | ||||||
| Some((_src_addr, _dst_addr, _payload)) => { | ||||||
|
|
||||||
|
|
||||||
| } | ||||||
| None => { | ||||||
| log::info!("#{number2} UDP Packet Endpoint 底层通道已关闭"); | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| // res = app.readpacket() => { | ||||||
| // match res { | ||||||
| // Ok(Some((remote_player_addr, my_local_addr, payload))) => { | ||||||
| // log::trace!("#{number2} [down] {} -> {} ({} bytes)", remote_player_addr, my_local_addr, payload.len()); | ||||||
|
|
||||||
| // | ||||||
| // if let Err(e) = endpoint.send(remote_player_addr, my_local_addr, payload) { | ||||||
| // log::warn!("#{number2} faild to send packet: {}", e); | ||||||
|
||||||
| // log::warn!("#{number2} faild to send packet: {}", e); | |
| // log::warn!("#{number2} failed to send packet: {}", e); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; | |
|
|
||
| pub use self::tcp::IpStackTcpStream; | ||
| pub use self::tcp::{TcpConfig, TcpOptions}; | ||
| #[cfg(feature = "udp_packet")] | ||
| pub use self::udp::IpStackUdpPacketEndpoint; | ||
|
|
||
| pub use self::udp::IpStackUdpStream; | ||
| pub use self::unknown::IpStackUnknownTransport; | ||
|
|
||
| mod seqnum; | ||
| mod tcb; | ||
| mod tcp; | ||
|
|
@@ -26,7 +28,11 @@ pub enum IpStackStream { | |
| /// A TCP connection stream. | ||
| Tcp(IpStackTcpStream), | ||
| /// A UDP stream. | ||
| #[cfg(not(feature = "udp_packet"))] | ||
| Udp(IpStackUdpStream), | ||
| /// UDP PACKET. | ||
| #[cfg(feature = "udp_packet")] | ||
| Udp(IpStackUdpPacketEndpoint), | ||
| /// A stream for unknown transport protocols. | ||
|
Comment on lines
32
to
36
|
||
| UnknownTransport(IpStackUnknownTransport), | ||
| /// Raw network packets that couldn't be parsed. | ||
|
|
@@ -51,7 +57,10 @@ impl IpStackStream { | |
| pub fn local_addr(&self) -> SocketAddr { | ||
| match self { | ||
| IpStackStream::Tcp(tcp) => tcp.local_addr(), | ||
| #[cfg(not(feature = "udp_packet"))] | ||
| IpStackStream::Udp(udp) => udp.local_addr(), | ||
| #[cfg(feature = "udp_packet")] | ||
| IpStackStream::Udp(udp_edp) => udp_edp.local_addr(), | ||
| IpStackStream::UnknownNetwork(_) => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)), | ||
Uxx0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| IpStackStream::UnknownTransport(unknown) => match unknown.src_addr() { | ||
| IpAddr::V4(addr) => SocketAddr::V4(SocketAddrV4::new(addr, 0)), | ||
|
|
@@ -77,7 +86,10 @@ impl IpStackStream { | |
| pub fn peer_addr(&self) -> SocketAddr { | ||
| match self { | ||
| IpStackStream::Tcp(tcp) => tcp.peer_addr(), | ||
| #[cfg(not(feature = "udp_packet"))] | ||
| IpStackStream::Udp(udp) => udp.peer_addr(), | ||
| #[cfg(feature = "udp_packet")] | ||
| IpStackStream::Udp(_) => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)), | ||
| IpStackStream::UnknownNetwork(_) => SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)), | ||
| IpStackStream::UnknownTransport(unknown) => match unknown.dst_addr() { | ||
| IpAddr::V4(addr) => SocketAddr::V4(SocketAddrV4::new(addr, 0)), | ||
|
|
@@ -89,6 +101,7 @@ impl IpStackStream { | |
| pub(crate) fn stream_sender(&self) -> Result<crate::PacketSender, std::io::Error> { | ||
| match self { | ||
| IpStackStream::Tcp(tcp) => Ok(tcp.stream_sender()), | ||
| #[cfg(not(feature = "udp_packet"))] | ||
| IpStackStream::Udp(udp) => Ok(udp.stream_sender()), | ||
| _ => Err(std::io::Error::other("Unknown transport stream does not have a sender")), | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This log line includes non-English text ("底层通道已关闭"), while the rest of the repository examples/logs are in English. Consider replacing it with an English message so the example output is consistent and understandable for all users.