changed the connection state
This commit is contained in:
parent
25f15830fc
commit
538dea12f8
10
src/lib.rs
10
src/lib.rs
@ -21,12 +21,20 @@ use sdlan_sn_rs::{
|
||||
utils::{create_or_load_uuid, get_sdlan_sock_from_socketaddr, Result, SDLanError},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(u8)]
|
||||
pub enum ConnectionState {
|
||||
NotConnected = 0,
|
||||
Connecting = 1,
|
||||
Connected = 2,
|
||||
}
|
||||
|
||||
pub async fn run_sdlan(
|
||||
args: CommandLine,
|
||||
sender: std::sync::mpsc::Sender<bool>,
|
||||
install_channel: &str,
|
||||
|
||||
connecting_chan: Option<Sender<bool>>
|
||||
connecting_chan: Option<Sender<ConnectionState>>
|
||||
// start_stop_sender: Sender<String>,
|
||||
// start_stop_receiver: Receiver<String>,
|
||||
) -> Result<()> {
|
||||
|
||||
@ -10,6 +10,7 @@ use crate::pb::{
|
||||
};
|
||||
use crate::tcp::{init_tcp_conn, EventType, NakMsgCode, PacketType, SdlanTcp};
|
||||
use crate::utils::{send_to_sock, CommandLine};
|
||||
use crate::ConnectionState;
|
||||
use etherparse::IpHeaders;
|
||||
use sdlan_sn_rs::config::{AF_INET, SDLAN_DEFAULT_TTL};
|
||||
use sdlan_sn_rs::peer::SdlanSock;
|
||||
@ -213,7 +214,7 @@ pub async fn async_main(
|
||||
args: CommandLine,
|
||||
start_stop_chan: Receiver<StartStopInfo>,
|
||||
cancel: CancellationToken,
|
||||
connecting_chan: Option<Sender<bool>>,
|
||||
connecting_chan: Option<Sender<ConnectionState>>,
|
||||
) -> Result<()> {
|
||||
// let _ = PidRecorder::new(".pid");
|
||||
|
||||
|
||||
@ -22,6 +22,7 @@ use tracing::error;
|
||||
use crate::config::TCP_PING_TIME;
|
||||
use crate::network::StartStopInfo;
|
||||
use crate::tcp::read_a_packet;
|
||||
use crate::ConnectionState;
|
||||
|
||||
use super::tcp_codec::SdlanTcp;
|
||||
|
||||
@ -36,7 +37,7 @@ pub struct ReadWriteActor {
|
||||
// actor收到数据之后,发送给上层的发送端口,接收端由handle保存
|
||||
from_tcp: Sender<SdlanTcp>,
|
||||
cancel: CancellationToken,
|
||||
connecting_chan: Option<Sender<bool>>,
|
||||
connecting_chan: Option<Sender<ConnectionState>>,
|
||||
}
|
||||
|
||||
impl ReadWriteActor {
|
||||
@ -46,7 +47,7 @@ impl ReadWriteActor {
|
||||
from_tcp: Sender<SdlanTcp>,
|
||||
connected: Arc<AtomicBool>,
|
||||
pong_time: Arc<AtomicU64>,
|
||||
connecting_chan: Option<Sender<bool>>,
|
||||
connecting_chan: Option<Sender<ConnectionState>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
// to_tcp,
|
||||
@ -76,6 +77,9 @@ impl ReadWriteActor {
|
||||
let mut started = false;
|
||||
let mut start_pkt_id = None;
|
||||
loop {
|
||||
if let Some(ref connecting_chan) = self.connecting_chan {
|
||||
let _ = connecting_chan.send(ConnectionState::NotConnected).await;
|
||||
}
|
||||
self.connected.store(false, Ordering::Relaxed);
|
||||
if !started {
|
||||
// println!("waiting for start");
|
||||
@ -108,10 +112,11 @@ impl ReadWriteActor {
|
||||
debug!("start stop chan recv none");
|
||||
continue;
|
||||
}
|
||||
debug!("try connecting...");
|
||||
|
||||
if let Some(ref connecting_chan) = self.connecting_chan {
|
||||
let _ = connecting_chan.send(true).await;
|
||||
let _ = connecting_chan.send(ConnectionState::Connecting).await;
|
||||
}
|
||||
debug!("try connecting...");
|
||||
let Ok(mut stream) = TcpStream::connect(&self.remote).await else {
|
||||
self.connected.store(false, Ordering::Relaxed);
|
||||
if keep_reconnect {
|
||||
@ -131,7 +136,7 @@ impl ReadWriteActor {
|
||||
on_connected(&mut stream, start_pkt_id.take()).await;
|
||||
|
||||
if let Some(ref connecting_chan) = self.connecting_chan {
|
||||
let _ = connecting_chan.send(false).await;
|
||||
let _ = connecting_chan.send(ConnectionState::Connected).await;
|
||||
}
|
||||
// stream.write("hello".as_bytes()).await;
|
||||
let (reader, mut write) = stream.into_split();
|
||||
@ -251,7 +256,7 @@ impl ReadWriterHandle {
|
||||
pong_time: Arc<AtomicU64>,
|
||||
start_stop_chan: Receiver<StartStopInfo>,
|
||||
// cancel: CancellationToken,
|
||||
connecting_chan: Option<Sender<bool>>,
|
||||
connecting_chan: Option<Sender<ConnectionState>>,
|
||||
) -> Self
|
||||
where
|
||||
T: for<'b> Fn(&'b mut TcpStream, Option<u32>) -> BoxFuture<'b, ()> + Send + 'static,
|
||||
@ -298,7 +303,7 @@ pub fn init_tcp_conn<'a, T, T3, T2, F, F2>(
|
||||
pong_time: Arc<AtomicU64>,
|
||||
// cancel: CancellationToken,
|
||||
start_stop_chan: Receiver<StartStopInfo>,
|
||||
connecting_chan: Option<Sender<bool>>
|
||||
connecting_chan: Option<Sender<ConnectionState>>
|
||||
) where
|
||||
T: for<'b> Fn(&'b mut TcpStream, Option<u32>) -> BoxFuture<'b, ()> + Send + 'static,
|
||||
T3: Fn() -> F2 + Send + 'static,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user