KcpEndpoint

Struct KcpEndpoint 

Source
pub struct KcpEndpoint { /* private fields */ }

Implementations§

Source§

impl KcpEndpoint

Source

pub fn new() -> Self

Examples found in repository?
examples/udp_server.rs (line 11)
10async fn main() {
11    let mut endpoint = KcpEndpoint::new();
12    endpoint.run().await;
13
14    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
15
16    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54321").await.unwrap());
17    udp_socket.connect("127.0.0.1:54320").await.unwrap();
18
19    let udp = udp_socket.clone();
20    tokio::spawn(async move {
21        while let Some(data) = output.recv().await {
22            udp.send(&data.inner()).await.unwrap();
23        }
24    });
25
26    let udp = udp_socket.clone();
27    tokio::spawn(async move {
28        loop {
29            let mut buf = vec![0; 1024];
30            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
31            input
32                .send(BytesMut::from(&buf[..size]).into())
33                .await
34                .unwrap();
35        }
36    });
37
38    loop {
39        let conn_id = endpoint.accept().await.unwrap();
40        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
41
42        let mut buf = vec![0; 64 * 1024];
43        let size = kcp_stream.read(&mut buf).await.unwrap();
44        println!("server recv {}", String::from_utf8_lossy(&buf[..size]));
45
46        kcp_stream.write_all(&buf[..size]).await.unwrap();
47        kcp_stream.flush().await.unwrap();
48    }
49}
More examples
Hide additional examples
examples/udp_client.rs (line 15)
14async fn main() {
15    let mut endpoint = KcpEndpoint::new();
16    endpoint.run().await;
17
18    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
19
20    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54320").await.unwrap());
21    udp_socket.connect("127.0.0.1:54321").await.unwrap();
22
23    let udp = udp_socket.clone();
24    tokio::spawn(async move {
25        while let Some(data) = output.recv().await {
26            udp.send(&data.inner()).await.unwrap();
27        }
28    });
29
30    let udp = udp_socket.clone();
31    tokio::spawn(async move {
32        loop {
33            let mut buf = vec![0; 1024];
34            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
35            input
36                .send(BytesMut::from(&buf[..size]).into())
37                .await
38                .unwrap();
39        }
40    });
41
42    loop {
43        let conn_id = endpoint
44            .connect(Duration::from_secs(1), 0, 0, Bytes::new())
45            .await
46            .unwrap();
47
48        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
49        kcp_stream.write_all(b"hello world").await.unwrap();
50        kcp_stream.flush().await.unwrap();
51
52        let mut buf = vec![0; 64 * 1024];
53        let size = kcp_stream.read(&mut buf).await.unwrap();
54
55        println!("{}", String::from_utf8_lossy(&buf[..size]));
56        tokio::time::sleep(Duration::from_secs(1)).await;
57    }
58}
Source

pub fn set_kcp_config_factory(&mut self, factory: KcpConfigFactory)

Source

pub async fn run(&mut self)

Examples found in repository?
examples/udp_server.rs (line 12)
10async fn main() {
11    let mut endpoint = KcpEndpoint::new();
12    endpoint.run().await;
13
14    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
15
16    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54321").await.unwrap());
17    udp_socket.connect("127.0.0.1:54320").await.unwrap();
18
19    let udp = udp_socket.clone();
20    tokio::spawn(async move {
21        while let Some(data) = output.recv().await {
22            udp.send(&data.inner()).await.unwrap();
23        }
24    });
25
26    let udp = udp_socket.clone();
27    tokio::spawn(async move {
28        loop {
29            let mut buf = vec![0; 1024];
30            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
31            input
32                .send(BytesMut::from(&buf[..size]).into())
33                .await
34                .unwrap();
35        }
36    });
37
38    loop {
39        let conn_id = endpoint.accept().await.unwrap();
40        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
41
42        let mut buf = vec![0; 64 * 1024];
43        let size = kcp_stream.read(&mut buf).await.unwrap();
44        println!("server recv {}", String::from_utf8_lossy(&buf[..size]));
45
46        kcp_stream.write_all(&buf[..size]).await.unwrap();
47        kcp_stream.flush().await.unwrap();
48    }
49}
More examples
Hide additional examples
examples/udp_client.rs (line 16)
14async fn main() {
15    let mut endpoint = KcpEndpoint::new();
16    endpoint.run().await;
17
18    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
19
20    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54320").await.unwrap());
21    udp_socket.connect("127.0.0.1:54321").await.unwrap();
22
23    let udp = udp_socket.clone();
24    tokio::spawn(async move {
25        while let Some(data) = output.recv().await {
26            udp.send(&data.inner()).await.unwrap();
27        }
28    });
29
30    let udp = udp_socket.clone();
31    tokio::spawn(async move {
32        loop {
33            let mut buf = vec![0; 1024];
34            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
35            input
36                .send(BytesMut::from(&buf[..size]).into())
37                .await
38                .unwrap();
39        }
40    });
41
42    loop {
43        let conn_id = endpoint
44            .connect(Duration::from_secs(1), 0, 0, Bytes::new())
45            .await
46            .unwrap();
47
48        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
49        kcp_stream.write_all(b"hello world").await.unwrap();
50        kcp_stream.flush().await.unwrap();
51
52        let mut buf = vec![0; 64 * 1024];
53        let size = kcp_stream.read(&mut buf).await.unwrap();
54
55        println!("{}", String::from_utf8_lossy(&buf[..size]));
56        tokio::time::sleep(Duration::from_secs(1)).await;
57    }
58}
Source

