示例#1
0
        /// <summary>
        /// Wait for a connection from VNC Server.
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="viewOnly"></param>
        public void Listen(string host, int port, bool viewOnly)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            rfb = new RfbProtocol();

            if (viewOnly)
            {
                inputPolicy = new VncViewInputPolicy(rfb);
            }
            else
            {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try
            {
                rfb.RfbConnectedFromServer += new EventHandler(ConnectedFromServerEventHandler);
                rfb.FailedToListen         += new EventHandler(FailedToListenHandler);
                rfb.Listen(host, port);
            }
            catch (Exception)
            {
                if (ConnectionLost != null)
                {
                    ConnectionLost(this, EventArgs.Empty);
                }
            }
        }
 /// <summary>
 /// Changes the input mode to view-only or interactive.
 /// </summary>
 /// <param name="viewOnly">True if view-only mode is desired (no mouse/keyboard events will be sent).</param>
 public void SetInputMode(bool viewOnly)
 {
     if (viewOnly)
     {
         inputPolicy = new VncViewInputPolicy(rfb);
     }
     else
     {
         inputPolicy = new VncDefaultInputPolicy(rfb);
     }
 }
示例#3
0
        private void CreateRfbProtocol(bool viewOnly)
        {
            this.rfb = new RfbProtocol();

            this.viewOnlyMode = viewOnly;
            if (viewOnly)
            {
                this.inputPolicy = new VncViewInputPolicy(this.rfb);
            }
            else
            {
                this.inputPolicy = new VncDefaultInputPolicy(this.rfb);
            }
        }
