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.