<p>I am rewriting code to handle some embedded communications and right now the protocol handling is implemented in a While loop with a large case/switch statement.  This method seems a little unwieldy. What are the most commonly used flow control methods for implementing communication protocols?</p>

[{'answer_id': 64528, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 0, 'selected': False, 'text': '<p>For cases where you key off of a field in a protocol header to direct you to the next stage of processing for that protocol, arrays of function pointers can be used. You use the value from the protocol header to index into the array and call the function for that protocol.</p>\n\n<p>You <strong>must</strong> handle all possible values in this array, even those which are not valid. Eventually you will get a packet containing the invalid value, either because someone is trying an attack or because a future rev of the protocol adds new values.</p>\n'}, {'answer_id': 64543, 'author': 'Ryan Roper', 'author_id': 8273, 'author_profile': 'https://Stackoverflow.com/users/8273', 'pm_score': 0, 'selected': False, 'text': '<p>If it is all one protocol being handled then a switch/case statement may be your best bet. However you should break all the individual message handlers into their own functions. </p>\n\n<p>If your switch statement contains any code to actually handle the messages than you would be better off breaking them out.</p>\n\n<p>If it is handling multiple similar protocols you could create a class to handle each one based off the same abstract class and when the connection comes in you could determine which protocol it is and create an instance of the appropriate handler class to decode and handle the communications.</p>\n'}, {'answer_id': 65614, 'author': 'junkforce', 'author_id': 2153, 'author_profile': 'https://Stackoverflow.com/users/2153', 'pm_score': 0, 'selected': False, 'text': "<p>I would think this depends largely on the language you are using, and what sort of data set objects you have available to you. </p>\n\n<p>In python, for example, you could create a Dictionary object of all the different handling statements, and just iterate through that to find the right method/function to call.</p>\n\n<p>Case/Switch statements aren't bad things, but if they get huge(like they can with massive amounts of protocol handlers) then they can become unwieldy to work with. </p>\n"}, {'answer_id': 124444, 'author': 'Martin Liesén', 'author_id': 20715, 'author_profile': 'https://Stackoverflow.com/users/20715', 'pm_score': 1, 'selected': False, 'text': '<p>It sounds like the "while + switch/case" is a statemachine implementation. I believe that a well thought out statemachine is often the easiest and most readable way to implement a protocol.</p>\n\n<p>When it comes to statemachines, breaking some of the traditional programming rules comes with the territory. Rules like "every function should be less than 25 lines" just don\'t work. One might even argue that statemachines are GOTOs in disguise.</p>\n'}]
['https://Stackoverflow.com/questions/64495', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']