Example #1
0
        private void Handle()
        {
            if (EndPoint == null)
            {
                _client.Close();
                return;
            }

            if (ServerConfiguration.BeepOnConnection)
            {
                Console.Beep(1000, 80);
                Console.Beep(1500, 80);
                Console.Beep(2000, 200);
            }

            IPAddress     ipaddress    = EndPoint.Address;
            NetworkStream clientStream = _client.GetStream();

            NLConsole.WriteLine(String.Format(UIStrings.ClientConnected, _controller.GetName(), ipaddress), _controller.ClientConnectedColor);

            int bytesRead = 0;

            Byte[]  messageBytes = new Byte[64];
            Boolean complete     = false;

            while (!ended)
            {
                try { bytesRead = clientStream.Read(messageBytes, 0, messageBytes.Length); }
                catch {
                    if (!ended)
                    {
                        NLConsole.WriteLine(String.Format(UIStrings.ClientTerminated, _controller.GetName(), ipaddress), _controller.ClientTerminatedColor);
                    }
                    break;
                }
                if (bytesRead == 0)
                {
                    complete = true;
                    break;
                }

                String         message = Encoding.ASCII.GetString(messageBytes, 0, bytesRead);
                CommandPattern command = CommandPattern.Create(message);
                _controller.InvokeAction(command, _client);
            }

            _client.Close();
            OnDeath.Invoke(this);

            if (complete)
            {
                NLConsole.WriteLine(String.Format(UIStrings.ClientDisconnected, _controller.GetName(), ipaddress), _controller.ClientDisconnectedColor);
            }

            if (ServerConfiguration.BeepOnDisconnection)
            {
                Console.Beep(2000, 80);
                Console.Beep(1500, 80);
                Console.Beep(1000, 200);
            }
        }