Chimney.MPD.Net.Connection.Connect C# (CSharp) Метод

Connect() публичный Метод

public Connect ( string host, string port, int timeout ) : Task
host string
port string
timeout int
Результат Task
        public async Task<bool> Connect(string host, string port, int timeout = 0)
        {
            _streamSocket = new StreamSocket();
            _streamSocket.Control.KeepAlive = true;
            _streamSocket.Control.QualityOfService = SocketQualityOfService.Normal;

            var cts = new CancellationTokenSource();

            _host = host;
            _port = port;

            try
            {
                if(timeout > 0)
                {
                    cts.CancelAfter(timeout);
                    await _streamSocket.ConnectAsync(new HostName(host), port).AsTask(cts.Token);
                }
                else
                    await _streamSocket.ConnectAsync(new HostName(host), port);

                _Connected = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);

                Close();

                _streamSocket = null;

                _host = null;
                _port = null;

                _Connected = false;
            }

            return _Connected;
        }

Usage Example

Пример #1
0
        public async Task<Tuple<bool, bool>> TestConnect(string host, string port, string password = null, bool silent = false, int timeout = 0)
        {
            this.host = host;
            this.port = port;
            this.password = password;

            if (_connection != null)
            {
                _connection.Close();
                Debug.WriteLine(this.name + " : CLOSE CONNECTION");
            }

            _connection = new Connection();

            var permission = true;

            bool success = await _connection.Connect(host, port, timeout);

            Debug.WriteLine(this.name + " : NEW CONNECTION : " + success);

            if (success)
            {
                var response = await Connection.Recive(_connection.Socket,
                    new List<string>() { MPDKeyWords.Response.SUCCESS_CONNECT },
                    new List<string>() { MPDKeyWords.Response.OK + MPDKeyWords.Response.LINEBREAK },
                    new List<string>() { MPDKeyWords.Response.ACK },
                    new List<string>() { MPDKeyWords.Response.LINEBREAK });

                if (string.IsNullOrEmpty(response) || !response.StartsWith(MPDKeyWords.Response.SUCCESS_CONNECT))
                    success = false;

                Debug.WriteLine(this.name + " : NEW CONNECTION : RESPONSE : " + success);

                if (success)
                {
                    await Password(password, false);

                    if (await TestPermission() == null)
                        permission = false;
                }

                Debug.WriteLine(this.name + " : NEW CONNECTION : PERMISSION : " + permission);
            }

            _connection.Close();

            Debug.WriteLine(this.name + " : NEW CONNECTION : RETURN : " + success + " " + permission);

            return new Tuple<bool, bool>(success, permission);
        }
All Usage Examples Of Chimney.MPD.Net.Connection::Connect