KcpPacket

Struct KcpPacket 

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

Implementations§

Source§

impl KcpPacket

Source

pub fn new(body_size: usize) -> Self

Source

pub fn new_with_payload(payload: &[u8]) -> Self

Source

pub fn mut_header(&mut self) -> &mut KcpPacketHeader

Source

pub fn header(&self) -> &KcpPacketHeader

Source

pub fn payload(&self) -> &[u8]

Source

pub fn inner(self) -> BytesMut

Examples found in repository?
examples/udp_server.rs (line 22)
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 26)
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 len(&self) -> usize

Trait Implementations§

Source§

impl Clone for KcpPacket

Source§

fn clone(&self) -> KcpPacket

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KcpPacket

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for KcpPacket

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&KcpPacket> for ConnId

Source§

fn from(packet: &KcpPacket) -> Self

Converts to this type from the input type.
Source§

impl From<BytesMut> for KcpPacket

Source§

fn from(inner: BytesMut) -> Self

Converts to this type from the input type.
Source§

impl Into<Bytes> for KcpPacket

Source§

fn into(self) -> Bytes

Converts this type into the (usually inferred) input type.
Source§

impl Into<BytesMut> for KcpPacket

Source§

fn into(self) -> BytesMut

Converts this type into the (usually inferred) input type.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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