title: Driving events across custom TCP Long-Polling server
body: <p>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.</p>
  
  <p>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#.</p>
  
  <p>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.</p>
  
  <p>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… </p>
  
  <p>The flow should be something like this (“client” is on a phone):</p>
  
  <ol>
  <li><p>User starts my application</p></li>
  <li><p>Client sends a request to be notified if data has changed</p></li>
  <li><p>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! </p></li>
  <li><p>Event</p>
  
  <p>o   If it is triggered (new data has arrived), Send the data to the client</p>
  
  <p>o   If it isn’t triggered (no new data), Send an “EmptyChanges” packet to client</p></li>
  <li><p>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)</p></li>
  <li><p>Client sends a request to be notified if data has changed</p></li>
  </ol>
  
  <p>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!</p>
  
  <p>So, my application would look something like this (all psuedocode):</p>
  
  <pre><code>Class AppClass
  {
      Main()
  
      List&lt;Client&gt; clients;
      List&lt;DataServers&gt; dataServers;
  
      DataReceivedFromServer(Data stuff)
      {
      }
  
      MessageReceivedFromPhone(PhoneMessage pm, object sender)
      {
          //Loop here until HeartBeat interval reached
          Int totalTime = 0;
          While(totalTime &lt; HEARTBEAT_INTERVAL)
          {
              If( ) // If we have received data from the server, and the client WANTED that data, send it now
              {
              }
          }
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <blockquote>
    <p>Server platform: Windows </p>
    
    <p>Server language: C#</p>
    
    <p>Test Client platform: Windows </p>
    
    <p>Test Client language: C#</p>
    
    <p>Target Client platform: Windows Mobile 6.5, iPhone, Android (clients will be written separately)</p>
    
    <p>Target client language: C#, Obj-C or MonoTouch, Java</p>
  </blockquote>