Example #1
0
 /// <summary>
 /// Handler for the Stop command.
 /// </summary>
 protected void OnStop()
 {
     if (this.registrationConnection != null)
     {
         this.registrationConnection.Close();
         this.registrationConnection = null;
     }
 }
Example #2
0
        /// <summary>
        /// Handler for forwarded connections.
        /// </summary>
        /// <param name="connection">
        /// The connection being forwarded.
        /// </param>
        /// <returns>
        /// True if forwarding was established, false otherwise.
        /// </returns>
        private bool Forwarding(ServiceConnection connection)
        {
            Socket localService = StaticUtilities.CreateConnectedSocket(
                "localhost",
                HomeOS.Hub.Common.Constants.InfoServicePort);

            NetworkStream netstream = new NetworkStream(localService, true /*ownSocket*/);

            if (localService != null)
            {
                Forwarder forwarder = new Forwarder(
                    netstream,
                    connection.GetStream(),
                    this.StopForwarding, null);
                return(true);
            }

            return(false);
        }
Example #3
0
        private void Register()
        {
            // -
            // Register ourselves with the cloud service.
            // Then wait for it to send us client forwarding requests.
            // -
            Socket socket = StaticUtilities.CreateConnectedSocket(
                Settings.ServiceHost,
                Settings.ServicePort);

            if (socket == null)
            {
                this.ExitCode = 1066;  // 1066 = "The service has returned a service-specific error code."  Or 10054? 10064? 10065?
                OnStop();
                return;
            }

            this.registrationConnection = new ServiceConnection(
                Settings.ServiceHost,
                socket,
                0,
                this.Forwarding, logger);
        }
