Example #1
0
        // returns the response
        public string Send(string theLocalPath, int theSequenceNumber, string theMessage)
        {
            ThreadSemaphore semaphore = null;

            lock (this)
            {
                // we use a lock here, to prevent concurrent requests
                // from getting mixed together on their way out
                connection.Connect();
                connection.LastActivity = DateTime.Now;
                HttpRequest request = new HttpRequest(connection.Socket);
                request.Post(theLocalPath, theSequenceNumber, theMessage);
                semaphore = blockedRequests.Add(theSequenceNumber, theMessage);
            }

            // block until the response arrives, or a timeout occurs
            if (!semaphore.Wait(rxTimeout))
            {
                throw new Exception("No response received");
            }

            // get the response from the semaphore
            byte[] response = semaphore.response;
            blockedRequests.Remove(theSequenceNumber);
            return(Encoding.UTF8.GetString(response));
        }
 public void Remove(int theSequenceNumber)
 {
     lock (this)
     {
         ThreadSemaphore semaphore = blockedRequests[theSequenceNumber] as ThreadSemaphore;
         blockedRequests.Remove(theSequenceNumber);
         semaphore.inUse = false;
     }
 }
 public RequestQueue()
 {
     // we support up to 500 concurrent requests. The value is arbitrary
     semaphores = new ThreadSemaphore[500];
     for (int i = 0; i < semaphores.Length; i++)
     {
         ThreadSemaphore semaphore = new ThreadSemaphore();
         semaphore.requestEvent = new ManualResetEvent(false);
         semaphores[i]          = semaphore;
     }
 }
 public ThreadSemaphore Add(int theSequenceNumber, string theRequest)
 {
     lock (this)
     {
         ThreadSemaphore semaphore = GetFirstAvailableSemaphore();
         blockedRequests.Add(theSequenceNumber, semaphore);
         semaphore.requestEvent.Reset();
         semaphore.sequenceNumber = theSequenceNumber;
         semaphore.request        = theRequest;
         semaphore.response       = null;
         semaphore.startTime      = DateTime.Now;
         return(semaphore);
     }
 }
Example #5
0
        void HandleResponse(Socket theSocket)
        {
            // read the response
            HttpResponse response = new HttpResponse(theSocket);

            response.Get();

            // get the semaphore for the blocked request thread
            ThreadSemaphore semaphore = blockedRequests.Get(response.SequenceNumber);

            if (semaphore == null)
            {
                throw new Exception("No pending request found for response");
            }

            // save the response in the semaphore
            semaphore.response = response.Body;

            // unblock the request thread waiting for this response
            semaphore.Signal();
        }