Ejemplo n.º 1
0
        public override Mapping EndGetSpecificMapping(IAsyncResult result)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            GetAllMappingsAsyncResult mappingResult = result as GetAllMappingsAsyncResult;

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

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

            if (mappingResult.SavedMessage is ErrorMessage)
            {
                ErrorMessage message = mappingResult.SavedMessage as ErrorMessage;
                if (message.ErrorCode != 0x2ca)
                {
                    throw new MappingException(message.ErrorCode, message.Description);
                }
            }
            if (mappingResult.Mappings.Count == 0)
            {
                return(new Mapping(Protocol.Tcp, -1, -1));
            }

            return(mappingResult.Mappings[0]);
        }
Ejemplo n.º 2
0
        private void EndGetAllMappingsInternal(IAsyncResult result)
        {
            EndMessageInternal(result);

            GetAllMappingsAsyncResult mappingResult           = result.AsyncState as GetAllMappingsAsyncResult;
            GetGenericPortMappingEntryResponseMessage message = mappingResult.SavedMessage as GetGenericPortMappingEntryResponseMessage;

            if (message != null)
            {
                Mapping mapping = new Mapping(message.Protocol, message.InternalPort, message.ExternalPort, message.LeaseDuration);
                mapping.Description = message.PortMappingDescription;
                mappingResult.Mappings.Add(mapping);
                GetGenericPortMappingEntry next = new GetGenericPortMappingEntry(mappingResult.Mappings.Count, this);

                // It's ok to do this synchronously because we should already be on anther thread
                // and this won't block the user.
                byte[]     body;
                WebRequest request = next.Encode(out body);
                if (body.Length > 0)
                {
                    request.ContentLength = body.Length;
                    request.GetRequestStream().Write(body, 0, body.Length);
                }
                mappingResult.Request = request;
                request.BeginGetResponse(EndGetAllMappingsInternal, mappingResult);
                return;
            }

            CompleteMessage(result);
        }
Ejemplo n.º 3
0
        public override Mapping[] EndGetAllMappings(IAsyncResult result)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            GetAllMappingsAsyncResult mappingResult = result as GetAllMappingsAsyncResult;

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

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

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

            return(mappingResult.Mappings.ToArray());
        }
Ejemplo n.º 4
0
        private void EndGetSpecificMappingInternal(IAsyncResult result)
        {
            EndMessageInternal(result);

            GetAllMappingsAsyncResult mappingResult           = result.AsyncState as GetAllMappingsAsyncResult;
            GetGenericPortMappingEntryResponseMessage message = mappingResult.SavedMessage as GetGenericPortMappingEntryResponseMessage;

            if (message != null)
            {
                Mapping mapping = new Mapping(mappingResult.SpecificMapping.Protocol, message.InternalPort, mappingResult.SpecificMapping.PublicPort, message.LeaseDuration);
                mapping.Description = mappingResult.SpecificMapping.Description;
                mappingResult.Mappings.Add(mapping);
            }

            CompleteMessage(result);
        }
        internal static PortMapAsyncResult Create(MessageBase message, WebRequest request, AsyncCallback storedCallback, object asyncState)
        {
            if (message is GetGenericPortMappingEntry)
            {
                return(new GetAllMappingsAsyncResult(request, storedCallback, asyncState));
            }

            if (message is GetSpecificPortMappingEntryMessage)
            {
                GetSpecificPortMappingEntryMessage mapMessage = (GetSpecificPortMappingEntryMessage)message;
                GetAllMappingsAsyncResult          result     = new GetAllMappingsAsyncResult(request, storedCallback, asyncState);

                result.SpecificMapping = new Mapping(mapMessage.protocol, 0, mapMessage.externalPort, 0);
                return(result);
            }

            return(new PortMapAsyncResult(request, storedCallback, asyncState));
        }