Esempio n. 1
0
        private void EndMessageInternal(IAsyncResult result)
        {
            HttpWebResponse    response      = null;
            PortMapAsyncResult mappingResult = result.AsyncState as PortMapAsyncResult;

            try
            {
                try
                {
                    response = (HttpWebResponse)mappingResult.Request.EndGetResponse(result);
                }
                catch (WebException ex)
                {
                    // Even if the request "failed" i want to continue on to read out the response from the router
                    response = ex.Response as HttpWebResponse;
                    if (response == null)
                    {
                        mappingResult.SavedMessage = new ErrorMessage((int)ex.Status, ex.Message);
                    }
                }
                if (response != null)
                {
                    mappingResult.SavedMessage = DecodeMessageFromResponse(response.GetResponseStream(), response.ContentLength);
                }
            }

            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }
Esempio n. 2
0
        private void CompleteMessage(IAsyncResult result)
        {
            PortMapAsyncResult mappingResult = result.AsyncState as PortMapAsyncResult;

            mappingResult.CompletedSynchronously = result.CompletedSynchronously;
            mappingResult.Complete();
        }
Esempio n. 3
0
        /// <summary>
        /// Ends an async request to get the external ip address of the router
        /// </summary>
        public override IPAddress EndGetExternalIP(IAsyncResult result)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            PortMapAsyncResult mappingResult = result as PortMapAsyncResult;

            if (mappingResult == null)
            {
                throw new ArgumentException("Invalid AsyncResult", "result");
            }

            if (!result.IsCompleted)
            {
                result.AsyncWaitHandle.WaitOne();
            }

            if (mappingResult.SavedMessage is ErrorMessage)
            {
                ErrorMessage msg = mappingResult.SavedMessage as ErrorMessage;
                throw new MappingException(msg.ErrorCode, msg.Description);
            }

            return(((GetExternalIPAddressResponseMessage)mappingResult.SavedMessage).ExternalIPAddress);
        }
Esempio n. 4
0
        private IAsyncResult BeginMessageInternal(MessageBase message, AsyncCallback storedCallback, object asyncState, AsyncCallback callback)
        {
            byte[]             body;
            WebRequest         request       = message.Encode(out body);
            PortMapAsyncResult mappingResult = PortMapAsyncResult.Create(message, request, storedCallback, asyncState);

            if (body.Length > 0)
            {
                request.ContentLength = body.Length;
                request.BeginGetRequestStream(delegate(IAsyncResult result) {
                    try
                    {
                        Stream s = request.EndGetRequestStream(result);
                        s.Write(body, 0, body.Length);
                        request.BeginGetResponse(callback, mappingResult);
                    }
                    catch (Exception ex)
                    {
                        mappingResult.Complete(ex);
                    }
                }, null);
            }
            else
            {
                request.BeginGetResponse(callback, mappingResult);
            }
            return(mappingResult);
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        public override void EndDeletePortMap(IAsyncResult result)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            PortMapAsyncResult mappingResult = result as PortMapAsyncResult;

            if (mappingResult == null)
            {
                throw new ArgumentException("Invalid AsyncResult", "result");
            }

            // Check if we need to wait for the operation to finish
            if (!mappingResult.IsCompleted)
            {
                mappingResult.AsyncWaitHandle.WaitOne();
            }

            // If we have a saved exception, it means something went wrong during the mapping
            // so we just rethrow the exception and let the user figure out what they should do.
            if (mappingResult.SavedMessage is ErrorMessage)
            {
                ErrorMessage msg = mappingResult.SavedMessage as ErrorMessage;
                throw new MappingException(msg.ErrorCode, msg.Description);
            }

            // If all goes well, we just return
            //return true;
        }