Commit 4436ffae authored by Alexander Danilov's avatar Alexander Danilov

Исправлен FFI, добавлен метод skaro_disconnect()

parent 074e9654
......@@ -18,19 +18,15 @@ int main()
c = getchar( );
skaro_register(x);
printf("x: ");printf(x);fflush(stdout);
c = getchar( );
printf("x: ");printf(x);fflush(stdout);
skaro_register(x);
printf("x: ");printf(x);fflush(stdout);
c = getchar( );
skaro_authorize(x);
fflush(stdout);
skaro_disconnect(x);
c = getchar( );
......
......@@ -4,5 +4,4 @@ extern handle skaro_connect(const char*);
void skaro_authorize(handle);
void skaro_register(handle);
void skaro_disconnect(handle);
......@@ -14,7 +14,6 @@ extern {
pub fn ws_connect(connection: &str) -> *mut (){
use std::thread;
use std::sync::mpsc::channel;
use std::io::stdin;
use websocket::{Message, OwnedMessage};
use websocket::client::ClientBuilder;
......@@ -134,11 +133,9 @@ pub fn ws_connect(connection: &str) -> *mut (){
println!("Exited");
let mut context = Box::new(tx);
let context = Box::new(tx);
let handle = Box::into_raw(context) as *mut ();
std::mem::forget(handle);
std::mem::forget(send_loop);
//std::mem::forget(client);
handle
}
\ No newline at end of file
......@@ -15,38 +15,30 @@ mod parse;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::os::raw::c_void;
use std::any::Any;
#[no_mangle]
pub extern fn skaro_connect(connection: *const c_char) -> *mut () {
dalek::ws_connect(unsafe{CStr::from_ptr(connection).to_string_lossy().into_owned().as_str()})
}
use std::any::Any;
fn print_if_string(value: Box<Any>) {
if let Ok(string) = value.downcast::<i32>() {
println!("i32 {}", string);
}
#[no_mangle]
pub extern fn skaro_disconnect(handle: *mut std::sync::mpsc::Sender<websocket::OwnedMessage<>>) {
let y: Box<Any> = unsafe{ Box::<std::sync::mpsc::Sender<websocket::OwnedMessage<>>>::from_raw(handle) };
std::mem::forget(y);
}
#[no_mangle]
pub extern fn skaro_authorize(handle: *mut std::sync::mpsc::Sender<websocket::OwnedMessage<>>) {
println!("start skaro_authorize");
let y: Box<Any> = unsafe{ Box::<std::sync::mpsc::Sender<websocket::OwnedMessage<>>>::from_raw(handle) };
match y.downcast::<std::sync::mpsc::Sender<websocket::OwnedMessage<>>>() {
Ok(s) => {
s.clone().send(
let maybe_channel = unsafe{ handle.as_mut() };
websocket::OwnedMessage::Binary(
::handler::authorize().unwrap()));
match maybe_channel {
Some(s) => {
s.send(websocket::OwnedMessage::Binary(::handler::authorize().unwrap()));
},
Err(x) => {
println!("Err");
None => {
println!("null ptr");
}
};
}
......@@ -55,15 +47,14 @@ pub extern fn skaro_authorize(handle: *mut std::sync::mpsc::Sender<websocket::Ow
pub extern fn skaro_register(handle: *mut std::sync::mpsc::Sender<websocket::OwnedMessage<>>) {
println!("start skaro_register");
let y: Box<Any> = unsafe{ Box::<std::sync::mpsc::Sender<websocket::OwnedMessage<>>>::from_raw(handle) };
let maybe_channel = unsafe{ handle.as_mut() };
match y.downcast::<std::sync::mpsc::Sender<websocket::OwnedMessage<>>>() {
Ok(s) => {
s.clone().send(websocket::OwnedMessage::Binary(::handler::register().unwrap()));
match maybe_channel {
Some(s) => {
s.send(websocket::OwnedMessage::Binary(::handler::register().unwrap()));
},
Err(x) => {
println!("Err");
None => {
println!("null ptr");
}
};
std::mem::forget(handle);
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment