Skip to content

Commit

Permalink
Connecting works.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirVer committed Sep 7, 2015
1 parent e9840e6 commit 0293eb4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
56 changes: 21 additions & 35 deletions c_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<client::Client> = mem::transmute(client);
}
}

#[no_mangle]
pub extern "C" fn hello1(cb: extern fn(i32) -> *mut Test) {
let test_ptr = cb(42);
let test: Box<Test> = 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);
}
20 changes: 12 additions & 8 deletions c_client/test.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0293eb4

Please sign in to comment.