Beispiel #1
0
		private void DownloadServiceCompleted(object sender, DownloadDataCompletedEventArgs e)
		{
			object[] P = (object[])e.UserState;
			UPnPService Service = (UPnPService)P[0];
			ServiceDescriptionEventHandler Callback = (ServiceDescriptionEventHandler)P[1];
			object State = P[2];
			ServiceDescriptionEventArgs e2;

			if (e.Error != null)
				e2 = new ServiceDescriptionEventArgs(e.Error, this, State);
			else
			{
				try
				{
					XmlDocument Xml = new XmlDocument();
					Xml.Load(new MemoryStream(e.Result));

					ServiceDescriptionDocument ServiceDoc = new ServiceDescriptionDocument(Xml, this, Service);
					e2 = new ServiceDescriptionEventArgs(ServiceDoc, this, State);
				}
				catch (Exception ex)
				{
					this.RaiseOnError(ex);
					e2 = new ServiceDescriptionEventArgs(e.Error, this, State);
				}
				finally
				{
					WebClient Client = sender as WebClient;
					if (Client != null)
						Client.Dispose();
				}
			}

			try
			{
				Callback(this, e2);
			}
			catch (Exception ex)
			{
				this.RaiseOnError(ex);
			}
		}
		private void ServiceRetrieved(object Sender, ServiceDescriptionEventArgs e)
		{
			try
			{
				DeviceLocationEventArgs e2 = (DeviceLocationEventArgs)e.State;
				Dictionary<ushort, bool> TcpPortMapped = new Dictionary<ushort, bool>();
				Dictionary<ushort, bool> UdpPortMapped = new Dictionary<ushort, bool>();
				string NewExternalIPAddress;
				ushort PortMappingIndex;
				string NewRemoteHost;
				ushort NewExternalPort;
				string NewProtocol;
				ushort NewInternalPort;
				string NewInternalClient;
				bool NewEnabled;
				string NewPortMappingDescription;
				uint NewLeaseDuration;

				this.serviceWANIPConnectionV1 = new WANIPConnectionV1(e.ServiceDescriptionDocument);
				this.State = PeerToPeerNetworkState.RegisteringApplicationInGateway;

				this.serviceWANIPConnectionV1.GetExternalIPAddress(out NewExternalIPAddress);
				this.externalAddress = IPAddress.Parse(NewExternalIPAddress);

				PortMappingIndex = 0;

				try
				{
					while (true)
					{
						this.serviceWANIPConnectionV1.GetGenericPortMappingEntry(PortMappingIndex, out NewRemoteHost,
							out NewExternalPort, out NewProtocol, out NewInternalPort, out NewInternalClient,
							out NewEnabled, out NewPortMappingDescription, out NewLeaseDuration);

						if (NewPortMappingDescription == this.applicationName && NewInternalClient == e2.LocalEndPoint.Address.ToString())
							this.serviceWANIPConnectionV1.DeletePortMapping(NewRemoteHost, NewExternalPort, NewProtocol);
						else
						{
							switch (NewProtocol)
							{
								case "TCP":
									TcpPortMapped[NewExternalPort] = true;
									break;

								case "UDP":
									UdpPortMapped[NewExternalPort] = true;
									break;
							}

							PortMappingIndex++;
						}
					}
				}
				catch (UPnPException)
				{
					// No more entries.
				}

				this.localAddress = e2.LocalEndPoint.Address;
				ushort LocalPort;
				int i;

				do
				{
					this.tcpListener = new TcpListener(this.localAddress, this.desiredPort);
					this.tcpListener.Start(this.backlog);

					i = ((IPEndPoint)this.tcpListener.LocalEndpoint).Port;
					LocalPort = (ushort)(i);
					if (i < 0 || i > ushort.MaxValue || TcpPortMapped.ContainsKey(LocalPort) || UdpPortMapped.ContainsKey(LocalPort))
					{
						this.tcpListener.Stop();
						this.tcpListener = null;

						if (this.desiredPort != 0)
							throw new ArgumentException("Port already assigned to another application in the network.", "Port");
					}
					else
					{
						try
						{
							this.udpClient = new UdpClient(this.tcpListener.LocalEndpoint.AddressFamily);
							this.udpClient.Client.Bind((IPEndPoint)this.tcpListener.LocalEndpoint);
						}
						catch (Exception)
						{
							this.tcpListener.Stop();
							this.tcpListener = null;
						}
					}
				}
				while (this.tcpListener == null);

				this.localEndpoint = new IPEndPoint(this.localAddress, LocalPort);

				this.serviceWANIPConnectionV1.AddPortMapping(string.Empty, LocalPort, "TCP", LocalPort, LocalAddress.ToString(), true, this.applicationName, 0);
				this.tcpMappingAdded = true;

				this.serviceWANIPConnectionV1.AddPortMapping(string.Empty, LocalPort, "UDP", LocalPort, LocalAddress.ToString(), true, this.applicationName, 0);
				this.udpMappingAdded = true;

				this.externalEndpoint = new IPEndPoint(this.externalAddress, LocalPort);
				this.State = PeerToPeerNetworkState.Ready;

				this.tcpListener.BeginAcceptTcpClient(this.EndAcceptTcpClient, null);
				this.udpClient.BeginReceive(this.EndReceiveUdp, null);
			}
			catch (Exception ex)
			{
				this.exception = ex;
				this.State = PeerToPeerNetworkState.Error;
			}
		}