Пример #1
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);
        }
Пример #2
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]);
        }
Пример #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());
        }
Пример #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);
        }
Пример #5
0
		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);
		}
Пример #6
0
        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));
        }