Anyone who’s been paying attention to my rants on this forum knows that I really prefer synchronous socket communication, but it appears that most of the rest of the world does not… so I’m learning how to adapt. To that end I’ve been devising and testing several different ways to handle sockets asynchronously, and I’d like to get some feedback from anyone to either confirm that I’m doing things right or to help me learn a better way.
In short, I have a startup process for my app that is pretty complex, and full of lots of different decision points and states. An overview for this process looks something like this:
Many of the ovals and boxes there involve making decisions that will require accessing information provided by some WebServices (Does this user account exist? Do they have a valid subscription? Have they installed on too many other devices? Have they reached the limits imposed by their subscription in other ways? Does their local database match the data on the cloud already, etc… ). As you can see, this is not a trivial process, as it involves lots of different decisions and states.
So, my approach to implementing this with asynchronous sockets is to wrap up this entire process into a thread which has its own socket (or set of sockets) to do all the network communication. The thread tracks its own state as it moves through the registration process, and I can have a timer on the main thread (in the main window/view/whatever) that periodically checks in on the thread to see what state it is in.
Here is a sample project that demonstrates this approach. There are a few things that I’m not real comfortable doing, but am purposely trying in it to see if they are a reasonable approach or not. Specifically:
The Run method of my thread is basically a huge while loop that determines what it will do on each trip through the loop based on the state of the thread (i.e, where are we in the startup process). I’m not sure this is the “right” way to do this, and am open to suggestions.
In the Run method of this thread there are several nested while loops that essentially do nothing but wait for a response from the socket to update the step the thread is on. I’m uncomfortable with this approach, as I’m not a fan of tight loops. However, it does seem to work. I’m concerned about performance implications and want to know if there is a better way to do this.
I’m open to suggestions, criticism, wild praise, or even reckless snide remarks.
Happy Thursday!