Stop! I hope you didn't really forget everything from CS342 after reading yesterday's post! Guess which concept just turned out to be a perfect match to the problem we're currently facing? Yes, none other than the lowly coroutine. Those who sat beside me in that class and listened to me mock will enjoy this ...
I'm currently writing a TCP server that speaks a protocol called SIP. SIP stands for Session Initiation Protocol and is used by video/audio conferencing software to find another person to talk with. Think Microsoft Netmeeting. This server will be one piece of our voice over IP product.
A typical SIP session works in the following way:
- A opens a connection to B, and says "I INVITE you to talk to me"
- B replys to A, "OK let's talk"
- A finishes by saying, "I ACKnowledge your OK"
- Now the parties start communicating.
This little exchange is needed because the underlying protocol may be unreliable (ie: UDP), so each party repeats what it has to say until it receives a reply. For example, B will repeat it's "OK" message until it hears an "ACK", because each time it speaks there's a risk that the network will lose it's message.
Now, what does this have to do with co-routines? Well, the process described above is like a finite state machine, and the server may have thousands of these conversations going on at one time. The most obvious solution is to start one thousands threads, one per conversation.
That, however, is not the way things work at NIT! Our servers are single threaded for religious (and other :) ) reasons. When a new request initially comes in, we will create a coroutine to handle it. When a message comes in associated with a conversation in progress, we will lookup the associated coroutine, and "resume" it, passing the incoming message as a parameter.
So code to handle the INVITE request can be as simple as the multithreaded version:
- Wait for INVITE.
- Send OK.
- Wait for ACK.
- Begin sending/receiving of audio/video
... yet we avoid creating one thread per client.
I can't believe I learned this in school!
7:26:04 PM
|