diff --git a/c_client/src/lib.rs b/c_client/src/lib.rs index 80550e8..1b31412 100644 --- a/c_client/src/lib.rs +++ b/c_client/src/lib.rs @@ -1,52 +1,38 @@ extern crate libc; +extern crate swiboe; use libc::c_char; use std::ffi::{CStr, CString}; use std::mem; +use std::path; use std::str; +use swiboe::client; -#[no_mangle] -pub extern "C" fn hello(c_buf: *const libc::c_char) { - let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) }; +// TODO(sirver): this always makes a copy, even though it might not be needed. +fn c_str_to_string(c_buf: *const c_char) -> String { + let c_str = unsafe { CStr::from_ptr(c_buf) }; let buf: &[u8] = c_str.to_bytes(); - let str_slice: &str = str::from_utf8(buf).unwrap(); - // let str_buf: String = str_slice.to_owned(); // if necessary - println!("#sirver str_slice(): {:#?}", str_slice); + // NOCOM(#sirver): do not unwrap, do error handling. + str::from_utf8(buf).unwrap().into() } -pub struct Test { - a: String, -} +// TODO(sirver): This crashes if the function is called connect. +#[no_mangle] +pub extern "C" fn create_client(socket_name: *const c_char) -> *mut client::Client { + let socket_name = c_str_to_string(socket_name); + let socket_name_path = path::Path::new(&socket_name); -impl Drop for Test { - fn drop(&mut self) { - println!("Test got dropped.") - } + let client = Box::new( + // NOCOM(#sirver): error handling + client::Client::connect(socket_name_path).unwrap(), + ); + + unsafe { mem::transmute(client) } } #[no_mangle] -pub extern "C" fn create(c_buf: *const c_char) -> *mut Test { - let c_str = unsafe { CStr::from_ptr(c_buf) }; - let buf: &[u8] = c_str.to_bytes(); - let str_slice: &str = str::from_utf8(buf).unwrap(); - +pub extern "C" fn disconnect(client: *mut client::Client) { unsafe { - mem::transmute(Box::new(Test { - a: str_slice.into(), - })) + let _: Box = mem::transmute(client); } } - -#[no_mangle] -pub extern "C" fn hello1(cb: extern fn(i32) -> *mut Test) { - let test_ptr = cb(42); - let test: Box = unsafe { - mem::transmute(test_ptr) - }; - - println!("#sirver test.a: {:#?}", test.a); - // let c_str = unsafe { CStr::from_ptr(c_buf) }; - // let buf: &[u8] = c_str.to_bytes(); - // let str_slice: &str = str::from_utf8(buf).unwrap(); - // println!("Back in rust: {}", str_slice); -} diff --git a/c_client/test.py b/c_client/test.py index 28387f2..1bb4e2a 100755 --- a/c_client/test.py +++ b/c_client/test.py @@ -1,16 +1,20 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from ctypes import c_void_p, c_char_p import ctypes -sw = ctypes.cdll.LoadLibrary("target/debug/libswiboe.dylib") +import time -sw.hello(u"Löwe 老虎 Léopard".encode('utf-8')) -sw.hello(u"ASCII żółć 🇨🇭 한".encode('utf-8')) +swiboe = ctypes.cdll.LoadLibrary("target/debug/libswiboe.dylib") -def callback(arg): - return sw.create("Hello again: %i" % arg) +swiboe.create_client.restype = c_void_p +swiboe.create_client.argtypes = [c_char_p] -sw.create.restype = ctypes.c_void_p +swiboe.disconnect.restype = None +swiboe.disconnect.argtypes = [c_void_p] -CALLBACK = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_int32) -sw.hello1(CALLBACK(callback)) +client = swiboe.create_client("/tmp/blub.socket") + +time.sleep(5) + +swiboe.disconnect(client)