How can I parse incoming data as to separate commands without running into trouble with payload commands?
We recommend implementing your program in a manner similar to the one below. • Whenever new data from the server arrives over the socket, append it to a string that will be used as a “cache”. Each time you receive data, call the cache parser function as well. • The cache parser function will examine the cache variable and look for a newline. • If one is not found, the function will return without doing anything. • If one is found, it will read everything from the beginning of the cache up to the newline and put it into a new variable. It will then remove the command up to (and including) the newline from the beginning of the cache. • The cache parser will look at the first three bytes of the command. • If the command is recognized as a regular command, it will call the handler for that command and loop back to see if there are more commands in the cache. • If the command is recognized as a payload command, it will look at the specified length of the payload, and compare that to the len