Making socket communication using bin_prot

244 views Asked by At

I want to make Socket communication using bin_prot in OCaml. However, I can't find any detailed explanation or example to do that. I made Socket communication in the other way before, so I know the flow of it.
Do you have good explanations or examples to make Socket communication using bin_prot in OCaml?

1

There are 1 answers

5
ivg On BEST ANSWER

Well, bin_prot is just a serialization protocol, and doesn't depend on whatever you're using for a transport layer. Basically, to serialize a value to string, you can use Binable.to_string function (or Binable.to_bigstring). It accepts a packed module. For example, to serialize a set of ints, do the following:

 let str = Binable.to_string (module Int.Set) mine_set; 

where mine_set is the set of integers.

If you have your arbitrary type, that implements bin_prot, then it will work the same. An example would be:

module My_data = struct
  type t = int * string with bin_io
end
let str = Binable.to_string (module My_data) (42,"answer")