From 40742a7c63a033b2005d8555d98dd875f3965b0a Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Fri, 7 Jan 2022 05:51:23 +0100 Subject: [PATCH] Add rust experiment example --- capi/Makefile | 7 ++- capi/example.rs | 112 ++++++++++++++++++++++++++++++++++++++++++ capi/libuwebsockets.h | 2 +- 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 capi/example.rs diff --git a/capi/Makefile b/capi/Makefile index 6ac53cd..7e99e8e 100644 --- a/capi/Makefile +++ b/capi/Makefile @@ -1,4 +1,7 @@ default: + rm -f *.o make -C ../uSockets - g++ -O3 -std=c++17 -flto -DUWS_NO_ZLIB -I ../src -I ../uSockets/src App.cpp -c - gcc -O3 -flto example.c *.o -lstdc++ ../uSockets/uSockets.a -o example \ No newline at end of file + g++ -c -O3 -std=c++17 -flto -DUWS_NO_ZLIB -I ../src -I ../uSockets/src App.cpp + $(AR) rvs uWebSockets.a App.o ../uSockets/*.o + gcc -O3 -flto example.c *.o -lstdc++ ../uSockets/uSockets.a -o c_example + rustc -C link-arg=uWebSockets.a -C link-arg=-lstdc++ -C opt-level=3 -C lto -L all=. example.rs -o rust_example \ No newline at end of file diff --git a/capi/example.rs b/capi/example.rs new file mode 100644 index 0000000..b3d9787 --- /dev/null +++ b/capi/example.rs @@ -0,0 +1,112 @@ +/* automatically generated by rust-bindgen 0.59.2 */ + +pub type size_t = ::std::os::raw::c_ulong; +pub type wchar_t = ::std::os::raw::c_uint; +#[repr(C)] +#[repr(align(16))] +#[derive(Debug, Copy, Clone)] +pub struct max_align_t { + pub __clang_max_align_nonce1: ::std::os::raw::c_longlong, + pub __bindgen_padding_0: u64, + pub __clang_max_align_nonce2: u128, +} +#[test] +fn bindgen_test_layout_max_align_t() { + assert_eq!( + ::std::mem::size_of::(), + 32usize, + concat!("Size of: ", stringify!(max_align_t)) + ); + assert_eq!( + ::std::mem::align_of::(), + 16usize, + concat!("Alignment of ", stringify!(max_align_t)) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::())).__clang_max_align_nonce1 as *const _ as usize + }, + 0usize, + concat!( + "Offset of field: ", + stringify!(max_align_t), + "::", + stringify!(__clang_max_align_nonce1) + ) + ); + assert_eq!( + unsafe { + &(*(::std::ptr::null::())).__clang_max_align_nonce2 as *const _ as usize + }, + 16usize, + concat!( + "Offset of field: ", + stringify!(max_align_t), + "::", + stringify!(__clang_max_align_nonce2) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct uws_app_s { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct uws_req_s { + _unused: [u8; 0], +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct uws_res_s { + _unused: [u8; 0], +} +pub type uws_app_t = uws_app_s; +pub type uws_req_t = uws_req_s; +pub type uws_res_t = uws_res_s; +extern "C" { + pub fn uws_create_app() -> *mut uws_app_t; +} +extern "C" { + pub fn uws_app_get( + app: *mut uws_app_t, + pattern: *const ::std::os::raw::c_char, + handler: ::std::option::Option< + unsafe extern "C" fn(arg1: *mut uws_res_t, + arg2: *mut uws_req_t), + >, + ); +} +extern "C" { + pub fn uws_app_run(arg1: *mut uws_app_t); +} +extern "C" { + pub fn uws_app_listen( + app: *mut uws_app_t, + port: ::std::os::raw::c_int, + handler: ::std::option::Option, + ); +} +extern "C" { + pub fn uws_res_end(res: *mut uws_res_t, data: *const ::std::os::raw::c_char, length: size_t); +} + +extern "C" fn listen_handler(listen_socket: *mut ::std::os::raw::c_void) { + println!("Listening"); +} + +extern "C" fn get_handler(res: *mut uws_res_t, req: *mut uws_req_t) { + unsafe { + uws_res_end(res, "Hello!".as_bytes().as_ptr(), 6); + } +} + +fn main() { + unsafe { + let app = uws_create_app(); + uws_app_get(app, ::std::ffi::CString::new("/*").expect("").as_ptr(), ::std::option::Option::Some(get_handler)); + uws_app_listen(app, 3000, ::std::option::Option::Some(listen_handler)); + uws_app_run(app); + } +} \ No newline at end of file diff --git a/capi/libuwebsockets.h b/capi/libuwebsockets.h index 7e87133..a30a8b9 100644 --- a/capi/libuwebsockets.h +++ b/capi/libuwebsockets.h @@ -1,5 +1,5 @@ #ifndef LIBUWS_CAPI_HEADER -#define LIBUS_CAPI_HEADER +#define LIBUWS_CAPI_HEADER #include