forked from nthieu173/srt-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
They are a straight port from the libsrt capi examples. Refer to nthieu173#2.
- Loading branch information
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |