Ejemplo n.º 1
0
        private void JoinRoomHandler(ClientJoinRoomRequest command, int streamId, CommandAnswerContext answerContext)
        {
            var         result = new ServerResponseBase();
            UserContext ctx    = null;
            var         room   = command.RoomName;

            lock (_users)
            {
                ctx = ValidateUser(streamId, answerContext, result);
                if (answerContext.Answered)
                {
                    return;
                }
                if (ctx.Rooms.Contains(room))
                {
                    result.Status      = ServerResponseStatus.Error;
                    result.Description = $"Already in room {room}";
                    answerContext.TryAnswer(result);
                    return;
                }
                ctx.Rooms.Add(room);
            }
            result.Status = ServerResponseStatus.Success;
            answerContext.TryAnswer(result);
            _commandHandler.Notify(new ServerServiceMessage()
            {
                Message = $"{ctx.Name}, welcome to '{room}'!"
            }, streamId);
        }
Ejemplo n.º 2
0
        private void RoomMessageHandler(ClientSendMessageToRoom command, int streamId, CommandAnswerContext answerContext)
        {
            var result  = new ServerResponseBase();
            var message = command.Message;
            var room    = command.Room;

            lock (_users)
            {
                var ctx = ValidateUser(streamId, answerContext, result);
                if (answerContext.Answered)
                {
                    return;
                }
                if (!ctx.Rooms.Contains(room))
                {
                    result.Status      = ServerResponseStatus.Error;
                    result.Description = $"You not in the room '{room}'!";
                    answerContext.TryAnswer(result);
                    return;
                }
                var dests      = _users.Where(u => u.Rooms.Contains(room)).Select(u => u.Stream).ToArray();
                var messageCmd = new ServerRoomMessage
                {
                    FromUser = ctx.Name,
                    Message  = message,
                    Room     = room
                };
                _commandHandler.Notify(messageCmd, dests);
            }
        }
Ejemplo n.º 3
0
        private void LoginHandler(ClientLoginRequest command, int streamId, CommandAnswerContext answerContext)
        {
            var result = new ServerLoginResponse();

            if (string.IsNullOrEmpty(command.UserName))
            {
                result.Status      = ServerResponseStatus.Error;
                result.Description = "Invalid name";
                answerContext.TryAnswer(result);
                return;
            }
            var name = command.UserName;

            lock (_users)
            {
                if (_users.Any(u => u.Name == name))
                {
                    result.Status      = ServerResponseStatus.Error;
                    result.Description = $"User with name '{name}' already logged in";
                    answerContext.TryAnswer(result);
                    return;
                }
                var ctx = new UserContext
                {
                    Name   = command.UserName,
                    Rooms  = new List <string>(),
                    Stream = streamId
                };
                _users.Add(ctx);
            }
            result.Status = ServerResponseStatus.Success;
            answerContext.TryAnswer(result);
            _commandHandler.Notify(new ServerServiceMessage()
            {
                Message = $"Hello, {name}!"
            }, streamId);
        }
Ejemplo n.º 4
0
 private UserContext ValidateUser(int streamId, CommandAnswerContext answerContext, ServerResponseBase responseBase)
 {
     lock (_users)
     {
         var ctx = _users.FirstOrDefault(u => u.Stream == streamId);
         if (ctx == null)
         {
             responseBase.Status      = ServerResponseStatus.Error;
             responseBase.Description = "User not logged in";
             answerContext.TryAnswer(responseBase);
             return(null);
         }
         return(ctx);
     }
 }
Ejemplo n.º 5
0
        private void ListRoomUsersRequest(ClientListRoomUsersRequest command, int streamId, CommandAnswerContext answerContext)
        {
            var result = new ServerListRoomUsersResponse();

            lock (_users)
            {
                var ctx = ValidateUser(streamId, answerContext, result);
                if (answerContext.Answered)
                {
                    return;
                }
                var usersInRoom = _users.Where(_ => _.Rooms.Contains(command.RoomName)).Select(_ => _.Name).ToArray();
                result.UsersNames = usersInRoom;
                answerContext.TryAnswer(result);
            }
        }
Ejemplo n.º 6
0
        private void ListRoomsHandler(ClientListRoomsRequest command, int streamId, CommandAnswerContext answerContext)
        {
            var result = new ServerListRoomsResponse();

            lock (_users)
            {
                var ctx = ValidateUser(streamId, answerContext, result);
                if (answerContext.Answered)
                {
                    return;
                }
                var allRooms = _users.SelectMany(_ => _.Rooms).Distinct().ToArray();
                result.RoomNames = allRooms;
                answerContext.TryAnswer(result);
            }
        }