示例#4
0
        /// <summary>
        /// Main Function for Connect Process.
        /// </summary>
        /// <param name="host"></param>
        /// <param name="display"></param>
        /// <param name="port"></param>
        /// <param name="viewOnly"></param>
        /// <param name="connectFromClient">Set true if connect from client to server. Set false if wait for a connection from server.</param>
        /// <returns></returns>
        private bool ConnectCore(string host, int display, ref int port, bool viewOnly, bool connectFromClient)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }

            // If a diplay number is specified (used to connectFromClient 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("display", display, "Display number must be non-negative.");
            }
            port += display;

            rfb = new RfbProtocol();

            if (viewOnly)
            {
                inputPolicy = new VncViewInputPolicy(rfb);
            }
            else
            {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try
            {
                if (connectFromClient)
                {
                    rfb.Connect(host, port);
                    return(DoHandShake());
                }
                else
                {
                    rfb.Listen(host, port);
                    return(true);
                }
            }
            catch (Exception e)
            {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }
示例#5
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 void Connect(string host, int display, int port, bool viewOnly)
        {
            if (host == null)
            {
                throw new ArgumentNullException("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("display", display, "Display number must be non-negative.");
            }
            port += display;

            rfb = new RfbProtocol();

            if (viewOnly)
            {
                inputPolicy = new VncViewInputPolicy(rfb);
            }
            else
            {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            this.host = host;
            this.port = port;

            // Lauch connecting thread
            connectingThread = new Thread(new ThreadStart(this.Connection));
            connectingThread.SetApartmentState(ApartmentState.STA);
            connectingThread.IsBackground = true;
            connectingThread.Start();
        }
示例#6
0
 /// <summary>
 /// Changes the input mode to view-only or interactive.
 /// </summary>
 /// <param name="viewOnly">True if view-only mode is desired (no mouse/keyboard events will be sent).</param>
 public void SetInputMode(bool viewOnly)
 {
     if (viewOnly)
         inputPolicy = new VncViewInputPolicy(rfb);
     else
         inputPolicy = new VncDefaultInputPolicy(rfb);
 }
示例#7
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 bool Connect(string host, int display, int port, bool viewOnly)
        {
            if (host == null) throw new ArgumentNullException("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("display", display, "Display number must be non-negative.");
            port += display;

            rfb = new RfbProtocol();

            if (viewOnly) {
                inputPolicy = new VncViewInputPolicy(rfb);
            } else {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try {
                rfb.Connect(host, port);
                rfb.ReadProtocolVersion();
                rfb.WriteProtocolVersion();

                // Figure out which type of authentication the server uses
                byte[] types = rfb.ReadSecurityTypes();

                // Based on what the server sends back in the way of supported Security Types, one of
                // two things will need to be done: either the server will reject the connection (i.e., type = 0),
                // or a list of supported types will be sent, of which we need to choose and use one.
                if (types.Length > 0) {
                    if (types[0] == 0) {
                        // The server is not able (or willing) to accept the connection.
                        // A message follows indicating why the connection was dropped.
                        throw new VncProtocolException("Connection Failed. The server rejected the connection for the following reason: " + rfb.ReadSecurityFailureReason());
                    } else {
                        securityType = GetSupportedSecurityType(types);
                        Debug.Assert(securityType > 0, "Unknown Security Type(s)", "The server sent one or more unknown Security Types.");

                        rfb.WriteSecurityType(securityType);

                        // Protocol 3.8 states that a SecurityResult is still sent when using NONE (see 6.2.1)
                        if (rfb.ServerVersion == 3.8f && securityType == 1) {
                            if (rfb.ReadSecurityResult() > 0) {
                                // For some reason, the server is not accepting the connection.  Get the
                                // reason and throw an exception
                                throw new VncProtocolException("Unable to Connecto to the Server. The Server rejected the connection for the following reason: " + rfb.ReadSecurityFailureReason());
                            }
                        }

                        return (securityType > 1) ? true : false;
                    }
                } else {
                    // Something is wrong, since we should have gotten at least 1 Security Type
                    throw new VncProtocolException("Protocol Error Connecting to Server. The Server didn't send any Security Types during the initial handshake.");
                }
            } catch (Exception e) {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }
示例#8
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 bool Connect(string host, int display, int port, bool viewOnly)
        {
            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;

            rfb = new RfbProtocol();

            viewOnlyMode = viewOnly;
            if (viewOnly)
            {
                inputPolicy = new VncViewInputPolicy(rfb);
            }
            else
            {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try
            {
                rfb.Connect(host, port);
                rfb.ReadProtocolVersion();

                // Handle possible repeater connection
                if (rfb.ServerVersion == 0.0)
                {
                    rfb.WriteProxyAddress();
                    // Now we are connected to the real server; read the protocol version of the
                    // server
                    rfb.ReadProtocolVersion();
                    // Resume normal handshake and protocol
                }

                rfb.WriteProtocolVersion();

                // Figure out which type of authentication the server uses
                var types = rfb.ReadSecurityTypes();

                // Based on what the server sends back in the way of supported Security Types, one of
                // two things will need to be done: either the server will reject the connection (i.e., type = 0),
                // or a list of supported types will be sent, of which we need to choose and use one.
                if (types.Length <= 0)
                {
                    // Something is wrong, since we should have gotten at least 1 Security Type
                    throw new VncProtocolException(
                              "Protocol Error Connecting to Server. The Server didn't send any Security Types during the initial handshake.");
                }
                if (types[0] == 0)
                {
                    // The server is not able (or willing) to accept the connection.
                    // A message follows indicating why the connection was dropped.
                    throw new VncProtocolException(
                              "Connection Failed. The server rejected the connection for the following reason: " +
                              rfb.ReadSecurityFailureReason());
                }

                securityType = GetSupportedSecurityType(types);
                Debug.Assert(securityType > 0, "Unknown Security Type(s)",
                             "The server sent one or more unknown Security Types.");

                rfb.WriteSecurityType(securityType);

                // Protocol 3.8 states that a SecurityResult is still sent when using NONE (see 6.2.1)
                if (rfb.ServerVersion != 3.8f || securityType != 1)
                {
                    return(securityType > 1);
                }
                if (rfb.ReadSecurityResult() > 0)
                {
                    // For some reason, the server is not accepting the connection.  Get the
                    // reason and throw an exception
                    throw new VncProtocolException(
                              "Unable to Connecto to the Server. The Server rejected the connection for the following reason: " +
                              rfb.ReadSecurityFailureReason());
                }

                return(securityType > 1);
            }
            catch (Exception e)
            {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }
示例#9
0
        /// <summary>
        /// Wait for a connection from VNC Server.
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="viewOnly"></param>
        public void Listen(string host, int port, bool viewOnly)
        {
            if (host == null) throw new ArgumentNullException("host");

			rfb = new RfbProtocol();

			if (viewOnly) {
				inputPolicy = new VncViewInputPolicy(rfb);
			} else {
				inputPolicy = new VncDefaultInputPolicy(rfb);
			}
			
			// Connect and determine version of server, and set client protocol version to match			
            try
            {
                rfb.RfbConnectedFromServer += new EventHandler(ConnectedFromServerEventHandler);
                rfb.FailedToListen += new EventHandler(FailedToListenHandler);
                rfb.Listen(host, port);
            } 
            catch (Exception)
            {
                if (ConnectionLost != null)
                {
                    ConnectionLost(this, EventArgs.Empty);
                }
            }
        }
示例#10
0
        /// <summary>
        /// Main Function for Connect Process.
        /// </summary>
        /// <param name="host"></param>
        /// <param name="display"></param>
        /// <param name="port"></param>
        /// <param name="viewOnly"></param>
        /// <param name="connectFromClient">Set true if connect from client to server. Set false if wait for a connection from server.</param>
        /// <returns></returns>
        private bool ConnectCore(string host, int display, ref int port, bool viewOnly, bool connectFromClient)
        {
            if (host == null) throw new ArgumentNullException("host");

            // If a diplay number is specified (used to connectFromClient 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("display", display, "Display number must be non-negative.");
            port += display;

            rfb = new RfbProtocol();

            if (viewOnly)
            {
                inputPolicy = new VncViewInputPolicy(rfb);
            }
            else
            {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match			
            try
            {
                if (connectFromClient)
                {
                    rfb.Connect(host, port);
                    return DoHandShake();
                }
                else
                {
                    rfb.Listen(host, port);
                    return true;
                }                
            }
            catch (Exception e)
            {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }