protected override Result PerformServiceLoop()
        {
            Accepted.Reset();
            ConnectionSocket.BeginAccept(AcceptConnection, ConnectionSocket);

            Accepted.WaitOne();
            return(Result.Success);
        }
Exemplo n.º 2
0
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket socket = ConnectionSocket.EndAccept(ar);

            _clientSockets.Add(socket);

            Console.CursorTop += 1;
            Console.CursorLeft = 0;
            Console.WriteLine("Client Connected: " + socket.RemoteEndPoint);

            socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
            ConnectionSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
Exemplo n.º 3
0
        private void SetupServer()
        {
            Console.WriteLine("\nSetting up server...\n");

            Console.Write("Setting up Hardware Monitor...");
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            ramCounter = new PerformanceCounter("Memory", "Available MBytes");
            Console.WriteLine("Done!");

            Console.Write("Setting up Network Communication...");
            ConnectionSocket.Bind(new IPEndPoint(ServerAddress, Port));
            ConnectionSocket.Listen(1);
            ConnectionSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
            Console.WriteLine("Done!");

            Console.WriteLine("\nReady!\n");

            string    consoleInput = "> ";
            Stopwatch timer        = new Stopwatch();

            timer.Start();
            do
            {
                while (!Console.KeyAvailable)
                {
                    if (timer.Elapsed.Seconds > 0)
                    {
                        Time         = Utility.Time();
                        CpuUsage     = (int)Math.Round(cpuCounter.NextValue(), 0);
                        RamAvailable = (int)ramCounter.NextValue();
                        timer.Restart();
                    }

                    Console.SetCursorPosition(0, Console.CursorTop + 2);
                    Console.Write(Utility.EmptyString(Console.BufferWidth - 1));

                    Console.SetCursorPosition(0, Console.CursorTop);
                    Console.Write("Server time: " + Time);
                    string writeLine = "[" + CpuUsage + "% / " + RamAvailable + " RAM]";
                    Console.CursorLeft = Console.BufferWidth - (writeLine.Length + 1);

                    Console.Write(writeLine);
                    Utility.Cleanup();
                    Console.SetCursorPosition(0, Console.CursorTop - 2);

                    Console.CursorLeft = 0;

                    if (consoleInput.Length < Console.BufferWidth)
                    {
                        Console.Write(consoleInput + Utility.EmptyString((Console.BufferWidth - 1) - consoleInput.Length));
                        Console.CursorLeft = consoleInput.Length;
                    }
                    System.Threading.Thread.Sleep(50);
                }
                string key = Console.ReadKey(true).Key.ToString();
                switch (key)
                {
                case "Backspace":
                    if (consoleInput.Length > 2)
                    {
                        consoleInput = consoleInput.Remove(consoleInput.Length - 1);
                    }
                    break;

                case "Spacebar":
                    consoleInput += " ";
                    break;

                case "Enter":
                    Console.CursorTop += 1;
                    Console.CursorLeft = 0;
                    Console.WriteLine("Command: '" + consoleInput.Remove(0, 2) + "' not found");
                    consoleInput = "> ";
                    break;

                default:
                    consoleInput += key.ToLower();
                    break;
                }
            } while (true);
        }