title: Telegram client updates and API requests over same session
body: <p>I'm coding a custom C# telegram client starting from TLSharp and modified it in order to support layer 54.</p>
  
  <p>I want handle both receiving updates from telegram servers and using the API without opening a separate session to do this.</p>
  
  <p>The problem is basically a multithreaded access to the socket connected to the Telegram Server.</p>
  
  <p>Here it is the scheme:</p>
  
  <p>TelegramClient &lt;------- socket_1[(session_1)] --------> TelegramServer</p>
  
  <p>The problem is that in order to receive constantly updates from telegram server I use a while(true) cycle that 
  basically is schematized as:</p>
  
  <pre><code>while(true){
      data_cipher = await socket_1.receive() // listen for new stuff from the socket_1
      data_plain  = decrypt(data_cipher)  // decrypt 
      processUpdate(data_plain)  // process the update
  }
  </code></pre>
  
  <p>Now if I want ,for example, query telegram servers for the list of all the chats in which am I registered, I have 
  to access the socket_1 in order to send this request, and wait for the answer, but socket_1 is in listening and I obviously can't access it.</p>
  
  <p>One solution could be to use a vector of request that will be processed after an update has been received,
  the idea is something like this:</p>
  
  <pre><code>List&lt;Request&gt; pending_requests = new List&lt;Request&gt;() // list of requests added by another thread 
  
      while(true){
  
          data_cipher = await socket_1.receive() // listen for new stuff from the socket_1
          data_plain  = decrypt(data_cipher)  // decrypt 
              processUpdate(data_plain)  // process the update
  
          if(pending_requests.Count != 0){
              foreach(Request r in pending_requests ){
                       processRequest(r)
                  }
              }
      }
  </code></pre>
  
  <p>This solution is quite horrible, since we process a request only after an update, and so no update = no request processed...</p>
  
  <p>Another possibility could be to use some kind of lock mechanism following a scheme like this:</p>
  
  <pre><code>//Thread_updater
  //--------------------------------------------
  while(true){
  
          lock(socket_1){
      data_cipher = await socket_1.receive() // listen for new stuff from the socket_1
      }
  
      data_plain  = decrypt(data_cipher)  // decrypt 
          handleUpdate(data_plain)  // process the update
  
  }
  --------------------------------------------
  
  
  //Thread_requests
  //--------------------------------------------
  Request r = new Request(&lt;stuff&gt;);
  
  lock(socket_1){
     await sendRequest(r,socket_1)
  }
  
  --------------------------------------------
  </code></pre>
  
  <p>The big problem about this is that once the Thread_updater takes the lock, it will never release it until an update has been received... this is basically the same problem as before.
  I've tried also to play with CancellationTasks or Socket Timeout, but I felt like I'm taking a wrong path.</p>
  
  <p>Is there an elegant solution/pattern in order to handle this in a neat way? 
  As said , I don't want to open 2 sessions since it is logically wrong ( it would be like having two clients in order to handle receiving of updates and sending messages ).</p>