protected override async Task <SourceConnectionClient> DoConnect(Uri source, CancellationToken cancel_token)
 {
     try {
         var port = source.Port < 0 ? PCPVersion.DefaultPort : source.Port;
         if (source.HostNameType == UriHostNameType.IPv4 ||
             source.HostNameType == UriHostNameType.IPv6)
         {
             var addr = IPAddress.Parse(source.Host);
             client = new TcpClient(addr.AddressFamily);
             await client.ConnectAsync(addr, port).ConfigureAwait(false);
         }
         else
         {
             client = new TcpClient(Channel.NetworkAddressFamily);
             await client.ConnectAsync(source.DnsSafeHost, port).ConfigureAwait(false);
         }
         var connection = new SourceConnectionClient(client);
         connection.Stream.ReadTimeout  = 30000;
         connection.Stream.WriteTimeout = 8000;
         remoteHost = new DnsEndPoint(source.Host, port);
         Logger.Debug("Connected: {0}", source);
         return(connection);
     }
     catch (SocketException e) {
         Logger.Debug("Connection Failed: {0}", source);
         Logger.Debug(e);
         return(null);
     }
 }
Example #2
0
        protected async Task <SourceConnectionClient> DoConnectGiv(Uri source, CancellationToken cancel_token)
        {
            Logger.Debug("DoConnectGiv");
            AddressFamily family = AddressFamily.InterNetwork;

            if (source.HostNameType == UriHostNameType.IPv6)
            {
                family = AddressFamily.InterNetworkV6;
            }

            Socket socket;

            socket = PeerCast.ReceiveGivSocket(Guid.Parse(source.LocalPath.Substring("/channel/".Length)), family);
            if (socket != null)
            {
                var client = new TcpClient();
                client.Client = socket;

                var connection = new SourceConnectionClient(client);
                connection.Stream.ReadTimeout  = 30000;
                connection.Stream.WriteTimeout = 8000;

                remoteHost = socket.RemoteEndPoint;

                this.client = client;

                return(connection);
            }
            else
            {
                return(null);
            }
        }
        protected override async Task <SourceConnectionClient> DoConnect(Uri source, CancellationToken cancellationToken)
        {
            TcpClient client    = null;
            var       bind_addr = GetBindAddresses(source);

            if (bind_addr.Count() == 0)
            {
                this.state = ConnectionState.Error;
                throw new BindErrorException(String.Format("Cannot resolve bind address: {0}", source.DnsSafeHost));
            }
            var listeners = bind_addr.Select(addr => {
                var listener = new TcpListener(addr);
                if (addr.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, true);
                }
                return(listener);
            }).ToArray();

            try {
                var cancel_task = cancellationToken.CreateCancelTask <TcpClient>();
                var tasks       = listeners.Select(listener => {
                    listener.Start(1);
                    Logger.Debug("Listening on {0}", listener.LocalEndpoint);
                    return(listener.AcceptTcpClientAsync());
                }).Concat(Enumerable.Repeat(cancel_task, 1)).ToArray();
                var result = await Task.WhenAny(tasks).ConfigureAwait(false);

                if (!result.IsCanceled)
                {
                    client = result.Result;
                    Logger.Debug("Client accepted");
                }
                else
                {
                    Logger.Debug("Listen cancelled");
                }
            }
            catch (SocketException) {
                this.state = ConnectionState.Error;
                throw new BindErrorException(String.Format("Cannot bind address: {0}", bind_addr));
            }
            finally {
                foreach (var listener in listeners)
                {
                    listener.Stop();
                }
            }
            if (client != null)
            {
                var c = new SourceConnectionClient(client);
                c.Stream.CloseTimeout = 0;
                return(c);
            }
            else
            {
                return(null);
            }
        }
        protected virtual async Task DoClose(SourceConnectionClient connection)
        {
            await connection.Stream.FlushAsync().ConfigureAwait(false);

            connection.Dispose();
            Logger.Debug("closed");
            this.Status = ConnectionStatus.Error;
        }