Example #4
0
        /// <summary>
        /// Handler for all message types.
        /// </summary>
        /// <remarks>
        /// Note we must not operate on the passed-in 'data' parameter after
        /// this routine returns, as it just references the receive buffer.
        /// </remarks>
        /// <param name="type">The message type.</param>
        /// <param name="data">The message data.</param>
        /// <returns>
        /// True if we entered send mode as a result of receiving this message,
        /// false if we're staying in receive mode and should re-start receive.
        /// </returns>
        private bool HandleMessage(MessageType type, ArraySegment <byte> data)
        {
#if DEBUG
            logger.Log(
                "HomeService Received '{0}' message with {1} bytes of data.",
                type.ToString(),
                data.Count.ToString());
#endif

            switch (type)
            {
            case MessageType.PadN:
                break;

            case MessageType.EchoRequest:
                this.bufferOffset = 0;
                this.AppendMessage(MessageType.EchoReply, data);
                this.SendMessage();
                return(true);

            case MessageType.EchoReply:
                break;

            case MessageType.Version:
                if (data.Count != sizeof(byte))
                {
                    this.ShutdownAndClose();
                    return(true);
                }

                this.peerProtocolVersion = data.Array[data.Offset];
#if DEBUG
                logger.Log(
                    " HomeService Server is using protocol version #{0}",
                    this.peerProtocolVersion.ToString());
#endif
                break;

            case MessageType.PleaseIdentify:
#if DEBUG
                logger.Log("  HomeService Sending Identification message");
#endif
                this.bufferOffset = 0;
                this.AppendMessage(
                    MessageType.Identification,
                    System.Text.Encoding.ASCII.GetBytes(this.identifier));
                this.SendMessage();
                return(true);

            case MessageType.PleaseAuthenticate:
                if (data.Count != sizeof(byte))
                {
                    this.ShutdownAndClose();
                    return(true);
                }

                AuthenticationType authType =
                    (AuthenticationType)data.Array[data.Offset];
#if DEBUG
                logger.Log(
                    "  HomeService Authentication request is for type {0}",
                    authType.ToString());
                logger.Log("  HomeService Sending SimpleAuthentication message");
#endif
                this.bufferOffset = 0;
                this.AppendMessage(
                    MessageType.SimpleAuthentication,
                    BitConverter.GetBytes(this.simpleAuthentication));
                this.SendMessage();
                return(true);

            case MessageType.Authenticated:
                this.bufferOffset = 0;
                if (this.clientToken == 0)
                {
#if DEBUG
                    logger.Log("  Sending RegisterService message");
#endif
                    this.AppendMessage(MessageType.RegisterService);
                }
                else
                {
#if DEBUG
                    logger.Log("  Sending ForwardToClient message");
#endif
                    this.AppendMessage(
                        MessageType.ForwardToClient,
                        BitConverter.GetBytes(this.clientToken));
                    this.forwarding = true;
                }

                this.SendMessage();
                return(true);

            case MessageType.ClientAwaits:
                if (data.Count != sizeof(uint))
                {
                    this.ShutdownAndClose();
                    return(true);
                }

                uint clientToken = BitConverter.ToUInt32(
                    data.Array,
                    data.Offset);
#if DEBUG
                logger.Log("  Client with instance token {0} awaits matchup.", clientToken.ToString());
                logger.Log("  Launching client connection to connect back in and match up with gatekeeper client instance.");
#endif

                // -
                // Note about garbage collection (GC).
                // The ServiceConnection constructor will have initiated
                // one or more async receive calls by the time it returns.
                // ReceiveAsync holds a reference to the
                // SocketAsyncEventArgs object in use, and that object
                // holds a reference to the ServiceConnection.  Everything
                // is driven off receive handlers, so some reference to the
                // new ServiceConnection should remain around until the
                // forwarding handler is invoked (or connection closed).
                // -
                Socket socket = StaticUtilities.CreateConnectedSocket(
                    this.socket.RemoteEndPoint);
                if (null != socket)
                {
                    ServiceConnection client = new ServiceConnection(
                        this.sslServerHost,
                        socket,
                        clientToken,
                        this.handler, this.logger);
                }
                break;

            default:
#if DEBUG
                logger.Log("  ** Unexpected message!  Closing! **");
#endif
                this.ShutdownAndClose();
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Handler for forwarded connections.
        /// </summary>
        /// <param name="connection">
        /// The connection being forwarded.
        /// </param>
        /// <returns>
        /// True if forwarding was established, false otherwise.
        /// </returns>
        private bool Forwarding(ServiceConnection connection)
        {
            Socket localService = StaticUtilities.CreateConnectedSocket(
                "localhost",
                HomeOS.Hub.Common.Constants.InfoServicePort);

            NetworkStream netstream = new NetworkStream(localService, true /*ownSocket*/);

            if (localService != null)
            {
                Forwarder forwarder = new Forwarder(
                    netstream,
                    connection.GetStream(),
                    this.StopForwarding, null);
                return true;
            }

            return false;
        }
 /// <summary>
 /// Handler for the Stop command.
 /// </summary>
 protected void OnStop()
 {
     if (this.registrationConnection != null)
     {
         this.registrationConnection.Close();
         this.registrationConnection = null;
     }
 }
        private void Register() 
        {
            // -
            // Register ourselves with the cloud service.
            // Then wait for it to send us client forwarding requests.
            // -
            Socket socket = StaticUtilities.CreateConnectedSocket(
                Settings.ServiceHost,
                Settings.ServicePort);
            if (socket == null)
            {
                this.ExitCode = 1066;  // 1066 = "The service has returned a service-specific error code."  Or 10054? 10064? 10065?
                OnStop();
                return;
            }

            this.registrationConnection = new ServiceConnection(
                Settings.ServiceHost,
                socket,
                0,
                this.Forwarding, logger);

        }
Example #8
0
        /// <summary>
        /// Handler for all message types.
        /// </summary>
        /// <remarks>
        /// Note we must not operate on the passed-in 'data' parameter after
        /// this routine returns, as it just references the receive buffer.
        /// </remarks>
        /// <param name="type">The message type.</param>
        /// <param name="data">The message data.</param>
        /// <returns>
        /// True if we entered send mode as a result of receiving this message,
        /// false if we're staying in receive mode and should re-start receive.
        /// </returns>
        private bool HandleMessage(MessageType type, ArraySegment<byte> data)
        {
            #if DEBUG
            logger.Log(
                "HomeService Received '{0}' message with {1} bytes of data.",
                type.ToString(),
                data.Count.ToString());
            #endif

            switch (type)
            {
                case MessageType.PadN:
                    break;

                case MessageType.EchoRequest:
                    this.bufferOffset = 0;
                    this.AppendMessage(MessageType.EchoReply, data);
                    this.SendMessage();
                    return true;

                case MessageType.EchoReply:
                    break;

                case MessageType.Version:
                    if (data.Count != sizeof(byte))
                    {
                        this.ShutdownAndClose();
                        return true;
                    }

                    this.peerProtocolVersion = data.Array[data.Offset];
            #if DEBUG
                    logger.Log(
                        " HomeService Server is using protocol version #{0}",
                        this.peerProtocolVersion.ToString());
            #endif
                    break;

                case MessageType.PleaseIdentify:
            #if DEBUG
                    logger.Log("  HomeService Sending Identification message");
            #endif
                    this.bufferOffset = 0;
                    this.AppendMessage(
                        MessageType.Identification,
                        System.Text.Encoding.ASCII.GetBytes(this.identifier));
                    this.SendMessage();
                    return true;

                case MessageType.PleaseAuthenticate:
                    if (data.Count != sizeof(byte))
                    {
                        this.ShutdownAndClose();
                        return true;
                    }

                    AuthenticationType authType =
                        (AuthenticationType)data.Array[data.Offset];
            #if DEBUG
                    logger.Log(
                        "  HomeService Authentication request is for type {0}",
                        authType.ToString());
                    logger.Log("  HomeService Sending SimpleAuthentication message");
            #endif
                    this.bufferOffset = 0;
                    this.AppendMessage(
                        MessageType.SimpleAuthentication,
                        BitConverter.GetBytes(this.simpleAuthentication));
                    this.SendMessage();
                    return true;

                case MessageType.Authenticated:
                    this.bufferOffset = 0;
                    if (this.clientToken == 0)
                    {
            #if DEBUG
                        logger.Log("  Sending RegisterService message");
            #endif
                        this.AppendMessage(MessageType.RegisterService);
                    }
                    else
                    {
            #if DEBUG
                        logger.Log("  Sending ForwardToClient message");
            #endif
                        this.AppendMessage(
                            MessageType.ForwardToClient,
                            BitConverter.GetBytes(this.clientToken));
                        this.forwarding = true;
                    }

                    this.SendMessage();
                    return true;

                case MessageType.ClientAwaits:
                    if (data.Count != sizeof(uint))
                    {
                        this.ShutdownAndClose();
                        return true;
                    }

                    uint clientToken = BitConverter.ToUInt32(
                        data.Array,
                        data.Offset);
            #if DEBUG
                    logger.Log("  Client with instance token {0} awaits matchup.", clientToken.ToString());
                    logger.Log("  Launching client connection to connect back in and match up with gatekeeper client instance.");
            #endif

                    // -
                    // Note about garbage collection (GC).
                    // The ServiceConnection constructor will have initiated
                    // one or more async receive calls by the time it returns.
                    // ReceiveAsync holds a reference to the
                    // SocketAsyncEventArgs object in use, and that object
                    // holds a reference to the ServiceConnection.  Everything
                    // is driven off receive handlers, so some reference to the
                    // new ServiceConnection should remain around until the
                    // forwarding handler is invoked (or connection closed).
                    // -
                    Socket socket = StaticUtilities.CreateConnectedSocket(
                        this.socket.RemoteEndPoint);
                    if (null != socket)
                    {
                        ServiceConnection client = new ServiceConnection(
                            this.sslServerHost,
                            socket,
                            clientToken,
                            this.handler, this.logger);
                    }
                    break;

                default:
            #if DEBUG
                    logger.Log("  ** Unexpected message!  Closing! **");
            #endif
                    this.ShutdownAndClose();
                    return true;
            }

            return false;
        }