Exemplo n.º 1
0
        public void SessionSelectLoop(ConsoleClient client)
        {
            var are = new AutoResetEvent(false);

            void check(ConsoleClient c)
            {
                if (c.Session == null)
                {
                    are.Set();
                }
            }

            try {
                client.SessionChanged += check;
                bool isFirst = true;
                while (!client.Closed)
                {
                    _sessionSelect(client, isFirst);
                    isFirst = false;
                    are.WaitOne();
                }
            } finally {
                client.SessionChanged -= check;
                are.Dispose();
            }
        }
Exemplo n.º 2
0
        public ConsoleSession CreateCmdSession(ConsoleClient clientToAttach)
        {
            var session = new ConsoleSession();

            clientToAttach.Attach(session);
            StartCmdLoopForSession(session);
            return(session);
        }
Exemplo n.º 3
0
        public void AddClient(ConsoleClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            lock (_bindedClients)
                _bindedClients.Add(client);
            client.Attach(this);
            foreach (var item in history)
            {
                client.Write(item);
            }
        }
Exemplo n.º 4
0
        private void _sessionSelect(ConsoleClient client, bool autoCreate)
        {
            if (Sessions.Count == 0 && autoCreate)
            {
                CreateCmdSession(client);
                return;
            }
            var tempsession = new ConsoleSession();

            void checkDetach(ConsoleClient cli)
            {
                if (cli.Session != tempsession)
                {
                    cli.SessionChanged -= checkDetach;
                    tempsession.InputLine(null);
                }
            }

            client.SessionChanged += checkDetach;
            client.Attach(tempsession);
            var c = tempsession.Console;

            while (true)
            {
                c.WriteLine("Select Session:");
                var sessions = Sessions.ToArray();
                for (int i = 0; i < sessions.Length; i++)
                {
                    c.WriteLine($"{i} {sessions[i]}");
                }
                c.WriteLine("n New Session");
                var line = c.ReadLine("> ");
                if (line == null)
                {
                    return;
                }
                if (int.TryParse(line, out int n) && n >= 0 && n < sessions.Length)
                {
                    client.Attach(sessions[n]);
                    break;
                }
                else if (line == "n")
                {
                    CreateCmdSession(client);
                    break;
                }
            }
        }
Exemplo n.º 5
0
        public void RemoveClient(ConsoleClient client, bool throwIfFailed = false)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            lock (_bindedClients) {
                bool ok = _bindedClients.Remove(client);
                if (ok == false && throwIfFailed)
                {
                    throw new Exception("failed to remove client.");
                }
                if (ok)
                {
                    client.Attach(null);
                }
            }
        }