pub fn output_receiver(&mut self) -> Option<KcpPacketReceiver>

Examples found in repository?
examples/udp_server.rs (line 14)
10async fn main() {
11    let mut endpoint = KcpEndpoint::new();
12    endpoint.run().await;
13
14    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
15
16    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54321").await.unwrap());
17    udp_socket.connect("127.0.0.1:54320").await.unwrap();
18
19    let udp = udp_socket.clone();
20    tokio::spawn(async move {
21        while let Some(data) = output.recv().await {
22            udp.send(&data.inner()).await.unwrap();
23        }
24    });
25
26    let udp = udp_socket.clone();
27    tokio::spawn(async move {
28        loop {
29            let mut buf = vec![0; 1024];
30            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
31            input
32                .send(BytesMut::from(&buf[..size]).into())
33                .await
34                .unwrap();
35        }
36    });
37
38    loop {
39        let conn_id = endpoint.accept().await.unwrap();
40        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
41
42        let mut buf = vec![0; 64 * 1024];
43        let size = kcp_stream.read(&mut buf).await.unwrap();
44        println!("server recv {}", String::from_utf8_lossy(&buf[..size]));
45
46        kcp_stream.write_all(&buf[..size]).await.unwrap();
47        kcp_stream.flush().await.unwrap();
48    }
49}
More examples
Hide additional examples
examples/udp_client.rs (line 18)
14async fn main() {
15    let mut endpoint = KcpEndpoint::new();
16    endpoint.run().await;
17
18    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
19
20    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54320").await.unwrap());
21    udp_socket.connect("127.0.0.1:54321").await.unwrap();
22
23    let udp = udp_socket.clone();
24    tokio::spawn(async move {
25        while let Some(data) = output.recv().await {
26            udp.send(&data.inner()).await.unwrap();
27        }
28    });
29
30    let udp = udp_socket.clone();
31    tokio::spawn(async move {
32        loop {
33            let mut buf = vec![0; 1024];
34            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
35            input
36                .send(BytesMut::from(&buf[..size]).into())
37                .await
38                .unwrap();
39        }
40    });
41
42    loop {
43        let conn_id = endpoint
44            .connect(Duration::from_secs(1), 0, 0, Bytes::new())
45            .await
46            .unwrap();
47
48        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
49        kcp_stream.write_all(b"hello world").await.unwrap();
50        kcp_stream.flush().await.unwrap();
51
52        let mut buf = vec![0; 64 * 1024];
53        let size = kcp_stream.read(&mut buf).await.unwrap();
54
55        println!("{}", String::from_utf8_lossy(&buf[..size]));
56        tokio::time::sleep(Duration::from_secs(1)).await;
57    }
58}
Source

pub fn input_sender(&self) -> KcpPakcetSender

Examples found in repository?
examples/udp_server.rs (line 14)
10async fn main() {
11    let mut endpoint = KcpEndpoint::new();
12    endpoint.run().await;
13
14    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
15
16    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54321").await.unwrap());
17    udp_socket.connect("127.0.0.1:54320").await.unwrap();
18
19    let udp = udp_socket.clone();
20    tokio::spawn(async move {
21        while let Some(data) = output.recv().await {
22            udp.send(&data.inner()).await.unwrap();
23        }
24    });
25
26    let udp = udp_socket.clone();
27    tokio::spawn(async move {
28        loop {
29            let mut buf = vec![0; 1024];
30            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
31            input
32                .send(BytesMut::from(&buf[..size]).into())
33                .await
34                .unwrap();
35        }
36    });
37
38    loop {
39        let conn_id = endpoint.accept().await.unwrap();
40        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
41
42        let mut buf = vec![0; 64 * 1024];
43        let size = kcp_stream.read(&mut buf).await.unwrap();
44        println!("server recv {}", String::from_utf8_lossy(&buf[..size]));
45
46        kcp_stream.write_all(&buf[..size]).await.unwrap();
47        kcp_stream.flush().await.unwrap();
48    }
49}
More examples
Hide additional examples
examples/udp_client.rs (line 18)
14async fn main() {
15    let mut endpoint = KcpEndpoint::new();
16    endpoint.run().await;
17
18    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
19
20    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54320").await.unwrap());
21    udp_socket.connect("127.0.0.1:54321").await.unwrap();
22
23    let udp = udp_socket.clone();
24    tokio::spawn(async move {
25        while let Some(data) = output.recv().await {
26            udp.send(&data.inner()).await.unwrap();
27        }
28    });
29
30    let udp = udp_socket.clone();
31    tokio::spawn(async move {
32        loop {
33            let mut buf = vec![0; 1024];
34            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
35            input
36                .send(BytesMut::from(&buf[..size]).into())
37                .await
38                .unwrap();
39        }
40    });
41
42    loop {
43        let conn_id = endpoint
44            .connect(Duration::from_secs(1), 0, 0, Bytes::new())
45            .await
46            .unwrap();
47
48        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
49        kcp_stream.write_all(b"hello world").await.unwrap();
50        kcp_stream.flush().await.unwrap();
51
52        let mut buf = vec![0; 64 * 1024];
53        let size = kcp_stream.read(&mut buf).await.unwrap();
54
55        println!("{}", String::from_utf8_lossy(&buf[..size]));
56        tokio::time::sleep(Duration::from_secs(1)).await;
57    }
58}
Source

