Пример #1
0
        public void Run(params string[] args)
        {
            Console.CancelKeyPress += OnCancelKeyPressed;

            try
            {
                using (var receiver = new UdpClient(8888))
                {
                    using (var sender = new UdpSender(7777))
                    {
                        Console.WriteLine("Welcome to terminal.");
                        Console.WriteLine("Press CTRL+C or enter empty line to stop terminal.");

                        while (_terminalRunning)
                        {
                            while (Console.KeyAvailable)
                            {
                                var line = Console.ReadLine();
                                if (!string.IsNullOrWhiteSpace(line))
                                    sender.Send(line);
                                else
                                    _terminalRunning = false;
                            }

                            while (receiver.Available > 0)
                            {
                                var message = receiver.ReceiveString();
                                if (!string.IsNullOrWhiteSpace(message))
                                    Console.WriteLine($"<- {message}");
                            }

                            Thread.Sleep(1);
                        }

                        Console.WriteLine("Terminal stopped.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToRecursiveString());
            }

            Console.CancelKeyPress -= OnCancelKeyPressed;
        }
Пример #2
0
        private static void Main()
        {
            Console.CancelKeyPress += OnCancelKeyPressed;

            try
            {
            #if DEBUG
                File.Delete(DefaultDatabaseFilename);
            #endif

                using (var links = new Links(DefaultDatabaseFilename, 8*1024*1024))
                {
                    InitUTF16(links);

                    var sequences = new Sequences(links);

                    PrintContents(links, sequences);

                    Console.WriteLine("Links server started.");
                    Console.WriteLine("Press CTRL+C or ESC to stop server.");

                    using (var sender = new UdpSender(8888))
                    {
                        MessageHandlerCallback handleMessage = message =>
                        {
                            if (!string.IsNullOrWhiteSpace(message))
                            {
                                Console.WriteLine("R.M.: {0}", message);

                                if (message.EndsWith("?"))
                                    sequences.Search(sender, message);
                                else
                                    sequences.Create(sender, message);
                            }
                        };

                        //using (var receiver = new UdpReceiver(7777, handleMessage))
                        using (var receiver = new UdpClient(7777))
                        {
                            while (LinksServerRunning)
                            {
                                while (receiver.Available > 0)
                                    handleMessage(receiver.ReceiveString());

                                while (Console.KeyAvailable)
                                {
                                    var info = Console.ReadKey(true);
                                    if (info.Key == ConsoleKey.Escape)
                                        LinksServerRunning = false;
                                }

                                Thread.Sleep(1);
                            }

                            Console.WriteLine("Links server stopped.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.WriteToConsole();
            }

            Console.CancelKeyPress -= OnCancelKeyPressed;
        }