[erlang-questions] encoding packets a style+efficiency question

Anupam Kapoor anupam.kapoor@REDACTED
Tue Jul 26 09:36:05 CEST 2011


hi all,

i have been playing with an implementation of tftp
(rfc-1350). encoding/decoding tftp-pdu's is trivial with erlang's bit
syntax.

there seem to be couple of approaches to encoding/decoding tftp-pdu's:

   1. use native "in-place" encode/decode
   2. have a 'generic' encode/decode routine which accepts
         - a list of pdu primitives e.g. 'byte'/'string' etc. and
         - a list of corresponding values
      and returns the binary packet.

      for example, encoding a tftp-data packet looks like this:
          ,----
          | %% encode data packet
          | encode_data_pkt(BlkNum, Data) ->
          |     encode_packet([?BYTE, ?BYTE, ?RAW],
[?TFTP_OPCODE_DATA, BlkNum, Data]).
          `----

to me, approach #2 seems pretty close to rfc's packet-format
specification. for example, the data-packet is shown as

           2 bytes     2 bytes      n bytes
           ----------------------------------
          | Opcode |   Block #  |   Data     |
           ----------------------------------

can you please provide your suggestion on whether #2 might be a good
approach to follow for complicated protocols e.g. gtp-v2 which defines a
large number of messages ? also, are there better (increasing order of
readability, performance etc.) approaches for doing this ?

thank you
kind regards
anupam

ps: the attached file contains encoders for tftp messages...

-- 
In the beginning was the lambda, and the lambda was with Emacs, and
Emacs was the lambda.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: misc.erl
Type: text/x-erlang
Size: 2269 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110726/c54fa77d/attachment.bin>


More information about the erlang-questions mailing list