lwt-zmq icon indicating copy to clipboard operation
lwt-zmq copied to clipboard

How to easier to rewrite code to work with binary data?

Open mryau opened this issue 9 years ago • 2 comments

Hello, I want to work with structured messages packed to protocol buffers. Is it right way to parametrize Router module by message type or you do not interested in such patches?

mryau avatar Nov 28 '16 12:11 mryau

Do you have an example of what you would want the interface to look like? I could see something along the lines of the following being simple but useful wrappers:

val make_send : ('a -> string list) -> _ Lwt_zmq.Socket.t -> ('a -> unit Lwt.t)
val make_recv : (string list -> 'a) -> _ Lwt_zmq.Socket.t -> (unit -> 'a Lwt.t)

val make_send_s : ('a -> string list Lwt.t) -> _ Lwt_zmq.Socket.t -> ('a -> unit Lwt.t)
val make_recv_s : (string list -> 'a Lwt.t) -> _ Lwt_zmq.Socket.t -> (unit -> 'a Lwt.t)

make_send to_message socket would return a function which sends values over socket, serialized by to_message.

make_recv of_message socket would return a function which received values over socket, deserialized by of_message.

The _s versions would take a cooperative function for the (de)serialization process.

hcarty avatar Dec 01 '16 22:12 hcarty

Hello,

currently I need only properly typed messages on zmq bus so I prefer following modification of your code with functor:

module type IDType = sig
  type t
  val to_string : t -> string
  val of_string : string -> t
end

module FLSocket(DType : IDType) = struct

  type 'a t = {
    socket : 'a ZMQ.Socket.t;
    fd : Lwt_unix.file_descr;
  }

...
  let recv s =
    wrap (fun s -> let r = ZMQ.Socket.recv ~block:false s in DType.of_string r) s

  let send ?more s m =
    wrap (fun s -> ZMQ.Socket.send ?more ~block:false s (DType.to_string m)) s
...
end

type vlan = int list [@@deriving protobuf { protoc }]
type io =
        | Port of int [@key 1]
        | Agg of int [@key 2]
        | Vlan of vlan [@key 3]
        [@@deriving protobuf { protoc }]

type router = io list [@@deriving protobuf { protoc }]

module LSocket =
  FLSocket(struct
    type t = router
    let to_string = Protobuf.Encoder.encode_exn router_to_protobuf
    let of_string = Protobuf.Decoder.decode_exn router_from_protobuf
  end)

I don't know should I send such modification as pull request to your repository?

mryau avatar Dec 05 '16 12:12 mryau