Ejemplo n.º 1
0
        /// <summary>
        /// Connect to a VNC Host and determine which type of Authentication it uses. If the host uses Password Authentication, a call to Authenticate() will be required.
        /// </summary>
        /// <param name="host">The IP Address or Host Name of the VNC Host.</param>
        /// <param name="display">The Display number (used on Unix hosts).</param>
        /// <param name="port">The Port number used by the Host, usually 5900.</param>
        /// <param name="viewOnly">True if mouse/keyboard events are to be ignored.</param>
        /// <returns>Returns True if the VNC Host requires a Password to be sent after Connect() is called, otherwise False.</returns>
        public async Task <bool> ConnectAsync(string host, int display, int port, bool viewOnly, CancellationToken ct = default)
        {
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }

            // If a diplay number is specified (used to connect to Unix servers)
            // it must be 0 or greater.  This gets added to the default port number
            // in order to determine where the server will be listening for connections.
            if (display < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(display), display, "Display number must be non-negative.");
            }
            port += display;

            this.CreateRfbProtocol(viewOnly);

            // Connect and determine version of server, and set client protocol version to match
            try
            {
                await rfb.ConnectAsync(host, port, ct).ConfigureAwait(false);

                return(await this.ContinueConnectAsync(ct).ConfigureAwait(false));
            } catch (Exception e) {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }