Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a parser for an existing binary protocol in Java

I have to implement a high performance Java client of an existing binary protocol. I will use Netty. However since the protocol is complex (many message types with many fields) I would like to separate netty code from parsing code and generate a parser of this protocol from a document describing this protocol in a higher abstraction. Similar like it is done in Google's Protocol Buffers. Unfortunately it seems I cannot use protobuf as my protocol is in a different binary wire format. Any suggestions?

like image 969
Adam Siemion Avatar asked Feb 25 '26 19:02

Adam Siemion


1 Answers

I haven't tried it myself, but I think you might be looking for the java binary block parser

Here's an example form the official readme that shows how it can be used to parse the structure of a TCP header using a high level DSL:

final JBBPParser tcpParser = JBBPParser.prepare(
          "skip:34; // skip bytes till the frame\n"
          + "ushort SourcePort;"
          + "ushort DestinationPort;"
          + "int SequenceNumber;"
          + "int AcknowledgementNumber;"

          + "bit:1 NONCE;"
          + "bit:3 RESERVED;"
          + "bit:4 HLEN;"

          + "bit:1 FIN;"
          + "bit:1 SYN;"
          + "bit:1 RST;"
          + "bit:1 PSH;"
          + "bit:1 ACK;"
          + "bit:1 URG;"
          + "bit:1 ECNECHO;"
          + "bit:1 CWR;"

          + "ushort WindowSize;"
          + "ushort TCPCheckSum;"
          + "ushort UrgentPointer;"
          + "byte [$$-34-HLEN*4] Option;"
          + "byte [_] Data;"
  );

final JBBPFieldStruct result = pngParser.parse(tcpFrameStream);
like image 161
Malt Avatar answered Feb 27 '26 08:02

Malt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!