Exemplo n.º 1
0
        public void JoinResult()
        {
            LoginResultState state = LoginResultState.Success;

            var msg = new JoinResultMessage(state, new UserInfo(Nickname, Nickname, UserId, ChannelId, true));

            Assert.AreEqual(state, msg.Result);
            Assert.AreEqual(UserId, msg.UserInfo.UserId);
            Assert.AreEqual(Nickname, msg.UserInfo.Nickname);
            Assert.AreEqual(ChannelId, msg.UserInfo.CurrentChannelId);
            Assert.AreEqual(Muted, msg.UserInfo.IsMuted);
            msg.WritePayload(serverContext, writer);
            long length = stream.Position;

            stream.Position = 0;

            msg = new JoinResultMessage();
            msg.ReadPayload(clientContext, reader);

            Assert.AreEqual(length, stream.Position);
            Assert.AreEqual(state, msg.Result);
            Assert.AreEqual(UserId, msg.UserInfo.UserId);
            Assert.AreEqual(Nickname, msg.UserInfo.Nickname);
            Assert.AreEqual(ChannelId, msg.UserInfo.CurrentChannelId);
            Assert.AreEqual(Muted, msg.UserInfo.IsMuted);
        }
Exemplo n.º 2
0
        internal void OnJoinMessage(MessageEventArgs <JoinMessage> e)
        {
            var join = e.Message;

            if (!e.Connection.IsConnected)
            {
                return;
            }

            if (join.Nickname.IsNullOrWhitespace())
            {
                e.Connection.SendAsync(new JoinResultMessage(LoginResultState.FailedInvalidNickname, null));
                return;
            }

            if (!String.IsNullOrEmpty(this.context.Settings.ServerPassword) && join.ServerPassword != this.context.Settings.ServerPassword)
            {
                e.Connection.SendAsync(new JoinResultMessage(LoginResultState.FailedServerPassword, null));
                return;
            }

            IUserInfo info = GetJoiningUserInfo(e.Connection, join);

            if (info == null)
            {
                return;
            }

            LoginResultState result = LoginResultState.Success;

            if (Manager.GetIsNicknameInUse(join.Nickname))
            {
                if (!AttemptNicknameRecovery(info, join.Nickname))
                {
                    result = LoginResultState.FailedNicknameInUse;
                }
            }

            var msg = new JoinResultMessage(result, info);

            if (result == LoginResultState.Success)
            {
                Manager.Join(e.Connection, info);
                e.Connection.SendAsync(msg);

                if (!Manager.GetIsLoggedIn(e.Connection))
                {
                    e.Connection.SendAsync(new PermissionsMessage(info.UserId, this.context.PermissionsProvider.GetPermissions(info.UserId)));
                }

                foreach (IConnection connection in this.context.Connections)
                {
                    connection.SendAsync(new UserJoinedMessage(info));
                }
            }
            else
            {
                e.Connection.SendAsync(msg);
            }
        }