Exemplo n.º 1
0
        /// <summary>
        /// Simulates the server side of the authentication handshake.
        /// </summary>
        /// <param name="serverConnection">The server side connection.</param>
        /// <param name="failAuth">Pass <c>true</c> to simulate an authentication failure.</param>
        private void AuthHandshake(SwitchConnection serverConnection, bool failAuth)
        {
            ArgCollection properties = new ArgCollection(ArgCollectionType.Unconstrained);
            SwitchPacket  packet;

            // Receive and respond to the [auth] command.

            packet = serverConnection.ReceivePacket();
            if (packet.CommandText != string.Format("auth {0}", SwitchConnection.DefaultPassword))
            {
                serverConnection.Close();
                return;
            }

            properties["Content-Type"] = "auth/request";
            serverConnection.SendPacket(new SwitchPacket(properties, null));

            // Receive and respond to the [api status] command.

            packet = serverConnection.ReceivePacket();
            if (packet.CommandText != string.Format("api status", SwitchConnection.DefaultPassword))
            {
                serverConnection.Close();
                return;
            }

            serverConnection.SendReply(failAuth ? "-ERR access denied" : "+OK success");

            properties["Content-Type"] = "api/response";
            serverConnection.SendPacket(new SwitchPacket(properties, Helper.ASCIIEncoding.GetBytes("Hello World!")));

            if (failAuth)
            {
                Thread.Sleep(1000);     // Get the client a chance to see the reply.
            }
        }