Ejemplo n.º 1
0
 public void Start(int port = DEFAULT_PORT)
 {
     if (Interlocked.Exchange(ref started, 1) == 0)
     {
         IPAddress address = LocalAddresses.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork && !IsIntranetAddress(p));
         if (address == null && UpnpEnabled && UPnP.Discover())
         {
             try
             {
                 address = UPnP.GetExternalIP();
                 UPnP.ForwardPort(port, ProtocolType.Tcp, "AntShares");
                 LocalAddresses.Add(address);
             }
             catch { }
         }
         listener = new TcpListener(IPAddress.Any, port);
         try
         {
             listener.Start();
             Port = (ushort)port;
         }
         catch (SocketException) { }
         connectThread.Start();
         if (Port > 0)
         {
             listenerThread.Start();
         }
     }
 }
Ejemplo n.º 2
0
 public void Start(int port)
 {
     if (Interlocked.Exchange(ref started, 1) == 0)
     {
         Task.Run(async() =>
         {
             IPAddress address = LocalAddresses.FirstOrDefault(p => p.IsIPv4MappedToIPv6 && !IsIntranetAddress(p));
             if (address == null && UpnpEnabled && await UPnP.DiscoverAsync())
             {
                 try
                 {
                     address = await UPnP.GetExternalIPAsync();
                     await UPnP.ForwardPortAsync(port, ProtocolType.Tcp, "AntShares");
                     LocalAddresses.Add(address);
                 }
                 catch { }
             }
             listener = new TcpListener(IPAddress.Any, port);
             try
             {
                 listener.Start();
                 Port = (ushort)port;
             }
             catch (SocketException) { }
             connectThread.Start();
             poolThread?.Start();
             if (Port > 0)
             {
                 await AcceptPeersAsync();
             }
         });
     }
 }
Ejemplo n.º 3
0
 public void Start(int port = 0, int ws_port = 0)
 {
     if (Interlocked.Exchange(ref started, 1) == 0)
     {
         Task.Run(async() =>
         {
             if (port > 0 || ws_port > 0)
             {
                 IPAddress address = LocalAddresses.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork && !IsIntranetAddress(p));
                 if (address == null && UpnpEnabled && await UPnP.DiscoverAsync())
                 {
                     try
                     {
                         address = await UPnP.GetExternalIPAsync();
                         if (port > 0)
                         {
                             await UPnP.ForwardPortAsync(port, ProtocolType.Tcp, "AntShares");
                         }
                         if (ws_port > 0)
                         {
                             await UPnP.ForwardPortAsync(ws_port, ProtocolType.Tcp, "AntShares WebSocket");
                         }
                         LocalAddresses.Add(address);
                     }
                     catch { }
                 }
             }
             connectThread.Start();
             poolThread?.Start();
             if (port > 0)
             {
                 listener = new TcpListener(IPAddress.Any, port);
                 try
                 {
                     listener.Start();
                     Port = (ushort)port;
                     AcceptPeersAsync();
                 }
                 catch (SocketException) { }
             }
             if (ws_port > 0)
             {
                 ws_host = new WebHostBuilder().UseKestrel().UseUrls($"http://*:{ws_port}").Configure(app => app.UseWebSockets().Run(ProcessWebSocketAsync)).Build();
                 ws_host.Start();
             }
         });
     }
 }
Ejemplo n.º 4
0
 public void Start(int port = DEFAULT_PORT)
 {
     if (Interlocked.Exchange(ref started, 1) == 0)
     {
         IPHostEntry localhost = null;
         try
         {
             localhost = Dns.GetHostEntry(Dns.GetHostName());
         }
         catch (SocketException) { }
         IPAddress address = localhost?.AddressList.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p) && !IsIntranetAddress(p));
         if (address == null && UpnpEnabled && UPnP.Discover())
         {
             try
             {
                 address = UPnP.GetExternalIP();
                 UPnP.ForwardPort(port, ProtocolType.Tcp, "AntShares");
             }
             catch
             {
                 address = null;
             }
         }
         if (address != null && !IsIntranetAddress(address))
         {
             listener = new TcpListener(IPAddress.Any, port);
             try
             {
                 listener.Start();
                 LocalEndpoint = new IPEndPoint(address.MapToIPv6(), port);
             }
             catch (SocketException) { }
         }
         connectThread.Start();
         if (LocalEndpoint == null)
         {
             return;
         }
         listenerThread.Start();
     }
 }