private void action_change_room() { var oldroom = Room; _chatserver.RemoveRoomUser(_chatserver.RoomUsers[oldroom - 1], Nickname); Room = 0; while (Room == 0) { _chatserver.Write(_client.GetStream(), ChatProtocolValues.CHOOSE_ROOM(Nickname, _chatserver.NumRoom)); var temp = _chatserver.Read(_client.GetStream()); try { var rN = int.Parse(temp); if (rN >= 1 && rN <= _chatserver.NumRoom) { Room = rN; } } catch { // ignored } } _chatserver.AddRoomUser(_chatserver.RoomUsers[Room - 1], Nickname); _chatserver.Broadcast(ChatProtocolValues.MOVE_TO(Nickname, Room), oldroom); _chatserver.Broadcast(ChatProtocolValues.Welcome(Nickname, Room), Room); }
public bool Authenticate() { var validateCommand = false; while (!validateCommand) { _chatserver.Write(_client.GetStream(), AuthenticationProtocolValues.CommandPrompt); var data = _chatserver.Read(_client.GetStream()); data = data.Trim(); _action = action_default; if (data.ToUpper().IndexOf(AuthenticationProtocolValues.LoginCommand, StringComparison.Ordinal) == 0) { _action = action_login; validateCommand = true; } if (data.ToUpper().IndexOf(AuthenticationProtocolValues.RegisterCommand, StringComparison.Ordinal) == 0) { _action = action_register; validateCommand = true; } if (!_action()) { return(false); } } _chatserver.Write(_client.GetStream(), AuthenticationProtocolValues.AutenticatedMsg); return(true); }
//Main authentication method public bool Authenticate() { byte[] bytes = new byte[256]; string data = null; bool validate_command = false; //Get user command //Valid command is login and register while (!validate_command) { chatserver.Write(client.GetStream(), AuthenticationProtocolValues.COMMAND_PROMPT); data = chatserver.Read(client.GetStream()); data = data.Trim(); //default action is to assume that the command is invalid action = new AuthenticateAction(action_default); //actions are reassigned based on data entered by user if (data.ToUpper().IndexOf(AuthenticationProtocolValues.LOGIN_COMMAND) == 0) { action = new AuthenticateAction(action_login); validate_command = true; } if (data.ToUpper().IndexOf(AuthenticationProtocolValues.REGISTER_COMMAND) == 0) { action = new AuthenticateAction(action_register); validate_command = true; } //perform the action //if it failed the the process has failed if (!action()) { return(false); } } //While //when out of loop the command has succeed //send the authenticated message to the client chatserver.Write(client.GetStream(), AuthenticationProtocolValues.AUTENTICATED_MSG); return(true); }
//CHANGE ROOM private void action_change_room() { //store old room number int oldroom = room; //Remove the user from the chat room chatserver.RemoveRoomUser(chatserver.RoomUsers[oldroom - 1], nickname); //Assigned to No room first room = 0; //while no room is assigned while (room == 0) { //Get room number from client chatserver.Write(client.GetStream(), ChatProtocolValues.CHOOSE_ROOM(nickname, chatserver.NumRoom)); string temp = chatserver.Read(client.GetStream()); //check for valid room number try { //convert the text message to integer int r_n = int.Parse(temp); //check to make sure that the room number is within range if ((r_n >= 1) && (r_n <= chatserver.NumRoom)) { room = r_n; } } catch {} } //Add user to the assigned room chatserver.AddRoomUser(chatserver.RoomUsers[room - 1], nickname); //Broadcast to old room participants chatserver.Broadcast(ChatProtocolValues.MOVE_TO(nickname, room), oldroom); //Broadcast to new room participants chatserver.Broadcast(ChatProtocolValues.WELCOME(nickname, room), room); }