Пример #1
0
        /// <summary>
        ///  This MUST be an asynchronous call
        /// </summary>
        private void SendTrackerGet()
        {
            PrepareTrackerRequest();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(trackerGetRequest);
            requestState = new RequestState();
            // To store the request
            requestState.request = request;
            try {
                // Start the Async request
                IAsyncResult result = request.BeginGetResponse(new AsyncCallback(EndGetTrackerResponse), requestState);

                // We need a TimeOut
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TrackerGetTimeOut), requestState, requestTimeOut, true);

                // Signal the manual event to wait.
                allDone.WaitOne();
                requestState.response.Close();
                trackerResponse = null;
                trackerFailure = false;
                trackerFailureReason = string.Empty;
            }
            catch(WebException we) {
                throw new TrackerException("Error in GET to tracker. [" + we.Message + "].", we);
            }
        }
Пример #2
0
        private void EndGetTrackerResponse(IAsyncResult result)
        {
            requestState = (RequestState) result.AsyncState;
            try {
                WebRequest webRequest = requestState.request;
                requestState.response = webRequest.EndGetResponse(result);

                // Get the response
                requestState.streamResponse = requestState.response.GetResponseStream();

                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader sr = new StreamReader(requestState.streamResponse, encode);
                // Tracker response must have less than 256 bytes.
                char[] bufferRead = new char[512];
                int responseLength = sr.Read(bufferRead, 0, 512);
                trackerResponse = new byte[responseLength];
                for (int i = 0; i < responseLength; i++) {
                    trackerResponse[i] = (byte)bufferRead[i];
                }
                ProcessTrackerResponse();
            }
            catch (WebException we) {
                throw new TrackerException("Error getting response from tracker. [" + we.Message + "].", we);
            }
            allDone.Set();
        }