[erlang-questions] Text based protocols

Joern opendev@REDACTED
Mon Apr 23 13:51:52 CEST 2007


Hi Ulf,

On 4/22/07, Ulf Eliasson <eliassou@REDACTED> wrote:

> Have you looked into yecc?

Not yet, as it seemed somewhat heavyweight for my use case.

> You don't describe the protocol in any detail so it's hard to say anything more
> but I have used it for parsing text based protocols fast with great success.
> That's what it's created for so ;).

It's emi / ucp which looks like this:

 <stx> T / L / O / Y / D / C <etx>

with T, L and Y being string representations of 0 padded integers. O
is "O" or "R", D is the payload (stays binary) and C is a checksum. /
is $/. L denotes the length of the whole package - <s|etx>.

> This might not be the best solution for you because I don't know the protocol in
> detail but I hope it helps, and for yecc there is man pages and also
> documentation on the web, otherwise just ask here.

ATM I do something like this:

decode(<<2, Head:14/binary, Tail/binary>>) ->
	<< TRN:2/binary, $/, Len:5/binary, $/, Op:1/binary, $/,
Type:2/binary, $/ >> = Head,
	Length = conv_bin_to_int(Len) - ?PAYLOAD_SIZE,
	true = Length > 0,
	<<Data:Length/binary, "/", Checksum:2/binary, 3>> = Tail,
	Expected = list_to_binary(
		crc(lists:foldl(
			fun(X, Sum) ->
				X + Sum end, 0, binary_to_list(Head) ++ binary_to_list(Data) ++ "/" )
		)
	),
	if
		Expected /= Checksum ->
			erlang:error( { invalid_crc, Checksum });
		true ->
			{ ok, #ucp { trn=conv_bin_to_int(TRN),
operation=conv_op_to_atom(Op), type=conv_bin_to_int(Type), data=Data }
}
	end.

While conv_?_to_? do things like list_to_integer(binary_to_list(B))
etc. - it works, is probably quite fast but the error messages are
still quite cryptic. The explicit error in the crc check is an attempt
to improve this a bit but still ...


rgs/joern
--



More information about the erlang-questions mailing list