Skip to content

Commit

Permalink
Add -sys examples
Browse files Browse the repository at this point in the history
They are a straight port from the libsrt capi examples.

Refer to nthieu173#2.
  • Loading branch information
lu-zero committed Dec 17, 2020
1 parent 560a51a commit 7a27653
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libsrt-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ repository = "https://github.com/nthieu173/srt-rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dev-dependencies]
os_socketaddr = "0.1.1"

[build-dependencies]
cmake = "0.1"
bindgen = "0.56"
84 changes: 84 additions & 0 deletions libsrt-sys/examples/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use libsrt_sys::*;

use os_socketaddr::OsSocketAddr;

use std::ffi::c_void;
use std::ffi::CStr;
use std::mem::size_of_val;
use std::net::SocketAddr;

fn error_to_str() -> &'static str {
unsafe {
let s = srt_getlasterror_str();
CStr::from_ptr(s).to_str().unwrap()
}
}

fn main() {
let mut args = std::env::args();

if args.len() < 2 {
eprintln!(
"Usage: {} <remote host>:<remote port>",
args.next().unwrap()
);
}

let _bin = args.next().unwrap();

let remote = args.next().unwrap();

let message = "This message should be sent to the other side".as_bytes();

let addr: SocketAddr = remote.parse().expect("Invalid addr:port syntax");

let os_addr: OsSocketAddr = addr.into();

let yes = 1u32;

unsafe {
srt_startup();
}

let ss = unsafe { srt_create_socket() };
assert_ne!(ss, SRT_ERROR, "create_socket: {}", error_to_str());

unsafe {
srt_setsockflag(
ss,
SRT_SOCKOPT::SRTO_SENDER,
&yes as *const u32 as *const c_void,
size_of_val(&yes) as i32,
);
}

let st = unsafe {
srt_connect(
ss,
os_addr.as_ptr() as *const sockaddr,
os_addr.len() as i32,
)
};
assert_ne!(st, SRT_ERROR, "connect {}", error_to_str());
for i in 0..100 {
println!("Sending message {}", i);
let st = unsafe {
srt_sendmsg2(
ss,
message.as_ptr() as *const i8,
message.len() as i32,
std::ptr::null_mut(),
)
};
assert_ne!(st, SRT_ERROR, "sendmsg2 {}", error_to_str());

std::thread::sleep(std::time::Duration::from_millis(100));
}

let st = unsafe { srt_close(ss) };
assert_ne!(st, SRT_ERROR, "close {}", error_to_str());

unsafe {
srt_cleanup();
}
}
84 changes: 84 additions & 0 deletions libsrt-sys/examples/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use libsrt_sys::*;

use os_socketaddr::OsSocketAddr;

use std::ffi::{c_void, CStr};
use std::mem::size_of_val;
use std::net::SocketAddr;

fn error_to_str() -> &'static str {
unsafe {
let s = srt_getlasterror_str();
CStr::from_ptr(s).to_str().unwrap()
}
}

fn main() {
let mut args = std::env::args();

if args.len() < 2 {
eprintln!(
"Usage: {} <remote host>:<remote port>",
args.next().unwrap()
);
}

let _bin = args.next().unwrap();

let remote = args.next().unwrap();

let addr: SocketAddr = remote.parse().expect("Invalid addr:port syntax");
let mut os_their_addr = OsSocketAddr::new();

let os_addr: OsSocketAddr = addr.into();

let yes = 1u32;

unsafe {
srt_startup();
}

let ss = unsafe { srt_create_socket() };
assert_ne!(ss, SRT_ERROR, "create_socket {}", error_to_str());

unsafe {
srt_setsockflag(
ss,
SRT_SOCKOPT::SRTO_RCVSYN,
&yes as *const u32 as *const c_void,
size_of_val(&yes) as i32,
);
}

let st = unsafe {
srt_bind(
ss,
os_addr.as_ptr() as *const sockaddr,
os_addr.len() as i32,
)
};
assert_ne!(st, SRT_ERROR);

let st = unsafe { srt_listen(ss, 2) };
assert_ne!(st, SRT_ERROR, "listen {}", error_to_str());
let mut len = os_their_addr.capacity() as i32;
let their_fd = unsafe { srt_accept(ss, os_their_addr.as_mut_ptr() as *mut sockaddr, &mut len) };
assert_ne!(their_fd, SRT_ERROR, "accept {}", error_to_str());

for _ in 0..100 {
let mut msg = [0u8; 2048];
let st = unsafe { srt_recvmsg(their_fd, msg.as_mut_ptr() as *mut i8, msg.len() as i32) };
assert_ne!(st, SRT_ERROR, "recvmsg {}", error_to_str());

let s = std::str::from_utf8(&msg[..st as usize]).expect("Malformed message");

println!("Got msg of len {} << {:?}", st, s);
}

let st = unsafe { srt_close(ss) };
assert_ne!(st, SRT_ERROR, "close {}", error_to_str());

unsafe {
srt_cleanup();
}
}

0 comments on commit 7a27653

Please sign in to comment.