Ejemplo n.º 1
0
 private static void ProcessResponse <T>(HttpWebRequest request, RequestState <T> state)
 {
     try
     {
         using (var response = (HttpWebResponse)request.GetResponse())
         {
             if (state.Callback != null)
             {
                 state.Callback(Response <T> .CreateSuccess(GetResponseBody(response)));
             }
         }
     }
     catch (Exception ex)
     {
         if (state.Callback != null)
         {
             state.Callback(Response <T> .CreateError(HandleException(ex)));
         }
     }
 }
Ejemplo n.º 2
0
        /**
         * Invoked when the request channel is closed.
         */
        protected void CloseHandler(object o, EventArgs args)
        {
            RequestState request = null;

            lock (_sync) {
                //retrieve the request state assiciated with the closed channel
                request = (RequestState)_channel_to_state[o];
                if (request == null)
                {
                    //cannot happen
                    Console.Error.WriteLine("VTS unable to retrieve request for a closed channel");
                    return;
                }
#if VTS_DEBUG
                Console.Error.WriteLine("VTS local: {0}, start: {1} channel closed.", _node.Address, request.Start);
#endif
                _channel_to_state.Remove(o);
                _num_requests--;
            }

            /**
             * The request object has been removed the shared data structure.
             *  Since no one alse has access to it, it can be accessed outside the lock.
             */

            SortedList sorted_result = new SortedList();
#if VTS_DEBUG
            SortedList sorted_stat = new SortedList();
#endif
            NCService.VivaldiState local_vs = _nc_service.State;
            foreach (Address target in request.ResultTable.Keys)
            {
                object[] curr_result      = (object[])request.ResultTable[target];
                NCService.VivaldiState vs = (NCService.VivaldiState)curr_result[0];
                double d = local_vs.Position.GetEucledianDistance(vs.Position);
                sorted_result[d] = target;
#if VTS_DEBUG
                Console.Error.WriteLine("VTS local: {0}, start: {1}, dest: {2}, distance: {3}",
                                        _node.Address, request.Start, target, d);
                string host = (string)curr_result[1];
                sorted_stat[d] = new object[] { d, host };
#endif
            }

#if VTS_DEBUG
            lock (_sync) {
                _query_list.Add(sorted_stat);
            }
#endif
            request.Callback(request.Start, sorted_result, request.Current);
        }
Ejemplo n.º 3
0
        private static void GetResponseCallback(IAsyncResult result)
        {
            RequestState    state        = (RequestState)result.AsyncState;
            HttpWebResponse httpResponse = null;

            try
            {
                using (System.Threading.Timer timeoutTimer = new System.Threading.Timer(new System.Threading.TimerCallback(o => state.Request.Abort())))
                {
                    httpResponse = (HttpWebResponse)state.Request.EndGetResponse(result);
                }
            }
            catch (WebException ex)
            {
                StringBuilder error = new StringBuilder();

                error.AppendLine("URI: {0}".FormatString(state.Request.RequestUri));
                error.AppendLine("Method: {0}".FormatString(state.Request.Method));

                //If there was a web exception, we'll still try to parse the response.
                if (ex.Response != null)
                {
                    using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        error.AppendLine(reader.ReadToEnd());
                    }
                }

                state.OnError(new ApplicationException(error.ToString(), ex));
                return;
            }

            if (state.Callback != null)
            {
                state.Callback(httpResponse);
            }
        }