public static async Task <bool> ValidateLogin(Server server, CommunicationClient client) { var email = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength)); var password = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserPasswordLength)); var user = new UserDto() { Email = email, Password = password }; var existUser = await server.Service.AutenticateUserAsync(user); if (!existUser) { ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Error, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes("Invalid User", ProtocolConstants.ResponseMessageLength)); } else { client.User = new User() { Email = email, Password = password }; loggerService.SendMessages("Login Successfully, mail: " + email); ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Ok, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes("Login Successfully", ProtocolConstants.ResponseMessageLength)); } return(existUser); }
public static async Task <MessageResponse> HandleImageUpload(Client client, string filePath) { if (!FileHandler.FileExists(filePath)) { return new MessageResponse() { responseCommands = ProtocolConstants.ResponseCommands.Error, Message = "Invalid File", } } ; var nameData = ConversionHandler.ConvertStringToBytes(FileHandler.FileName(filePath), Photo.PhotoNameLength); var extensionData = ConversionHandler.ConvertStringToBytes(FileHandler.FileExtension(filePath), Photo.PhotoExtensionLength); var fileSize = FileHandler.GetFileSize(filePath); var fileSizeData = ConversionHandler.ConvertLongToBytes(fileSize); ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.UPLOAD_PHOTO, client.StreamCommunication); client.StreamCommunication.Write(nameData); client.StreamCommunication.Write(extensionData); client.StreamCommunication.Write(fileSizeData); FileHandler.SendFileWithStream(fileSize, filePath, client.StreamCommunication); return(await ProtocolHelpers.RecieveMessageCommand(client.StreamCommunication)); }
public static async Task <MessageResponse> HandleCommentCreation(Client client, Comment comment) { ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.COMMENT_PHOTO, client.StreamCommunication); var photoIdData = ConversionHandler.ConvertLongToBytes(comment.Photo.Id); var commentData = ConversionHandler.ConvertStringToBytes(comment.Message, Comment.CommentLength); client.StreamCommunication.Write(photoIdData); client.StreamCommunication.Write(commentData); return(await ProtocolHelpers.RecieveMessageCommand(client.StreamCommunication)); }
public static async Task <MessageResponse> HandleLogin(Client client, User user) { ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.LOGIN, client.StreamCommunication); var email = ConversionHandler.ConvertStringToBytes(user.Email, User.UserEmailLength); var password = ConversionHandler.ConvertStringToBytes(user.Password, User.UserPasswordLength); client.StreamCommunication.Write(email); client.StreamCommunication.Write(password); return(await ProtocolHelpers.RecieveMessageCommand(client.StreamCommunication)); }
public static async Task <MessageResponse> HandleRegister(Client client, User user) { ProtocolHelpers.SendRequestCommand(ProtocolConstants.RequestCommands.CREATE_USER, client.StreamCommunication); var name = ConversionHandler.ConvertStringToBytes(user.Name, User.UserNameLength); var email = ConversionHandler.ConvertStringToBytes(user.Email, User.UserEmailLength); var password = ConversionHandler.ConvertStringToBytes(user.Password, User.UserPasswordLength); client.StreamCommunication.Write(email); client.StreamCommunication.Write(name); client.StreamCommunication.Write(password); var response = await ProtocolHelpers.RecieveMessageCommand(client.StreamCommunication); return(response); }
private async Task ProcessCommands(CommunicationClient client) { var request = ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); var commandType = ConversionHandler.ConvertBytesToShort(await client.StreamCommunication.ReadAsync(ProtocolConstants.ShortTypeLength)); switch (commandType) { case (short)ProtocolConstants.RequestCommands.LOGIN: await ClientHandler.HandleCreateUser(this, client); break; case (short)ProtocolConstants.RequestCommands.UPLOAD_PHOTO: await ClientHandler.HandleUploadPhoto(this, client); break; case (short)ProtocolConstants.RequestCommands.VIEW_USERS: await ClientHandler.HandleViewUsers(this, client); break; case (short)ProtocolConstants.RequestCommands.VIEW_PHOTOS: await ClientHandler.HandleViewPhotos(this, client); break; case (short)ProtocolConstants.RequestCommands.VIEW_COMMENTS: await ClientHandler.HandleViewCommentsPhoto(this, client); break; case (short)ProtocolConstants.RequestCommands.COMMENT_PHOTO: await ClientHandler.HandleCommentPhoto(this, client); break; default: ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Error, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes("Invalid User", ProtocolConstants.ResponseMessageLength)); break; } }
public static async Task <bool> HandleCreateUser(Server server, CommunicationClient client) { var name = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserNameLength)); var email = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserEmailLength)); var password = ConversionHandler.ConvertBytesToString(await client.StreamCommunication.ReadAsync(User.UserPasswordLength)); if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(email)) { ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Error, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes("Input Error", ProtocolConstants.ResponseMessageLength)); } var user = new UserDto() { Email = email, Name = name, Password = password, IsLogedIn = true }; var response = await server.Service.AddUserAsync(user); if (response.Status.Equals("Ok")) { client.User = new User() { Email = email, Name = name, Password = password, }; loggerService.SendMessages("User created, mail: " + email); ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Ok, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes(response.Message, ProtocolConstants.ResponseMessageLength)); } else { ProtocolHelpers.SendResponseCommand(ProtocolConstants.ResponseCommands.Error, client.StreamCommunication); client.StreamCommunication.Write(ConversionHandler.ConvertStringToBytes(response.Message, ProtocolConstants.ResponseMessageLength)); } return(response.Status.Equals("Ok")); }