public void HandleRequest(ChatroomServerProtocol protocol, PropertyDictionary request)
        {
            if (!request.HasValueFor(ChatroomKeys.MessageType) || request.StringFor(ChatroomKeys.MessageType) != Key)
            {
                protocol.SendMalformedRequestResponse();
                return;
            }

            object connected = null;

            lock (_lockObject)
            {
                connected = protocol.Parent.RequestLogin(protocol, request);
                request.SetValueFor(ChatroomKeys.Message, connected);
            }

            if (connected.ToString() == ChatroomKeys.LoginSuccess)
            {
                protocol.Parent.BroadcastServerMessage("{0} has joined.", protocol.Username);
                protocol.SendWelcomeMessage();
            }

            protocol.SendTerm(Atom.From(request.PersistToString()));
        }
        public void HandleRequest(ChatroomServerProtocol protocol, PropertyDictionary request)
        {
            if (!request.HasValueFor(ChatroomKeys.MessageType) || !(request.StringFor(ChatroomKeys.MessageType) == Key))
            {
                protocol.SendMalformedRequestResponse();
                return;
            }

            lock (_lockObject)
            {
                string sender = protocol.Parent.ResolveSenderName(request);

                if (sender != null)
                {
                    request[ChatroomKeys.SenderName] = sender;

                    protocol.Parent.Broadcast(request);
                }
                else
                {
                    protocol.SendMalformedRequestResponse();
                }
            }
        }
        public Protocol BuildProtocol()
        {
            ChatroomServerProtocol newInstance = new ChatroomServerProtocol(this, _plugins);

            return newInstance;
        }
        internal object RequestLogin(ChatroomServerProtocol protocol, PropertyDictionary request)
        {
            if (!CanAddClient) return ChatroomKeys.LoginFail_TooManyClients;
            if (!UsernameIsAvailable(request.StringFor(ChatroomKeys.SenderName))) return ChatroomKeys.LoginFail_UserNameInUse;

            protocol.Username = request.StringFor(ChatroomKeys.SenderName);
            protocol.Key = request.StringFor(ChatroomKeys.SenderId);

            _clients.AddClient(protocol);

            BroadcastLoggedInUsers();

            return ChatroomKeys.LoginSuccess;
        }
        internal void ReportDisconnection(ChatroomServerProtocol disconnectingProtocol)
        {
            _clients.RemoveClient(disconnectingProtocol);

            BroadcastServerMessage("{0} has left.", disconnectingProtocol.Username);
            BroadcastLoggedInUsers();
        }
        internal void NotifyOnLogout(ChatroomServerProtocol protocol)
        {
            Console.WriteLine("{0} logged out gracefully.", protocol.Username);

            ReportDisconnection(protocol);
        }