pub fn input_sender_ref(&self) -> &KcpPakcetSender

Source

pub fn conn_sender_receiver( &self, conn_id: ConnId, ) -> Option<(KcpStreamSender, KcpStreamReceiver)>

Source

pub fn conn_data(&self, conn_id: &ConnId) -> Option<Bytes>

Source

pub async fn connect( &self, timeout_dur: Duration, src_session_id: u32, dst_session_id: u32, conn_data: Bytes, ) -> Result<ConnId, Error>

Examples found in repository?
examples/udp_client.rs (line 44)
14async fn main() {
15    let mut endpoint = KcpEndpoint::new();
16    endpoint.run().await;
17
18    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
19
20    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54320").await.unwrap());
21    udp_socket.connect("127.0.0.1:54321").await.unwrap();
22
23    let udp = udp_socket.clone();
24    tokio::spawn(async move {
25        while let Some(data) = output.recv().await {
26            udp.send(&data.inner()).await.unwrap();
27        }
28    });
29
30    let udp = udp_socket.clone();
31    tokio::spawn(async move {
32        loop {
33            let mut buf = vec![0; 1024];
34            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
35            input
36                .send(BytesMut::from(&buf[..size]).into())
37                .await
38                .unwrap();
39        }
40    });
41
42    loop {
43        let conn_id = endpoint
44            .connect(Duration::from_secs(1), 0, 0, Bytes::new())
45            .await
46            .unwrap();
47
48        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
49        kcp_stream.write_all(b"hello world").await.unwrap();
50        kcp_stream.flush().await.unwrap();
51
52        let mut buf = vec![0; 64 * 1024];
53        let size = kcp_stream.read(&mut buf).await.unwrap();
54
55        println!("{}", String::from_utf8_lossy(&buf[..size]));
56        tokio::time::sleep(Duration::from_secs(1)).await;
57    }
58}
Source

pub async fn accept(&self) -> Result<ConnId, Error>

Examples found in repository?
examples/udp_server.rs (line 39)
10async fn main() {
11    let mut endpoint = KcpEndpoint::new();
12    endpoint.run().await;
13
14    let (input, mut output) = (endpoint.input_sender(), endpoint.output_receiver().unwrap());
15
16    let udp_socket = Arc::new(UdpSocket::bind("0.0.0.0:54321").await.unwrap());
17    udp_socket.connect("127.0.0.1:54320").await.unwrap();
18
19    let udp = udp_socket.clone();
20    tokio::spawn(async move {
21        while let Some(data) = output.recv().await {
22            udp.send(&data.inner()).await.unwrap();
23        }
24    });
25
26    let udp = udp_socket.clone();
27    tokio::spawn(async move {
28        loop {
29            let mut buf = vec![0; 1024];
30            let (size, _) = udp.recv_from(&mut buf).await.unwrap();
31            input
32                .send(BytesMut::from(&buf[..size]).into())
33                .await
34                .unwrap();
35        }
36    });
37
38    loop {
39        let conn_id = endpoint.accept().await.unwrap();
40        let mut kcp_stream = KcpStream::new(&endpoint, conn_id).unwrap();
41
42        let mut buf = vec![0; 64 * 1024];
43        let size = kcp_stream.read(&mut buf).await.unwrap();
44        println!("server recv {}", String::from_utf8_lossy(&buf[..size]));
45
46        kcp_stream.write_all(&buf[..size]).await.unwrap();
47        kcp_stream.flush().await.unwrap();
48    }
49}

Trait Implementations§

Source§

impl Debug for KcpEndpoint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more