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); }
private void OnServiceMessage(ServerServiceMessage command, int streamId, CommandAnswerContext answerContext) { if (command.RoomNames == null || command.RoomNames.Length < 1) { Console.WriteLine($"Service message: {command.Message}"); } else { for (var i = 0; i < command.RoomNames.Length; ++i) { var r = command.RoomNames[i]; Console.WriteLine($"{r}: Service message: {command.Message}"); } } }
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); } }
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); } }
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); }
private void OnRoomMessage(ServerRoomMessage command, int streamId, CommandAnswerContext answerContext) { Console.WriteLine($"{command.Room}:{command.FromUser}: {command.Message}"); }
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); } }
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); } }