robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <assert.h> 2#include <inttypes.h> 3#include <stdbool.h> 4#include <stdio.h> 5#include <stdlib.h> 6 7#include "klib/kvec.h" 8#include "nvim/api/private/defs.h" 9#include "nvim/api/private/dispatch.h" 10#include "nvim/api/private/helpers.h" 11#include "nvim/api/ui.h" 12#include "nvim/channel.h" 13#include "nvim/channel_defs.h" 14#include "nvim/event/defs.h" 15#include "nvim/event/loop.h" 16#include "nvim/event/multiqueue.h" 17#include "nvim/event/proc.h" 18#include "nvim/event/rstream.h" 19#include "nvim/event/wstream.h" 20#include "nvim/globals.h" 21#include "nvim/log.h" 22#include "nvim/main.h" 23#include "nvim/map_defs.h" 24#include "nvim/memory.h" 25#include "nvim/message.h" 26#include "nvim/msgpack_rpc/channel.h" 27#include "nvim/msgpack_rpc/channel_defs.h" 28#include "nvim/msgpack_rpc/packer.h" 29#include "nvim/msgpack_rpc/packer_defs.h" 30#include "nvim/msgpack_rpc/unpacker.h" 31#include "nvim/os/input.h" 32#include "nvim/types_defs.h" 33#include "nvim/ui.h" 34#include "nvim/ui_client.h" 35 36#ifdef MSWIN 37# include "nvim/os/os_win_console.h" 38#endif 39 40#include "msgpack_rpc/channel.c.generated.h" 41 42#ifdef NVIM_LOG_DEBUG 43# define REQ "[request] " 44# define RES "[response] " 45# define NOT "[notify] " 46# define ERR "[error] " 47 48# define SEND "->" 49# define RECV "<-" 50 51static void log_request (char * dir ,uint64_t channel_id ,uint32_t req_id ,const char * name ) 52{ 53logmsg (LOGLVL_DBG ,"RPC: " ,NULL ,-1 , false,"%s %" PRIu64 ": %s id=%u: %s\n" ,dir ,channel_id , 54REQ ,req_id ,name ); 55} 56 57static void log_response (char * dir ,uint64_t channel_id ,char * kind ,uint32_t req_id ) 58{ 59logmsg (LOGLVL_DBG ,"RPC: " ,NULL ,-1 , false,"%s %" PRIu64 ": %s id=%u\n" ,dir ,channel_id ,kind , 60req_id ); 61} 62 63static void log_notify (char * dir ,uint64_t channel_id ,const char * name ) 64{ 65logmsg (LOGLVL_DBG ,"RPC: " ,NULL ,-1 , false,"%s %" PRIu64 ": %s %s\n" ,dir ,channel_id ,NOT , 66name ); 67} 68 69#else 70# define log_request (...) 71# define log_response (...) 72# define log_notify (...) 73#endif 74 75void rpc_init (void ) 76{ 77ch_before_blocking_events = multiqueue_new_child (main_loop .events ); 78} 79 80void rpc_start (Channel * channel ) 81{ 82channel_incref (channel ); 83channel -> is_rpc = true; 84RpcState * rpc = & channel -> rpc ; 85rpc -> closed = false; 86rpc -> unpacker = xcalloc (1 ,sizeof * rpc -> unpacker ); 87unpacker_init (rpc -> unpacker ); 88rpc -> next_request_id = 1 ; 89rpc -> info = (Dict )ARRAY_DICT_INIT ; 90kv_init (rpc -> call_stack ); 91 92if (channel -> streamtype != kChannelStreamInternal ) { 93RStream * out = channel_outstream (channel ); 94#ifdef NVIM_LOG_DEBUG 95Stream * in = channel_instream (channel ); 96DLOG ("rpc ch %" PRIu64 " in-stream=%p out-stream=%p" ,channel -> id , 97 (void * )in , (void * )out ); 98#endif 99 100rstream_start (out ,receive_msgpack ,channel ); 101 } 102} 103 104static Channel * find_rpc_channel (uint64_t id ) 105{ 106Channel * chan = find_channel (id ); 107if (!chan || !chan -> is_rpc || chan -> rpc .closed ) { 108return NULL ; 109 } 110return chan ; 111} 112 113/// Publishes an event to a channel (emits a notification to method `name`). 114/// 115/// @param id Channel id, or 0 to broadcast to all RPC channels. 116/// @param name Event name (application-defined) 117/// @param args Array of event arguments 118/// @return True if the event was sent successfully, false otherwise. 119bool rpc_send_event (uint64_t id ,const char * name ,Array args ) 120{ 121Channel * channel = NULL ; 122 123if (id && (!(channel = find_rpc_channel (id )))) { 124return false; 125 } 126 127log_notify (SEND ,channel ?channel -> id :0 ,name ); 128if (channel ) { 129serialize_request (& channel ,1 ,0 ,name ,args ); 130 }else { 131broadcast_event (name ,args ); 132 } 133 134return true; 135} 136 137/// Sends a method call to a channel 138/// 139/// @param id The channel id 140/// @param method_name The method name, an arbitrary string 141/// @param args Array with method arguments 142/// @param[out] error True if the return value is an error 143/// @return Whatever the remote method returned 144Object rpc_send_call (uint64_t id ,const char * method_name ,Array args ,ArenaMem * result_mem , 145Error * err ) 146{ 147Channel * channel = NULL ; 148 149if (!(channel = find_rpc_channel (id ))) { 150api_set_error (err ,kErrorTypeException ,"Invalid channel: %" PRIu64 ,id ); 151return NIL ; 152 } 153 154channel_incref (channel ); 155RpcState * rpc = & channel -> rpc ; 156uint32_t request_id = rpc -> next_request_id ++ ; 157// Send the msgpack-rpc request 158serialize_request (& channel ,1 ,request_id ,method_name ,args ); 159 160log_request (SEND ,channel -> id ,request_id ,method_name ); 161 162// Push the frame 163ChannelCallFrame frame = {request_id , false, false,NIL ,NULL }; 164kv_push (rpc -> call_stack ,& frame ); 165LOOP_PROCESS_EVENTS_UNTIL (& main_loop ,channel -> events ,-1 ,frame .returned || rpc -> closed ); 166 (void )kv_pop (rpc -> call_stack ); 167 168// !frame.returned implies rpc->closed. 169// If frame.returned is true, its memory needs to be freed, so don't return here. 170if (!frame .returned ) { 171api_set_error (err ,kErrorTypeException ,"Invalid channel: %" PRIu64 ,id ); 172channel_decref (channel ); 173return NIL ; 174 } 175 176if (frame .errored ) { 177if (frame .result .type == kObjectTypeString ) { 178api_set_error (err ,kErrorTypeException ,"%s" , 179frame .result .data .string .data ); 180 }else if (frame .result .type == kObjectTypeArray ) { 181// Should be an error in the form [type, message] 182Array array = frame .result .data .array ; 183if (array .size == 2 && array .items [0 ].type == kObjectTypeInteger 184&& (array .items [0 ].data .integer == kErrorTypeException 185|| array .items [0 ].data .integer == kErrorTypeValidation ) 186&& array .items [1 ].type == kObjectTypeString ) { 187api_set_error (err , (ErrorType )array .items [0 ].data .integer ,"%s" , 188array .items [1 ].data .string .data ); 189 }else { 190api_set_error (err ,kErrorTypeException ,"%s" ,"unknown error" ); 191 } 192 }else { 193api_set_error (err ,kErrorTypeException ,"%s" ,"unknown error" ); 194 } 195 196// frame.result was allocated in an arena 197arena_mem_free (frame .result_mem ); 198frame .result_mem = NULL ; 199 } 200 201channel_decref (channel ); 202 203* result_mem = frame .result_mem ; 204 205return frame .errored ?NIL :frame .result ; 206} 207 208static size_t receive_msgpack (RStream * stream ,const char * rbuf ,size_t c ,void * data ,bool eof ) 209{ 210Channel * channel = data ; 211channel_incref (channel ); 212size_t consumed = 0 ; 213 214DLOG ("ch %" PRIu64 ": parsing %zu bytes from msgpack Stream: %p" , 215channel -> id ,c , (void * )stream ); 216 217if (c > 0 ) { 218Unpacker * p = channel -> rpc .unpacker ; 219p -> read_ptr = rbuf ; 220p -> read_size = c ; 221parse_msgpack (channel ); 222 223if (!unpacker_closed (p )) { 224consumed = c - p -> read_size ; 225 } 226 } 227 228if (eof ) { 229char buf [256 ]; 230snprintf (buf ,sizeof (buf ),"ch %" PRIu64 " was closed by the peer" ,channel -> id ); 231chan_close_on_err (channel ,buf ,LOGLVL_INF ); 232 } 233 234channel_decref (channel ); 235return consumed ; 236} 237 238static ChannelCallFrame * find_call_frame (RpcState * rpc ,uint32_t request_id ) 239{ 240for (size_t i = 0 ;i < kv_size (rpc -> call_stack );i ++ ) { 241ChannelCallFrame * frame = kv_Z (rpc -> call_stack ,i ); 242if (frame -> request_id == request_id ) { 243return frame ; 244 } 245 } 246return NULL ; 247} 248 249static void parse_msgpack (Channel * channel ) 250{ 251Unpacker * p = channel -> rpc .unpacker ; 252while (unpacker_advance (p )) { 253if (p -> type == kMessageTypeRedrawEvent ) { 254if (ui_client_attached ) { 255if (p -> has_grid_line_event ) { 256ui_client_event_raw_line (& p -> grid_line_event ); 257p -> has_grid_line_event = false; 258 }else if (p -> ui_handler .fn != NULL && p -> result .type == kObjectTypeArray ) { 259p -> ui_handler .fn (p -> result .data .array ); 260 } 261 } 262arena_mem_free (arena_finish (& p -> arena )); 263 }else if (p -> type == kMessageTypeResponse ) { 264ChannelCallFrame * frame = channel -> rpc .client_type == kClientTypeMsgpackRpc 265 ?find_call_frame (& channel -> rpc ,p -> request_id ) 266 :kv_last (channel -> rpc .call_stack ); 267if (frame == NULL || p -> request_id != frame -> request_id ) { 268char buf [256 ]; 269snprintf (buf ,sizeof (buf ), 270"ch %" PRIu64 " (type=%" PRIu32 ") returned a response with an unknown request " 271"id %" PRIu32 ". Ensure the client is properly synchronized" , 272channel -> id , (unsigned )channel -> rpc .client_type ,p -> request_id ); 273chan_close_on_err (channel ,buf ,LOGLVL_ERR ); 274return ; 275 } 276frame -> returned = true; 277frame -> errored = (p -> error .type != kObjectTypeNil ); 278 279if (frame -> errored ) { 280frame -> result = p -> error ; 281// TODO(bfredl): p->result should not even be decoded 282// api_free_object(p->result); 283 }else { 284frame -> result = p -> result ; 285 } 286frame -> result_mem = arena_finish (& p -> arena ); 287log_response (RECV ,channel -> id ,frame -> errored ?ERR :RES ,p -> request_id ); 288 }else { 289if (p -> type == kMessageTypeNotification ) { 290log_notify (RECV ,channel -> id ,p -> handler .name ); 291 }else { 292log_request (RECV ,channel -> id ,p -> request_id ,p -> handler .name ); 293 } 294 295Object res = p -> result ; 296if (p -> result .type != kObjectTypeArray ) { 297chan_close_on_err (channel ,"msgpack-rpc request args must be an array" ,LOGLVL_ERR ); 298return ; 299 } 300Array arg = res .data .array ; 301handle_request (channel ,p ,arg ); 302 } 303 } 304 305if (unpacker_closed (p )) { 306chan_close_on_err (channel ,p -> unpack_error .msg ,LOGLVL_INF ); 307api_clear_error (& p -> unpack_error ); 308 } 309} 310 311/// Handles requests and notifications received on the channel. 312static void handle_request (Channel * channel ,Unpacker * p ,Array args ) 313FUNC_ATTR_NONNULL_ALL 314{ 315assert (p -> type == kMessageTypeRequest || p -> type == kMessageTypeNotification ); 316 317if (!p -> handler .fn ) { 318send_error (channel ,p -> handler ,p -> type ,p -> request_id ,p -> unpack_error .msg ); 319api_clear_error (& p -> unpack_error ); 320arena_mem_free (arena_finish (& p -> arena )); 321return ; 322 } 323 324RequestEvent * evdata = xmalloc (sizeof (RequestEvent )); 325evdata -> type = p -> type ; 326evdata -> channel = channel ; 327evdata -> handler = p -> handler ; 328evdata -> args = args ; 329evdata -> used_mem = p -> arena ; 330p -> arena = (Arena )ARENA_EMPTY ; 331evdata -> request_id = p -> request_id ; 332channel_incref (channel ); 333if (p -> handler .fast ) { 334bool is_get_mode = p -> handler .fn == handle_nvim_get_mode ; 335 336if (is_get_mode && !input_blocking ()) { 337// Defer the event to a special queue used by os/input.c. #6247 338multiqueue_put (ch_before_blocking_events ,request_event ,evdata ); 339 }else { 340// Invoke immediately. 341request_event ((void ** )& evdata ); 342 } 343 }else { 344bool is_resize = p -> handler .fn == handle_nvim_ui_try_resize ; 345if (is_resize ) { 346Event ev = event_create_oneshot (event_create (request_event ,evdata ),2 ); 347multiqueue_put_event (channel -> events ,ev ); 348multiqueue_put_event (resize_events ,ev ); 349 }else { 350multiqueue_put (channel -> events ,request_event ,evdata ); 351DLOG ("RPC: scheduled %.*s" , (int )p -> method_name_len ,p -> handler .name ); 352 } 353 } 354} 355 356/// Handles a message, depending on the type: 357/// - Request: invokes method and writes the response (or error). 358/// - Notification: invokes method (emits `nvim_error_event` on error). 359static void request_event (void ** argv ) 360{ 361RequestEvent * e = argv [0 ]; 362Channel * channel = e -> channel ; 363MsgpackRpcRequestHandler handler = e -> handler ; 364Error error = ERROR_INIT ; 365if (channel -> rpc .closed ) { 366// channel was closed, abort any pending requests 367 gotofree_ret ; 368 } 369 370Object result = handler .fn (channel -> id ,e -> args ,& e -> used_mem ,& error ); 371if (e -> type == kMessageTypeRequest || ERROR_SET (& error )) { 372// Send the response. 373serialize_response (channel ,e -> handler ,e -> type ,e -> request_id ,& error ,& result ); 374 } 375if (handler .ret_alloc ) { 376api_free_object (result ); 377 } 378 379free_ret : 380// e->args (and possibly result) are allocated in an arena 381arena_mem_free (arena_finish (& e -> used_mem )); 382channel_decref (channel ); 383xfree (e ); 384api_clear_error (& error ); 385} 386 387bool rpc_write_raw (uint64_t id ,WBuffer * buffer ) 388{ 389Channel * channel = find_rpc_channel (id ); 390if (!channel ) { 391wstream_release_wbuffer (buffer ); 392return false; 393 } 394 395return channel_write (channel ,buffer ); 396} 397 398static bool channel_write (Channel * channel ,WBuffer * buffer ) 399{ 400int err = 0 ; 401 402if (channel -> rpc .closed ) { 403wstream_release_wbuffer (buffer ); 404return false; 405 } 406 407if (channel -> streamtype == kChannelStreamInternal ) { 408channel_incref (channel ); 409CREATE_EVENT (channel -> events ,internal_read_event ,channel ,buffer ); 410 }else { 411Stream * in = channel_instream (channel ); 412err = wstream_write (in ,buffer ); 413 } 414 415if (err != 0 ) { 416// If the write failed for any reason, close the channel 417char buf [256 ]; 418snprintf (buf , 419sizeof (buf ), 420"ch %" PRIu64 ": stream write failed: %s. " 421"RPC canceled; closing channel" , 422channel -> id ,os_strerror (err )); 423// UV_EPIPE can happen if pipe is closed by peer and shouldn't be an error. 424chan_close_on_err (channel ,buf ,err == UV_EPIPE ?LOGLVL_INF :LOGLVL_ERR ); 425 } 426 427return err == 0 ; 428} 429 430static void internal_read_event (void ** argv ) 431{ 432Channel * channel = argv [0 ]; 433WBuffer * buffer = argv [1 ]; 434Unpacker * p = channel -> rpc .unpacker ; 435 436p -> read_ptr = buffer -> data ; 437p -> read_size = buffer -> size ; 438if (unpacker_closed (p )) { 439 gotoend ; 440 } 441parse_msgpack (channel ); 442 443if (p -> read_size ) { 444// This should not happen, as WBuffer is one single serialized message. 445if (!channel -> rpc .closed ) { 446chan_close_on_err (channel ,"internal channel: internal error" ,LOGLVL_ERR ); 447 } 448 } 449 450end : 451channel_decref (channel ); 452wstream_release_wbuffer (buffer ); 453} 454 455static void send_error (Channel * chan ,MsgpackRpcRequestHandler handler ,MessageType type , 456uint32_t id ,char * err ) 457{ 458Error e = ERROR_INIT ; 459api_set_error (& e ,kErrorTypeException ,"%s" ,err ); 460serialize_response (chan ,handler ,type ,id ,& e ,& NIL ); 461api_clear_error (& e ); 462} 463 464/// Broadcasts a notification to all RPC channels. 465static void broadcast_event (const char * name ,Array args ) 466{ 467kvec_withinit_t (Channel * ,4 )chans = KV_INITIAL_VALUE ; 468kvi_init (chans ); 469Channel * channel ; 470 471map_foreach_value (& channels ,channel , { 472if (channel -> is_rpc ) { 473kv_push (chans ,channel ); 474 } 475 }); 476 477if (kv_size (chans )) { 478serialize_request (chans .items ,kv_size (chans ),0 ,name ,args ); 479 } 480 481kvi_destroy (chans ); 482} 483 484/// Mark rpc state as closed, and release its reference to the channel. 485/// Don't call this directly, call channel_close(id, kChannelPartRpc, &error) 486void rpc_close (Channel * channel ) 487{ 488if (channel -> rpc .closed ) { 489return ; 490 } 491 492channel -> rpc .closed = true; 493 494// Scheduled to avoid running UILeave autocommands in a libuv handler. 495multiqueue_put (main_loop .fast_events ,rpc_close_event ,channel ); 496} 497 498static void rpc_close_event (void ** argv ) 499{ 500Channel * channel = (Channel * )argv [0 ]; 501assert (channel ); 502 503channel_decref (channel ); 504 505#ifdef MSWIN 506// For ":detach!": unexpected disconnect does not call ui_detach_channel, unlike the normal 507// ":detach" case, so we need to do some cleanup here. 508bool detached_ui_stdio = channel -> streamtype == kChannelStreamStdio 509&& channel -> detach && channel -> rpc .ui != NULL ; 510#endif 511 512// No more I/O can happen on this channel. Remove UI if there is one attached. 513// Do this here instead of in rpc_free() which isn't always called on exit, so that 514// UILeave events behave consistently. 515remote_ui_disconnect (channel -> id ,NULL , false); 516 517bool is_ui_client = ui_client_channel_id && channel -> id == ui_client_channel_id ; 518if (is_ui_client ) { 519if (!exiting && channel -> streamtype != kChannelStreamProc ) { 520ui_client_attach_to_restarted_server (false); 521 } 522if (ui_client_channel_id != channel -> id ) { 523// Attached to new server. Don't exit. 524return ; 525 } 526if (channel -> streamtype == kChannelStreamProc && ui_client_error_exit < 0 ) { 527// Wait for the embedded server to exit instead of exiting immediately, 528// as it's necessary to get the server's exit code in on_proc_exit(). 529return ; 530 } 531exit_on_closed_chan (0 ); 532 }else if (channel -> streamtype == kChannelStreamStdio && !channel -> detach ) { 533exit_on_closed_chan (0 ); 534 } 535#ifdef MSWIN 536else if (detached_ui_stdio ) { 537// Move this server off the now-dead console so it keeps working (CONIN$/CONOUT$). 538os_swap_to_hidden_console (); 539 } 540#endif 541} 542 543void rpc_free (Channel * channel ) 544{ 545unpacker_teardown (channel -> rpc .unpacker ); 546xfree (channel -> rpc .unpacker ); 547 548kv_destroy (channel -> rpc .call_stack ); 549api_free_dict (channel -> rpc .info ); 550} 551 552/// Closes a channel after receiving fatal error, and logs a message. 553static void chan_close_on_err (Channel * channel ,char * msg ,int loglevel ) 554{ 555logmsg (loglevel ,"RPC: " ,NULL ,-1 , true,"%s" ,msg ); 556for (size_t i = 0 ;i < kv_size (channel -> rpc .call_stack );i ++ ) { 557ChannelCallFrame * frame = kv_A (channel -> rpc .call_stack ,i ); 558if (frame -> returned ) { 559continue ;// Don't overwrite an already received result. #24214 560 } 561frame -> returned = true; 562frame -> errored = true; 563frame -> result = CSTR_TO_ARENA_OBJ (& channel -> rpc .unpacker -> arena ,msg ); 564frame -> result_mem = arena_finish (& channel -> rpc .unpacker -> arena ); 565 } 566 567channel_close (channel -> id ,kChannelPartRpc ,NULL ); 568 569LOG (loglevel ,"RPC: %s" ,msg ); 570} 571 572static void serialize_request (Channel ** chans ,size_t nchans ,uint32_t request_id , 573const char * method ,Array args ) 574{ 575PackerBuffer packer ; 576packer_buffer_init_channels (chans ,nchans ,& packer ); 577 578mpack_array (& packer .ptr ,request_id ?4 :3 ); 579mpack_w (& packer .ptr ,request_id ?0 :2 ); 580 581if (request_id ) { 582mpack_uint (& packer .ptr ,request_id ); 583 } 584 585mpack_str (cstr_as_string (method ),& packer ); 586mpack_object_array (args ,& packer ); 587 588packer_buffer_finish_channels (& packer ); 589} 590 591void serialize_response (Channel * channel ,MsgpackRpcRequestHandler handler ,MessageType type , 592uint32_t response_id ,Error * err ,Object * arg ) 593{ 594if (ERROR_SET (err )&& type == kMessageTypeNotification ) { 595if (handler .fn == handle_nvim_paste ) { 596// TODO(bfredl): this is pretty much ad-hoc. maybe TUI and UI:s should be 597// allowed to ask nvim to just scream directly in the users face 598// instead of sending nvim_error_event, in general. 599semsg ("paste: %s" ,err -> msg ); 600api_clear_error (err ); 601 }else { 602MAXSIZE_TEMP_ARRAY (args ,2 ); 603ADD_C (args ,INTEGER_OBJ (err -> type )); 604ADD_C (args ,CSTR_AS_OBJ (err -> msg )); 605serialize_request (& channel ,1 ,0 ,"nvim_error_event" ,args ); 606 } 607return ; 608 } 609 610PackerBuffer packer ; 611packer_buffer_init_channels (& channel ,1 ,& packer ); 612 613mpack_array (& packer .ptr ,4 ); 614mpack_w (& packer .ptr ,1 ); 615mpack_uint (& packer .ptr ,response_id ); 616 617if (ERROR_SET (err )) { 618// error represented by a [type, message] array 619mpack_array (& packer .ptr ,2 ); 620mpack_integer (& packer .ptr ,err -> type ); 621mpack_str (cstr_as_string (err -> msg ),& packer ); 622// Nil result 623mpack_nil (& packer .ptr ); 624 }else { 625// Nil error 626mpack_nil (& packer .ptr ); 627// Return value 628mpack_object (arg ,& packer ); 629 } 630 631packer_buffer_finish_channels (& packer ); 632 633log_response (SEND ,channel -> id ,ERROR_SET (err ) ?ERR :RES ,response_id ); 634} 635 636static void packer_buffer_init_channels (Channel ** chans ,size_t nchans ,PackerBuffer * packer ) 637{ 638for (size_t i = 0 ;i < nchans ;i ++ ) { 639Channel * chan = chans [i ]; 640if (chan -> rpc .ui && chan -> rpc .ui -> incomplete_event ) { 641remote_ui_flush_pending_data (chan -> rpc .ui ); 642 } 643 } 644packer -> startptr = alloc_block (); 645packer -> ptr = packer -> startptr ; 646packer -> endptr = packer -> startptr + ARENA_BLOCK_SIZE ; 647packer -> packer_flush = channel_flush_callback ; 648packer -> anydata = chans ; 649packer -> anyint = (int64_t )nchans ; 650} 651 652static void packer_buffer_finish_channels (PackerBuffer * packer ) 653{ 654size_t len = (size_t )(packer -> ptr - packer -> startptr ); 655if (len > 0 ) { 656WBuffer * buf = wstream_new_buffer (packer -> startptr ,len , (size_t )packer -> anyint ,free_block ); 657Channel ** chans = packer -> anydata ; 658for (int64_t i = 0 ;i < packer -> anyint ;i ++ ) { 659channel_write (chans [i ],buf ); 660 } 661 }else { 662free_block (packer -> startptr ); 663 } 664} 665 666static void channel_flush_callback (PackerBuffer * packer ) 667{ 668packer_buffer_finish_channels (packer ); 669packer_buffer_init_channels (packer -> anydata , (size_t )packer -> anyint ,packer ); 670} 671 672void rpc_set_client_info (uint64_t id ,Dict info ) 673{ 674Channel * chan = find_rpc_channel (id ); 675if (!chan ) { 676abort (); 677 } 678 679api_free_dict (chan -> rpc .info ); 680chan -> rpc .info = info ; 681 682// Parse "type" on "info" and set "client_type" 683const char * type = get_client_info (chan ,"type" ); 684if (type == NULL || strequal (type ,"remote" )) { 685chan -> rpc .client_type = kClientTypeRemote ; 686 }else if (strequal (type ,"msgpack-rpc" )) { 687chan -> rpc .client_type = kClientTypeMsgpackRpc ; 688 }else if (strequal (type ,"ui" )) { 689chan -> rpc .client_type = kClientTypeUi ; 690 }else if (strequal (type ,"embedder" )) { 691chan -> rpc .client_type = kClientTypeEmbedder ; 692 }else if (strequal (type ,"host" )) { 693chan -> rpc .client_type = kClientTypeHost ; 694 }else if (strequal (type ,"plugin" )) { 695chan -> rpc .client_type = kClientTypePlugin ; 696 }else { 697chan -> rpc .client_type = kClientTypeUnknown ; 698 } 699 700channel_event (chan ,EVENT_CHANINFO ); 701} 702 703Dict rpc_client_info (Channel * chan ) 704{ 705return copy_dict (chan -> rpc .info ,NULL ); 706} 707 708const char * get_client_info (Channel * chan ,const char * key ) 709FUNC_ATTR_NONNULL_ALL 710{ 711if (!chan -> is_rpc ) { 712return NULL ; 713 } 714Dict info = chan -> rpc .info ; 715for (size_t i = 0 ;i < info .size ;i ++ ) { 716if (strequal (key ,info .items [i ].key .data ) 717&& info .items [i ].value .type == kObjectTypeString ) { 718return info .items [i ].value .data .string .data ; 719 } 720 } 721 722return NULL ; 723} 724 725#ifdef EXITFREE 726void rpc_free_all_mem (void ) 727{ 728multiqueue_free (ch_before_blocking_events ); 729} 730#endif