Skip to content

jefftt/tokio-sctp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tokio-sctp

Crates.io Docs.rs

Non-blocking SCTP socket support for tokio. Currently only supports Linux

Example

use std::{env, error::Error};

use bytes::BytesMut;
use tokio_sctp::{InitMsg, SctpListener, SendOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let addrs = env::args()
        .nth(1)
        .unwrap_or_else(|| "127.0.0.1:8080".to_string())
        .split(",")
        .map(|s| s.parse().unwrap())
        .collect::<Vec<_>>();

    let listener = SctpListener::bindx(&addrs)?;
    listener.set_sctp_initmsg(&InitMsg {
        num_ostreams: 5,
        max_instreams: 5,
        max_attempts: 0,
        max_init_timeout: 0,
    })?;

    println!("Listening on: {:?}", addrs.as_slice());

    loop {
        let (socket, _) = listener.accept().await?;
        tokio::spawn(async move {
            let mut buf = BytesMut::with_capacity(1024);

            loop {
                let (n, _, _) = socket
                    .recvmsg_buf(&mut buf)
                    .await
                    .expect("failed to read data from socket");

                if n == 0 {
                    return;
                }

                socket
                    .sendmsg(&buf, None, &SendOptions::default())
                    .await
                    .expect("failed to write data to socket");
            }
        });
    }
}

About

tokio SCTP support

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages