@@ -4963,97 +4963,97 @@ pub unsafe extern "C" fn dc_accounts_get_event_emitter(
49634963 Box :: into_raw ( Box :: new ( emitter) )
49644964}
49654965
4966- pub struct dc_jsonrpc_instance_t {
4967- receiver : OutReceiver ,
4968- handle : RpcSession < CommandApi > ,
4966+ pub struct dc_jsonrpc_instance_t {
4967+ receiver : OutReceiver ,
4968+ handle : RpcSession < CommandApi > ,
4969+ }
4970+
4971+ #[ no_mangle]
4972+ pub unsafe extern "C" fn dc_jsonrpc_init (
4973+ account_manager : * mut dc_accounts_t ,
4974+ ) -> * mut dc_jsonrpc_instance_t {
4975+ if account_manager. is_null ( ) {
4976+ eprintln ! ( "ignoring careless call to dc_jsonrpc_init()" ) ;
4977+ return ptr:: null_mut ( ) ;
49694978 }
49704979
4971- #[ no_mangle]
4972- pub unsafe extern "C" fn dc_jsonrpc_init (
4973- account_manager : * mut dc_accounts_t ,
4974- ) -> * mut dc_jsonrpc_instance_t {
4975- if account_manager. is_null ( ) {
4976- eprintln ! ( "ignoring careless call to dc_jsonrpc_init()" ) ;
4977- return ptr:: null_mut ( ) ;
4978- }
4980+ let account_manager = & * account_manager;
4981+ let cmd_api = block_on ( deltachat_jsonrpc:: api:: CommandApi :: from_arc (
4982+ account_manager. inner . clone ( ) ,
4983+ ) ) ;
49794984
4980- let account_manager = & * account_manager;
4981- let cmd_api = block_on ( deltachat_jsonrpc:: api:: CommandApi :: from_arc (
4982- account_manager. inner . clone ( ) ,
4983- ) ) ;
4985+ let ( request_handle, receiver) = RpcClient :: new ( ) ;
4986+ let handle = RpcSession :: new ( request_handle, cmd_api) ;
49844987
4985- let ( request_handle, receiver) = RpcClient :: new ( ) ;
4986- let handle = RpcSession :: new ( request_handle, cmd_api) ;
4988+ let instance = dc_jsonrpc_instance_t { receiver, handle } ;
49874989
4988- let instance = dc_jsonrpc_instance_t { receiver, handle } ;
4990+ Box :: into_raw ( Box :: new ( instance) )
4991+ }
49894992
4990- Box :: into_raw ( Box :: new ( instance) )
4993+ #[ no_mangle]
4994+ pub unsafe extern "C" fn dc_jsonrpc_unref ( jsonrpc_instance : * mut dc_jsonrpc_instance_t ) {
4995+ if jsonrpc_instance. is_null ( ) {
4996+ eprintln ! ( "ignoring careless call to dc_jsonrpc_unref()" ) ;
4997+ return ;
49914998 }
4999+ drop ( Box :: from_raw ( jsonrpc_instance) ) ;
5000+ }
49925001
4993- #[ no_mangle]
4994- pub unsafe extern "C" fn dc_jsonrpc_unref ( jsonrpc_instance : * mut dc_jsonrpc_instance_t ) {
4995- if jsonrpc_instance. is_null ( ) {
4996- eprintln ! ( "ignoring careless call to dc_jsonrpc_unref()" ) ;
4997- return ;
4998- }
4999- drop ( Box :: from_raw ( jsonrpc_instance) ) ;
5000- }
5002+ fn spawn_handle_jsonrpc_request ( handle : RpcSession < CommandApi > , request : String ) {
5003+ spawn ( async move {
5004+ handle. handle_incoming ( & request) . await ;
5005+ } ) ;
5006+ }
50015007
5002- fn spawn_handle_jsonrpc_request ( handle : RpcSession < CommandApi > , request : String ) {
5003- spawn ( async move {
5004- handle. handle_incoming ( & request) . await ;
5005- } ) ;
5008+ #[ no_mangle]
5009+ pub unsafe extern "C" fn dc_jsonrpc_request (
5010+ jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5011+ request : * const libc:: c_char ,
5012+ ) {
5013+ if jsonrpc_instance. is_null ( ) || request. is_null ( ) {
5014+ eprintln ! ( "ignoring careless call to dc_jsonrpc_request()" ) ;
5015+ return ;
50065016 }
50075017
5008- #[ no_mangle]
5009- pub unsafe extern "C" fn dc_jsonrpc_request (
5010- jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5011- request : * const libc:: c_char ,
5012- ) {
5013- if jsonrpc_instance. is_null ( ) || request. is_null ( ) {
5014- eprintln ! ( "ignoring careless call to dc_jsonrpc_request()" ) ;
5015- return ;
5016- }
5018+ let handle = & ( * jsonrpc_instance) . handle ;
5019+ let request = to_string_lossy ( request) ;
5020+ spawn_handle_jsonrpc_request ( handle. clone ( ) , request) ;
5021+ }
50175022
5018- let handle = & ( * jsonrpc_instance) . handle ;
5019- let request = to_string_lossy ( request) ;
5020- spawn_handle_jsonrpc_request ( handle. clone ( ) , request) ;
5023+ #[ no_mangle]
5024+ pub unsafe extern "C" fn dc_jsonrpc_next_response (
5025+ jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5026+ ) -> * mut libc:: c_char {
5027+ if jsonrpc_instance. is_null ( ) {
5028+ eprintln ! ( "ignoring careless call to dc_jsonrpc_next_response()" ) ;
5029+ return ptr:: null_mut ( ) ;
50215030 }
5031+ let api = & * jsonrpc_instance;
5032+ block_on ( api. receiver . recv ( ) )
5033+ . map ( |result| serde_json:: to_string ( & result) . unwrap_or_default ( ) . strdup ( ) )
5034+ . unwrap_or ( ptr:: null_mut ( ) )
5035+ }
50225036
5023- #[ no_mangle]
5024- pub unsafe extern "C" fn dc_jsonrpc_next_response (
5025- jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5026- ) -> * mut libc:: c_char {
5027- if jsonrpc_instance. is_null ( ) {
5028- eprintln ! ( "ignoring careless call to dc_jsonrpc_next_response()" ) ;
5029- return ptr:: null_mut ( ) ;
5030- }
5031- let api = & * jsonrpc_instance;
5032- block_on ( api. receiver . recv ( ) )
5033- . map ( |result| serde_json:: to_string ( & result) . unwrap_or_default ( ) . strdup ( ) )
5034- . unwrap_or ( ptr:: null_mut ( ) )
5035- }
5036-
5037- #[ no_mangle]
5038- pub unsafe extern "C" fn dc_jsonrpc_blocking_call (
5039- jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5040- input : * const libc:: c_char ,
5041- ) -> * mut libc:: c_char {
5042- if jsonrpc_instance. is_null ( ) {
5043- eprintln ! ( "ignoring careless call to dc_jsonrpc_blocking_call()" ) ;
5044- return ptr:: null_mut ( ) ;
5045- }
5046- let api = & * jsonrpc_instance;
5047- let input = to_string_lossy ( input) ;
5048- let res = block_on ( api. handle . process_incoming ( & input) ) ;
5049- match res {
5050- Some ( message) => {
5051- if let Ok ( message) = serde_json:: to_string ( & message) {
5052- message. strdup ( )
5053- } else {
5054- ptr:: null_mut ( )
5055- }
5037+ #[ no_mangle]
5038+ pub unsafe extern "C" fn dc_jsonrpc_blocking_call (
5039+ jsonrpc_instance : * mut dc_jsonrpc_instance_t ,
5040+ input : * const libc:: c_char ,
5041+ ) -> * mut libc:: c_char {
5042+ if jsonrpc_instance. is_null ( ) {
5043+ eprintln ! ( "ignoring careless call to dc_jsonrpc_blocking_call()" ) ;
5044+ return ptr:: null_mut ( ) ;
5045+ }
5046+ let api = & * jsonrpc_instance;
5047+ let input = to_string_lossy ( input) ;
5048+ let res = block_on ( api. handle . process_incoming ( & input) ) ;
5049+ match res {
5050+ Some ( message) => {
5051+ if let Ok ( message) = serde_json:: to_string ( & message) {
5052+ message. strdup ( )
5053+ } else {
5054+ ptr:: null_mut ( )
50565055 }
5057- None => ptr:: null_mut ( ) ,
50585056 }
5057+ None => ptr:: null_mut ( ) ,
50595058 }
5059+ }
0 commit comments