Пример #1
0
        /// <summary>
        /// Initializes a new instance of the ServiceConnection class.
        /// </summary>
        /// <param name="connected">
        /// The socket for the new connection.
        /// </param>
        /// <param name="token">
        /// The token for this client of the gatekeeper service,
        /// or zero if this is the registration connection.
        /// </param>
        /// <param name="handler">
        /// The handler routine to call upon forwarding a connection.
        /// </param>
        public ServiceConnection(
            string serverHost,
            Socket connected,
            uint token,
            ForwardingHandler handler,
            VLogger logger)
        {
            this.useSecureStream = true;

            // -
            // Set up the connection state.
            // -
            this.socket        = connected;
            this.netstream     = new NetworkStream(this.socket, true /*ownSocket*/);
            this.sslServerHost = serverHost;
            if (this.useSecureStream)
            {
                this.sslStream = new SslStream(
                    this.netstream,
                    false /* leaveInnerStreamOpen */,
                    new RemoteCertificateValidationCallback(ValidateServerCertificate),
                    null
                    );
                // The server name must match the name on the server certificate.
                try
                {
                    sslStream.AuthenticateAsClient(this.sslServerHost);
                }
                catch (Exception e)
                {
                    logger.Log("Exception: {0}", e.Message);
                    if (e.InnerException != null)
                    {
                        logger.Log("Inner exception: {0}", e.InnerException.Message);
                    }
                    logger.Log("Authentication failed - closing the connection.");
                    this.ShutdownAndClose();
                    return;
                }
            }
            this.clientToken          = token;
            this.handler              = handler;
            this.forwarding           = false;
            this.identifier           = HomeOS.Shared.Gatekeeper.Settings.HomeId;
            this.simpleAuthentication = HomeOS.Shared.Gatekeeper.Settings.HomePassword;

            this.logger = logger;
//#if false
            //-
            //We use keep-alives on the home <-> cloud service
            //connection in an attempt to prevent NAT/firewall
            //state from timing out and dropping our connection.
            //-
            StaticUtilities.SetKeepAlive(this.socket, 120000, 1000);
//#endif

            // -
            // Prepare our buffer space and asynchronous state holder.
            // This is currently just a simplistic single buffer system.
            // Note that this code assumes that the buffer is larger than
            // the largest possible single message (currently 257 bytes).
            // -
            this.buffer       = new byte[1500];
            this.bufferOffset = 0;

            this.streamBufState = new StreamBufferState();
            this.streamBufState.SetBuffer(this.buffer, 0, this.buffer.Length);

            // -
            // Start the dialog with our peer.
            // -
            this.AppendMessage(
                MessageType.Version,
                ServiceConnection.ProtocolVersion);
            this.SendMessage();
        }