feat jsonrpc2: add ?on_received/?on_sent

this can be useful to debug
This commit is contained in:
Simon Cruanes 2024-04-15 12:41:47 -04:00
parent 7730eabf98
commit c9ade392ef
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 36 additions and 7 deletions

View file

@ -13,8 +13,17 @@ module type S = sig
include module type of Server.Make (IO) include module type of Server.Make (IO)
val create : ic:IO.in_channel -> oc:IO.out_channel -> server -> t val create :
val create_stdio : server -> t ?on_received:(json -> unit) ->
?on_sent:(json -> unit) ->
ic:IO.in_channel ->
oc:IO.out_channel ->
server ->
t
val create_stdio :
?on_received:(json -> unit) -> ?on_sent:(json -> unit) -> server -> t
val send_server_notification : t -> Lsp.Server_notification.t -> unit IO.t val send_server_notification : t -> Lsp.Server_notification.t -> unit IO.t
val send_server_request : val send_server_request :
@ -59,18 +68,30 @@ module Make (IO : IO) : S with module IO = IO = struct
type t = { type t = {
ic: IO.in_channel; ic: IO.in_channel;
oc: IO.out_channel; oc: IO.out_channel;
on_sent: json -> unit;
on_received: json -> unit;
s: server; s: server;
mutable id_counter: int; mutable id_counter: int;
pending_responses: (Req_id.t, server_request_handler_pair) Hashtbl.t; pending_responses: (Req_id.t, server_request_handler_pair) Hashtbl.t;
} }
let create ~ic ~oc server : t = let create ?(on_received = ignore) ?(on_sent = ignore) ~ic ~oc server : t =
{ ic; oc; s = server; id_counter = 0; pending_responses = Hashtbl.create 8 } {
ic;
oc;
s = server;
id_counter = 0;
on_sent;
on_received;
pending_responses = Hashtbl.create 8;
}
let create_stdio server : t = create ~ic:IO.stdin ~oc:IO.stdout server let create_stdio ?on_received ?on_sent server : t =
create ?on_received ?on_sent ~ic:IO.stdin ~oc:IO.stdout server
(* send a single message *) (* send a single message *)
let send_json_ (self : t) (j : json) : unit IO.t = let send_json_ (self : t) (j : json) : unit IO.t =
self.on_sent j;
let json = J.to_string j in let json = J.to_string j in
Log.debug (fun k -> Log.debug (fun k ->
k "jsonrpc2: send json (%dB): %s" (String.length json) json); k "jsonrpc2: send json (%dB): %s" (String.length json) json);
@ -296,6 +317,7 @@ module Make (IO : IO) : S with module IO = IO = struct
Fun.id @@ try_ Fun.id @@ try_
@@ fun () -> IO.return @@ J.from_string (Bytes.unsafe_to_string buf) @@ fun () -> IO.return @@ J.from_string (Bytes.unsafe_to_string buf)
in in
self.on_received j;
Log.debug (fun k -> k "got json %s" (J.to_string j)); Log.debug (fun k -> k "got json %s" (J.to_string j));
(match Jsonrpc.Packet.t_of_yojson @@ fix_null_in_params j with (match Jsonrpc.Packet.t_of_yojson @@ fix_null_in_params j with

View file

@ -14,10 +14,17 @@ module type S = sig
include module type of Server.Make (IO) include module type of Server.Make (IO)
val create : ic:IO.in_channel -> oc:IO.out_channel -> server -> t val create :
?on_received:(json -> unit) ->
?on_sent:(json -> unit) ->
ic:IO.in_channel ->
oc:IO.out_channel ->
server ->
t
(** Create a connection from the pair of channels *) (** Create a connection from the pair of channels *)
val create_stdio : server -> t val create_stdio :
?on_received:(json -> unit) -> ?on_sent:(json -> unit) -> server -> t
(** Create a connection using stdin/stdout *) (** Create a connection using stdin/stdout *)
val send_server_notification : t -> Lsp.Server_notification.t -> unit IO.t val send_server_notification : t -> Lsp.Server_notification.t -> unit IO.t