use std::io::{Read,Write};
use std::net::{TcpListener, TcpStream};
use crate::clib::*;
- pub fn server() -> Result<(), std::io::Error> {
+ fn server() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:9989").unwrap();
for stream in listener.incoming() {
let mut stream: TcpStream = stream.unwrap();
let out_buf: [u8; 256] = [0; 256];
let mut plaintext: [u8; 256] = [0; 256];
- plaintext[0..11].copy_from_slice("hello world".as_bytes());
+ plaintext[0..13].copy_from_slice("from server 1".as_bytes());
ossuary_send_data(
conn,
- (&plaintext) as *const u8 as *mut u8, 11 as u16,
+ (&plaintext) as *const u8 as *mut u8, 13 as u16,
(&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
let _ = stream.write(&out_buf);
let out_buf: [u8; 256] = [0; 256];
let mut plaintext: [u8; 256] = [0; 256];
- plaintext[0..13].copy_from_slice("goodbye world".as_bytes());
+ plaintext[0..13].copy_from_slice("from server 2".as_bytes());
ossuary_send_data(
conn,
(&plaintext) as *const u8 as *mut u8, 13 as u16,
(&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
let _ = stream.write(&out_buf);
+ let _ = stream.read(&mut in_buf);
+ let out_buf: [u8; 256] = [0; 256];
+ let len = ossuary_recv_data(
+ conn,
+ (&in_buf) as *const u8 as *mut u8, in_buf.len() as u16,
+ (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
+ if len != -1 {
+ println!("CLIB READ: {:?}",
+ std::str::from_utf8(&out_buf[0..len as usize]).unwrap());
+ }
+
ossuary_destroy_connection(&mut conn);
break;
}
Ok(())
}
- pub fn client() -> Result<(), std::io::Error> {
+ fn client() -> Result<(), std::io::Error> {
let mut stream = TcpStream::connect("127.0.0.1:9989").unwrap();
let mut conn = ossuary_create_connection(0);
}
}
+ let out_buf: [u8; 256] = [0; 256];
+ let mut plaintext: [u8; 256] = [0; 256];
+ plaintext[0..11].copy_from_slice("from client".as_bytes());
+ ossuary_send_data(
+ conn,
+ (&plaintext) as *const u8 as *mut u8, 11 as u16,
+ (&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
+ let _ = stream.write(&out_buf);
let _ = stream.read(&mut in_buf);
let out_buf: [u8; 256] = [0; 256];
(&in_buf) as *const u8 as *mut u8, in_buf.len() as u16,
(&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
if len != -1 {
- println!("FFI RECEIVED: {:?}", std::str::from_utf8(&out_buf[0..len as usize]));
+ println!("CLIB READ: {:?}",
+ std::str::from_utf8(&out_buf[0..len as usize]).unwrap());
}
let _ = stream.read(&mut in_buf);
(&in_buf) as *const u8 as *mut u8, in_buf.len() as u16,
(&out_buf) as *const u8 as *mut u8, out_buf.len() as u16);
if len != -1 {
- println!("FFI RECEIVED: {:?}", std::str::from_utf8(&out_buf[0..len as usize]));
+ println!("CLIB READ: {:?}",
+ std::str::from_utf8(&out_buf[0..len as usize]).unwrap());
}
ossuary_destroy_connection(&mut conn);
Ok(())
}
- pub fn test() {
- println!("FFI START");
+ #[test]
+ fn test() {
thread::spawn(move || { let _ = server(); });
let child = thread::spawn(move || { let _ = client(); });
let _ = child.join();
- println!("FFI DONE");
- }
- #[test]
- fn it_works() {
- test();
}
}
#[cfg(test)]
mod tests {
use std::thread;
- use std::time;
use std::net::{TcpListener, TcpStream};
use crate::*;
- fn event_loop<T>(mut conn: ConnectionContext, mut stream: T, is_server: bool) -> Result<(), std::io::Error>
+ fn event_loop<T>(mut conn: ConnectionContext,
+ mut stream: T,
+ is_server: bool) -> Result<(), std::io::Error>
where T: std::io::Read + std::io::Write {
while crypto_handshake_done(&conn) == false {
if crypto_send_handshake(&mut conn, &mut stream) {
}
if is_server {
- let mut plaintext = "hello, world".as_bytes();
+ let mut plaintext = "message from server".as_bytes();
let _ = crypto_send_data(&mut conn, &mut plaintext, &mut stream);
- let _ = stream.flush();
- loop {
- std::thread::sleep(time::Duration::from_millis(50));
- }
}
-
- loop {
- let mut plaintext = vec!();
- let _ = crypto_recv_data(&mut conn, &mut stream, &mut plaintext);
- println!("decrypted: {:?}", String::from_utf8(plaintext));
- std::thread::sleep(time::Duration::from_millis(50));
+ else {
+ let mut plaintext = "message from client".as_bytes();
+ let _ = crypto_send_data(&mut conn, &mut plaintext, &mut stream);
}
+
+ let mut plaintext = vec!();
+ let _ = crypto_recv_data(&mut conn, &mut stream, &mut plaintext);
+ println!("LIB READ: {:?}", String::from_utf8(plaintext).unwrap());
+ Ok(())
}
- pub fn server() -> Result<(), std::io::Error> {
+ fn server() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:9988").unwrap();
for stream in listener.incoming() {
let stream: TcpStream = stream.unwrap();
Ok(())
}
- pub fn client() -> Result<(), std::io::Error> {
+ fn client() -> Result<(), std::io::Error> {
let stream = TcpStream::connect("127.0.0.1:9988").unwrap();
let conn = ConnectionContext::new(false);
let _ = event_loop(conn, stream, false);
Ok(())
}
- pub fn test() {
+ #[test]
+ fn test() {
thread::spawn(move || { let _ = server(); });
let child = thread::spawn(move || { let _ = client(); });
let _ = child.join();
}
-
- #[test]
- fn it_works() {
- test();
- }
}