Henry
Henry

Reputation: 3160

how twisted server detects an incomplete message

I am right now implementing a twisted client and twisted server. The question is how does the server detect if a message that is sent by client is completed?

For example, there are 2 clients sending messages to server, the message is a python list which has only several elements, respectively. The 2nd client sends the message very short time after the 1st client does.

Since it's async, the server will switch to serve the 2nd client, leaving the message of 1st client half-processed. So how do I do to let the server know this message is not complete? Thanks in advance.

Upvotes: 0

Views: 74

Answers (1)

notorious.no
notorious.no

Reputation: 5107

You want to implement some sort of protocol (not to be confused with twisted.internet.protocol) which has some sort of delimiter signifying the beginning and end of a message and how long your message will be. For example, let's define a protocol which implements the following rules:

  1. All messages start with {
  2. An int number of bytes which will contain the message body, followed by :
  3. The message body
  4. An end tag }

An example expected message would look like:

{10:Hey Earth!}

Twisted provides many interfaces to do this for you, so you don't really have to do this on your own. There's LineReceiver which combines bytes until a line break. There's the AMP protocol and NetStrings, which is similar to the example I provided previously.

References:

Upvotes: 1

Related Questions