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