0

I am looking for optimal for maintenance/easy of programming and elegant patterns to parse delimited network streams, e.g. protocols like SMTP. Some of the concerns I've always had are:

  • how to react when a recv(2) call returns 0 - the need to track state to determine if no data has arrived or no more data will arrive
  • what if in one packet, we have multiple delimited fields arriving (e.g. HTTP request/response headers)

I've usually ended up building a buffer and parsing buffer until delimiter, handling that then separately, but it's always inelegant. Is there a Knuth-ian or GOF solution that is elegant?

By optimal/elegant I mean maintenance/programmability rather than hard-core use.

  • 4
    What are you optimising for and how do you define "elegant"? – MetaFight Nov 15 '17 at 13:10
2

Asking for "optimal" is, in a word, nonsensical. As @MetaFight asked, what are you optimizing and what is elegant?

Optimal

  • Readability
  • Speed
  • Robustness
  • Lines of code

and a number of other things.

Design Patterns

  • Finite State Machine - implement as a set of hierarchical state machines
  • Lexer/Parser - this is a specific type of hierarchical state machines which is often supported by code generation libraries, such as yacc/lex, bison, etc.
  • State Monad - Another implementation of state machine patterns.

Elegance

An open-ended question with lots of personal taste thrown in. Personally, I would start with an implementation in Scala, using a State Monad approach, with Either values that allow capturing error information to help reporting and recovery. Your mileage may vary (YMMV).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.