Serializing Erlang Binary Protocols

I am currently using Erlang for a large project, but I have a question regarding the correct path.

I am receiving bytes over a tcp socket. Bytes correspond to a fixed protocol, the sender is a python client. The python client uses class inheritance to create bytes from objects.

Now I would like (in Erlang) to take bytes and convert them to their equiprobable messages, they all share a common message header.

How can I make this as general as possible in Erlang?

Respectfully,

Me

+3


source to share


1 answer


Pattern matching / binary header consumption using Erlang binary syntax. But you need to know which bytes or bits you expect to receive, or the sizes of the fields in bytes or bits.

For example, let's say that you are expecting a byte string that will either start with the equivalent of "PUSH" or "PULL" ASCII strings, and then some other data that you will post somewhere. You can create a function head that matches these and displays the rest to navigate to a function that does "push ()" or "pull ()" based on the byte header:

operation_type(<<"PUSH", Rest/binary>>) -> push(Rest);
operation_type(<<"PULL", Rest/binary>>) -> pull(Rest).

      

Now the bytes after the first four will be in Rest

, allowing you to freely interpret any subsequent headers or data in turn. You can also match all binaries:

operation_type(Bin = <<"PUSH", _/binary>>) -> push(Bin);
operation_type(Bin = <<"PULL", _/binary>>) -> pull(Bin).

      

In this case, the "_" variable works as it always does, you just check for leadership by essentially looking at the buffer and passing it all in based on the original content.



You can also skip in it. Say you knew you were going to get a binary with 4 bytes of fluff up front, 6 bytes of type data, and then the rest you want to transfer:

filter_thingy(<<_:4/binary, Type:6/binary, Rest/binary>>) ->
    % Do stuff with Rest based on Type...

      

It is very natural to strip binaries in function headers (regardless of whether the data matches character strings or not), allowing "rest" to jump to the corresponding functions as you go. If you are getting Python pickle data or something similar, you want to write a parsing routine in a recursive way so that the output of each data type takes you back to the top to determine the next type, with an accumulated tree that represents the data read so far.

I only looked at the 8 bit bytes above, but there is also a pure bit string syntax that lets you get as far into the bit and byte weed as you need with the same ease of syntax. Compliance is a real lifesaver here.

I hope this is more than confusing. The binary syntax in Erlang makes this the most enjoyable binary syntax environment in a general programming language that I've come across.

http://www.erlang.org/doc/programming_examples/bit_syntax.html

+2


source







All Articles