pub fn register_callback(socket: I, function: extern "C" fn(I) -> K) -> KExpand description
Register callback to the associated kdb+ socket.
#[macro_use]
extern crate kdbplus;
use kdbplus::api::*;
use kdbplus::qtype;
static mut PIPE:[I; 2]=[-1, -1];
// Callback for some message queue.
extern "C" fn callback(socket: I)->K{
let mut buffer: [K; 1]=[0 as K];
unsafe{libc::read(socket, buffer.as_mut_ptr() as *mut V, 8)};
// Call `shout` function on q side with the received data.
let result=error_to_string(unsafe{native::k(0, str_to_S!("shout"), buffer[0], KNULL)});
if result.get_type() == qtype::ERROR{
eprintln!("Execution error: {}", result.get_symbol().unwrap());
decrement_reference_count(result);
};
KNULL
}
#[no_mangle]
pub extern "C" fn plumber(_: K) -> K{
if 0 != unsafe{libc::pipe(PIPE.as_mut_ptr())}{
return new_error("Failed to create pipe\0");
}
if KNULL == register_callback(unsafe{PIPE[0]}, callback){
return new_error("Failed to register callback\0");
}
// Lock symbol in a worker thread.
pin_symbol();
let handle=std::thread::spawn(move ||{
let mut precious=new_list(qtype::SYMBOL_LIST, 3);
let precious_array=precious.as_mut_slice::<S>();
precious_array[0]=enumerate(null_terminated_str_to_S("belief\0"));
precious_array[1]=enumerate(null_terminated_str_to_S("love\0"));
precious_array[2]=enumerate(null_terminated_str_to_S("hope\0"));
unsafe{libc::write(PIPE[1], std::mem::transmute::<*mut K, *mut V>(&mut precious), 8)};
});
handle.join().unwrap();
unpin_symbol();
KNULL
}q)shout:{[precious] -1 "What are the three largest elements?: ", .Q.s1 precious;};
q)fall_into_pipe: `libc_api_example 2: (`plumber; 1);
q)fall_into_pipe[]
What are the three largest elements?: `belief`love`hope