views:

284

answers:

1

I’m trying to write a custom TCP based long polling server that will serve as a go-between for other TCP connections that require more persistence than a mobile phone can provide.

The way I’m trying to do it is writing an asynchronous TCP server in C# and a matching TCP Client also written in C#.

The way that long polling works (as far as I understand it) is that you open a TCP connection to a server, and the server Halts before sending data back across the socket. You find a Heartbeat interval that works on a mobile phone network (I’ve heard that around 8 minutes works?) and you send an empty packet if there is no updated data.

This is where my trouble comes in. I can’t figure out how to “link” my client’s request for data with an event handler running on the server…

The flow should be something like this (“client” is on a phone):

  1. User starts my application

  2. Client sends a request to be notified if data has changed

  3. Server “links” (registers) client’s socket object into an “Event handler” that is called by the server’s other TCP connections that I talked about!

  4. Event

    o If it is triggered (new data has arrived), Send the data to the client

    o If it isn’t triggered (no new data), Send an “EmptyChanges” packet to client

  5. Client receives data on the phone and processes it (calls an event handler based on what type of packet it received and passes the “data” it got from the server to it)

  6. Client sends a request to be notified if data has changed

So, my problem is that I can’t think of a design that will accomplish what I want it to do. The problem is that I don’t know HOW to do #3. How do I “Link” one event handler from another? And these are almost guaranteed to be running on different threads!

So, my application would look something like this (all psuedocode):

Class AppClass
{
    Main()

    List<Client> clients;
    List<DataServers> dataServers;

    DataReceivedFromServer(Data stuff)
    {
    }

    MessageReceivedFromPhone(PhoneMessage pm, object sender)
    {
     //Loop here until HeartBeat interval reached
     Int totalTime = 0;
     While(totalTime < HEARTBEAT_INTERVAL)
     {
      If( ) // If we have received data from the server, and the client WANTED that data, send it now
      {
      }
     }
    }
}

Kind of? I want it to be event driven, but I'm having the damndest time figuring out how to drive the application with a PUSH driven style vs. what I'm "used" to of Polling.

Please, be kind as I might be doing something overly complicated and stupid because this is my first real attempt at using Socket programming (never needed it) and it's especially hard due to the nature of Cell phones being on transient networks and my server needing to maintain the location of these phones with an OPEN TCP connection.

Server platform: Windows

Server language: C#

Test Client platform: Windows

Test Client language: C#

Target Client platform: Windows Mobile 6.5, iPhone, Android (clients will be written separately)

Target client language: C#, Obj-C or MonoTouch, Java

A: 

Just anyone wondering this, I trashed the idea of writing a custom TCP server to manage my connections. There was so much overhead in doing that, I'd basically be replicating writing my own HTTP server, so instead of doing that, I went with the Web Tornado framework in Python as my server and am writing the back end services to communicate through HTTP requests in Web Tornado.

Instead of using Long polling at all, I'm going to use SMS for push notifications. I believe all of the major phone platforms implement something similar to an SMS Interceptor that you write... if an SMS of a certain format comes through, it will run your custom code. This allows me to remove the requirements of using consistent open connections (other than for live chat, which will use a comet style Long poll, but the connection can only remain open if active for about 5 minutes.)

Basically, the Web Tornado framework is serving as an Enterprise bus in my architecture.

Crowe T. Robot