Refactor Parser#163
Conversation
| return PARSER_INCOMPLETE; | ||
| } | ||
|
|
||
| parser_return_state_t parse_sever_settings_message(snapcast_custom_parser_t *parser, |
There was a problem hiding this comment.
You could put all those variables passed to the functions in a struct/union combination.
There was a problem hiding this comment.
Yes! I will do this, once the parser is simplified. I think I will be able to remove a lot of parameters corresponding to internal parser state first.
|
Before you continue your effort maybe we should check if netconn api is really the best thing to use. There have been some packet delivery issues on my last tests and I am uncertain where this comes from. Also espressif says netconn is less resource intensive but when asking chatGPT I get this for an answer So I am abit confused now and I think a comparison of the two should be done by logging max heap usage while reception. Maybe we can integrate this into http_task and just drop all the data and do the same using sockets |
|
I’ve significantly reduced the number of parameters passed to These values now live in much smaller scopes: either directly inside Strongly reducing the parameter count further would likely mean grouping values with little semantic connection into a shared struct. I’m not sure that would improve readability or maintainability. But I may also be missing what kind of refactoring you have in mind. If you’d like me to continue in this direction, a concrete example of the struct or grouping you’re thinking of would really help. Let me know if the current state works for you. If so, I’ll resolve the conflicts and get things wrapped up 🙂 |
|
Thank you. Hopefully I'll find some time to review soon. Maybe someone else will jump in sooner? |
|
I just had a look at the conflicts and it's ... a lot. Maybe it makes sense to postpone the review until the conflicts are resolved? I'm not sure. But I guess I'll already start working toward resolving the conflicts, because this will only get worse the more |
9d6fc4e to
49bf600
Compare
|
I finally managed to resolve all of the conflicts! 🎉 |
|
I had no time to look into details, but I like the structure a lot and a quick test was running fine. @CarlosDerSeher I think it would be great if we could move forward because all the PRs or planned PRs would benefit from not modifying the same main.c file. #174 wants to wait for this, I also wait with further work. And #195 again adds a lot of modifications, that would otherwise go the the connection_manager I guess. |
|
Oh didn't realize this was ready :) nice |
|
Great, and just today I merged some other stuff :( hopefully this isn't so much work to resolve |
It's resolved again :) |
e234e51
into
CarlosDerSeher:refactor_parser
| process_data(&parser, &time_sync_data, &received_codec_header, &codec, | ||
| &scSet, &pcmData); | ||
| if (result == -1) { | ||
| return; // critical error in data processing |
There was a problem hiding this comment.
@blattm this return makes no sense. Should this be a break or continue?
There was a problem hiding this comment.
As far as I know, there were cases of critical errors, where we returned from http_get_task, which triggers a reboot. I kept this behavior in those cases by including this return statement.
There was a problem hiding this comment.
any idea how to resolve this gracefully instead?
There was a problem hiding this comment.
To make it more explicit you could call esp_restart().
Not sure in which case this happens. If a restart of the connection helps we could also break like for -2.
There was a problem hiding this comment.
There are two kind of cases where how this might happen: Failed memory allocations and receiving invalid data (e.g. broken JSON). These issues can occur both in the snapcast_protocol_parser.c as well as the functions handling the parsed data (in main.c). Restarting the connection might work for broken data cases, but probably not in an out-of-memory situation.
I'd say being explicit and using esp_restart() sounds better and it even comes with an additional potential benefit :)
We could move the calls to esp_restart() directly into the parser and the functions handling the data. This would simplify the structure a little, because process_data and the main loop calling process_data wouldn't need to check for (and pass through) this kind of error anymore!
Hi!
This PR aims to improve the readability of
main.cby separating parsing logic from message handling.Although the code is already separated, it's not perfect, and there is still work to be done. There are, for example, too many variables being passed in function calls for my taste.
I'm planning to write a
read_bytefunction that the parser can call. If would fetch data from the network when the buffer is empty. This would allow us to eliminate theswitch/caseand make many variables locally scoped. I would use error codes to detect whether a reset (corresponding to the outermost while loop of the ´http_get_task`) is necessary.It might also make sense to further modularize the message-handling side (e.g., splitting out time sync, codecs, etc.), but that can easily be done in a later PR.