private async void UpnpClient_OnDeviceFound(object Sender, DeviceLocationEventArgs e) { try { lock (this.ipAddressesFound) { if (this.ipAddressesFound.ContainsKey(e.RemoteEndPoint.Address)) { return; } this.ipAddressesFound[e.RemoteEndPoint.Address] = true; } DeviceDescriptionDocument Doc = await e.Location.GetDeviceAsync(); if (Doc != null) { UPnPService Service = Doc.GetService("urn:schemas-upnp-org:service:WANIPConnection:1"); if (Service == null) { Service = Doc.GetService("urn:schemas-upnp-org:service:WANIPConnection:2"); if (Service == null) { return; } } ServiceDescriptionDocument Scpd = await Service.GetServiceAsync(); this.ServiceRetrieved(Scpd, e.LocalEndPoint); } } catch (Exception ex) { this.exception = ex; this.State = PeerToPeerNetworkState.Error; } }
private void UpnpClient_OnDeviceFound(UPnPClient Sender, DeviceLocationEventArgs e) { try { lock (this.ipAddressesFound) { if (this.ipAddressesFound.ContainsKey(e.RemoteEndPoint.Address)) { return; } this.ipAddressesFound[e.RemoteEndPoint.Address] = true; } e.Location.StartGetDevice(this.DeviceRetrieved, e); } catch (Exception ex) { this.exception = ex; this.State = PeerToPeerNetworkState.Error; } }
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>(); ushort PortMappingIndex; this.serviceWANIPConnectionV1 = new WANIPConnectionV1(e.ServiceDescriptionDocument); this.State = PeerToPeerNetworkState.RegisteringApplicationInGateway; this.serviceWANIPConnectionV1.GetExternalIPAddress(out string NewExternalIPAddress); this.externalAddress = IPAddress.Parse(NewExternalIPAddress); PortMappingIndex = 0; try { while (true) { this.serviceWANIPConnectionV1.GetGenericPortMappingEntry(PortMappingIndex, out string NewRemoteHost, out ushort NewExternalPort, out string NewProtocol, out ushort NewInternalPort, out string NewInternalClient, out bool NewEnabled, out string NewPortMappingDescription, out uint 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 is 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; } }