This page serves to answer the most frequently asked questions. Below is a list of questions and their respective answers. Please read through this before asking someone an obvious question.
Submitting an Entry
We do not currently have a system for submitting entries. If you have a good question and response you would like to see here, please contact us.
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 length of the cache.
- If it appears that the entire payload is in the cache, the cache parser will take out the beginning of the cache up to the specified length and store it into a payload variable. It will then call the appropriate handler for the payload function and loop back to see if there are more commands in the cache.
- If the length of the cache is smaller than the specified length of the payload, then the cache parser will add the command back into the beginning of the cache and return.
Here is how Mike Mintz implements this in Python. The function
read_buffer() is called every time there is data in the socket waiting to be read (determined by a select loop).
lines = string.split(self.inbuffer, '\r\n')
if len(lines) < 2:
words = string.split(lines)
if self.inbuffer[:3] in ['MSG', 'NOT']: # Incoming payload commands
length = int(words)
remaining_lines = string.join(lines[1:], '\r\n')
if len(remaining_lines) >= length:
self.inbuffer = self.inbuffer[(len(lines) + 2 + length):]
self.cmd_handler(lines + '\r\n' + remaining_lines[:length])
self.inbuffer = self.inbuffer[(len(lines) + 2):]