Example #5
0
        protected async Task <SourceConnectionClient> DoConnectPcp(Uri source, CancellationToken cancel_token)
        {
            Logger.Debug("DoConnectPcp");
            TcpClient client = null;

            try {
                var port = source.Port < 0 ? PCPVersion.DefaultPort : source.Port;
                if (source.HostNameType == UriHostNameType.IPv4 ||
                    source.HostNameType == UriHostNameType.IPv6)
                {
                    var addr = IPAddress.Parse(source.Host);
                    client = new TcpClient(addr.AddressFamily);
                    using (var cs = CancellationTokenSource.CreateLinkedTokenSource(cancel_token)) {
                        cs.CancelAfter(3000);
                        var task = await Task.WhenAny(
                            cs.Token.CreateCancelTask(),
                            client.ConnectAsync(addr, port)
                            ).ConfigureAwait(false);

                        await task.ConfigureAwait(false);
                    }
                }
                else
                {
                    client = new TcpClient(Channel.NetworkAddressFamily);
                    using (var cs = CancellationTokenSource.CreateLinkedTokenSource(cancel_token)) {
                        cs.CancelAfter(3000);
                        var task = await Task.WhenAny(
                            cs.Token.CreateCancelTask(),
                            client.ConnectAsync(source.DnsSafeHost, port)
                            ).ConfigureAwait(false);

                        await task.ConfigureAwait(false);
                    }
                }
                var connection = new SourceConnectionClient(client);
                connection.Stream.ReadTimeout  = 30000;
                connection.Stream.WriteTimeout = 8000;
                remoteHost = new DnsEndPoint(source.Host, port);
                Logger.Debug("Connected: {0}", source);
                this.client = client;
                return(connection);
            }
            catch (OperationCanceledException) {
                client?.Close();
                Logger.Debug("Connection Cancelled: {0}", source);
                return(null);
            }
            catch (SocketException e) {
                Logger.Debug("Connection Failed: {0}", source);
                Logger.Debug(e);
                return(null);
            }
        }
        protected async Task <SourceConnectionClient> DoConnect(IPEndPoint endpoint)
        {
            try {
                client = new TcpClient(endpoint.AddressFamily);
                var connection = new SourceConnectionClient(client);
                await client.ConnectAsync(endpoint.Address, endpoint.Port).ConfigureAwait(false);

                connection.Stream.ReadTimeout  = 30000;
                connection.Stream.WriteTimeout = 8000;
                remoteHost = endpoint;
                Logger.Debug("Connected: {0}", endpoint);
                return(connection);
            }
            catch (SocketException e) {
                Logger.Debug("Connection Failed: {0}", endpoint);
                Logger.Debug(e);
                return(null);
            }
        }
 protected override async Task <SourceConnectionClient> DoConnect(Uri source, CancellationToken cancel_token)
 {
     try {
         var client = new TcpClient();
         if (source.HostNameType == UriHostNameType.IPv4 ||
             source.HostNameType == UriHostNameType.IPv6)
         {
             await client.ConnectAsync(IPAddress.Parse(source.Host), source.Port).ConfigureAwait(false);
         }
         else
         {
             await client.ConnectAsync(source.DnsSafeHost, source.Port).ConfigureAwait(false);
         }
         var connection = new SourceConnectionClient(client);
         connection.Stream.ReadTimeout  = 10000;
         connection.Stream.WriteTimeout = 8000;
         return(connection);
     }
     catch (SocketException e) {
         Logger.Error(e);
         return(null);
     }
 }
        public async Task <StopReason> Run()
        {
            this.Status = ConnectionStatus.Connecting;
            try {
                connection = await DoConnect(SourceUri, isStopped.Token).ConfigureAwait(false);

                if (connection == null)
                {
                    Stop(StopReason.ConnectionError);
                }
            }
            catch (OperationCanceledException) {
                connection = null;
                Stop(StopReason.UserShutdown);
            }
            catch (BindErrorException e) {
                connection = null;
                Logger.Error(e);
                Stop(StopReason.NoHost);
            }
            if (!IsStopped)
            {
                OnStarted();
                try {
                    await DoProcess(isStopped.Token).ConfigureAwait(false);
                }
                catch (OperationCanceledException) {
                }
                OnStopped();
            }
            if (connection != null)
            {
                await DoClose(connection).ConfigureAwait(false);
            }
            return(StoppedReason);
        }
 protected virtual async Task DoClose(SourceConnectionClient connection)
 {
   await connection.Stream.FlushAsync();
   connection.Dispose();
   Logger.Debug("closed");
   this.Status = ConnectionStatus.Error;
 }
 public async Task<StopReason> Run()
 {
   this.Status = ConnectionStatus.Connecting;
   try {
     connection = await DoConnect(SourceUri, isStopped.Token);
   }
   catch (OperationCanceledException) {
     connection = null;
   }
   if (connection==null) {
     Stop(StopReason.ConnectionError);
   }
   if (!IsStopped) {
     OnStarted();
     try {
       await DoProcess(isStopped.Token);
     }
     catch (OperationCanceledException) {
     }
     OnStopped();
   }
   if (connection!=null) {
     await DoClose(connection);
   }
   return StoppedReason;
 }
 protected override async Task<SourceConnectionClient> DoConnect(Uri source, CancellationToken cancel_token)
 {
   try {
     var port = source.Port<0 ? PCPVersion.DefaultPort : source.Port;
     client = new TcpClient();
     if (source.HostNameType==UriHostNameType.IPv4 ||
         source.HostNameType==UriHostNameType.IPv6) {
       await client.ConnectAsync(IPAddress.Parse(source.Host), port);
     }
     else {
       await client.ConnectAsync(source.DnsSafeHost, port);
     }
     var connection = new SourceConnectionClient(client);
     connection.Stream.ReadTimeout  = 30000;
     connection.Stream.WriteTimeout = 8000;
     remoteHost = new DnsEndPoint(source.Host, port);
     Logger.Debug("Connected: {0}", source);
     return connection;
   }
   catch (SocketException e) {
     Logger.Debug("Connection Failed: {0}", source);
     Logger.Debug(e);
     return null;
   }
 }
 protected async Task<SourceConnectionClient> DoConnect(IPEndPoint endpoint)
 {
   try {
     client = new TcpClient();
     var connection = new SourceConnectionClient(client);
     await client.ConnectAsync(endpoint.Address, endpoint.Port);
     connection.Stream.ReadTimeout  = 30000;
     connection.Stream.WriteTimeout = 8000;
     remoteHost = endpoint;
     Logger.Debug("Connected: {0}", endpoint);
     return connection;
   }
   catch (SocketException e) {
     Logger.Debug("Connection Failed: {0}", endpoint);
     Logger.Debug(e);
     return null;
   }
 }