Beispiel #1
0
        private void ReciveFile(long fileSize, string fileName, Socket socket)
        {
            var segments = (fileSize / FileSenderHandler.FileSegmentSize);

            segments = segments * FileSenderHandler.FileSegmentSize == fileSize ? segments : segments + 1;

            long offset          = 0;
            long currentSegments = 1;

            IFileSenderHandler senderHandler = new FileSenderHandler();

            while (fileSize > offset)
            {
                ByteDataTransform  transfer = new ByteDataTransform();
                DataTransferResult result   = DataTransference.RecieveData(socket);
                if (result.Header.GetCommand() == 31)
                {
                    byte[] fileDataRecived = (byte[])result.objectResult;
                    int    size            = 0;
                    if (currentSegments == segments)
                    {
                        size = (int)(fileSize - offset);
                    }
                    else
                    {
                        size = (int)FileSenderHandler.FileSegmentSize;
                    }

                    senderHandler.Write(fileName, fileDataRecived);
                    offset += size;
                    currentSegments++;
                }
            }
        }
Beispiel #2
0
        private void SendFile(string path)
        {
            DataTransformSuper dataTransferSender = new StringDataTransform();

            IFileSenderHandler senderHandler = new FileSenderHandler();
            IFileHandler       fileHandler   = new FileHandler();
            long   fileSize = fileHandler.GetFileSize(path);
            string fileName = fileHandler.GetFileName(path);
            string message  = fileName + Common.DataTransfer.SEPARATOR + fileSize;
            Header header   = new Header(HeaderConstants.Request, CommandConstants.UploadFile, message.Length);

            var codedMessage = dataTransferSender.GenMenssage(message, header);

            DataTransference.SendData(codedMessage, socket);

            long segments = (fileSize / FileSenderHandler.FileSegmentSize);

            segments = segments * FileSenderHandler.FileSegmentSize == fileSize ? segments : segments + 1;

            long offset          = 0;
            long currentSegments = 1;

            while (fileSize > offset)
            {
                byte[] fileData;
                int    size = 0;
                if (currentSegments == segments)
                {
                    size     = (int)(fileSize - offset);
                    fileData = senderHandler.Read(path, offset, size);
                    offset  += size;
                    currentSegments++;
                }
                else
                {
                    size     = (int)FileSenderHandler.FileSegmentSize;
                    fileData = senderHandler.Read(path, offset, size);
                    offset  += size;
                    currentSegments++;
                }

                dataTransferSender = new ByteDataTransform();
                Header header1 = new Header(HeaderConstants.Request, CommandConstants.UploadFileSignal, size);

                var data = dataTransferSender.GenMenssage(fileData, header1);
                Console.WriteLine("Mande paquete " + currentSegments);
                DataTransference.SendData(data, socket);
            }
        }
Beispiel #3
0
        public void HandleClient(Socket socket)
        {
            DataTransformSuper transfer = new StringDataTransform();
            Response           response = new StringResponse();
            User   user     = new User();
            bool   endConn  = false;
            long   fileSize = 0;
            string fileName = "";

            while (isServerUp && !endConn)
            {
                DataTransferResult result = DataTransference.RecieveData(socket);
                if (result != null)
                {
                    int    command    = result.Header.GetCommand();
                    int    dataLength = result.Header.GetDataLength();
                    string direction  = result.Header.GetDirection();
                    string word       = "";
                    Log    log        = new Log()
                    {
                        Command = command,
                        Date    = DateTime.Now,
                    };
                    if (command != 31)
                    {
                        transfer            = new StringDataTransform();
                        result.objectResult = transfer.DecodeMessage((byte[])result.objectResult);
                        word        = (string)result.objectResult;
                        log.Message = word;
                    }
                    ;
                    dynamic responseData;


                    switch (command)
                    {
                    case CommandConstants.Login:
                        try
                        {
                            user         = Login(word);
                            response     = new StringResponse();
                            responseData = "true";
                            response.SendResponse(command, responseData, socket, responseData.Length);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (Exception)
                        {
                            response     = new StringResponse();
                            responseData = "false";
                            response.SendResponse(command, responseData, socket, responseData.Length);
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.SignUp:
                        try
                        {
                            SignUp(word);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (UserAlreadyExistException)
                        {
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.ListUsers:
                        try
                        {
                            List <string> usersList = GetUsers();
                            response     = new ListStringResponse();
                            responseData = usersList;
                            int responseDataLength = ListStringDataTransform.ListLength(usersList);
                            response.SendResponse(command, responseData, socket, responseDataLength);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Ocurrio un error al listar los usuarios");
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.ListFiles:
                        try
                        {
                            User userPhoto = new User();
                            userPhoto.Username = word;
                            List <string> fileList = GetUserPhotos(userPhoto);
                            response     = new ListStringResponse();
                            responseData = fileList;
                            int responseDataLength = ListStringDataTransform.ListLength(fileList);
                            if (responseDataLength == 0)
                            {
                                throw new Exception();
                            }
                            response.SendResponse(command, responseData, socket, responseDataLength);
                            SendSuccessfullLog(user, log);
                            break;
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Ocurrio un problema al listar los archivos");
                            response     = new StringResponse();
                            responseData = "Error al listar fotos";
                            response.SendResponse(Common.CommandConstants.ErrorListing, responseData, socket, responseData.Length);
                            SendWarningLog(log);
                            break;
                        }

                    case CommandConstants.UploadFile:
                        ReciveFileData(word, out fileName, out fileSize);
                        transfer = new ByteDataTransform();
                        Photo photo1 = new Photo();
                        photo1.Name     = fileName;
                        photo1.Comments = new List <string>();
                        user.AddPhoto(photo1);

                        SendSuccessfullLog(user, log);
                        break;

                    case CommandConstants.UploadFileSignal:
                        byte[]             fileBytes     = (byte[])result.objectResult;
                        IFileSenderHandler senderHandler = new FileSenderHandler();
                        senderHandler.Write(fileName, fileBytes);
                        log.Username = user.Username;
                        log.Message  = "File part";
                        SendLog(log);
                        break;

                    case CommandConstants.AddComent:
                        try
                        {
                            AddComment(word);
                            SendSuccessfullLog(user, log);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Ocurrio un problema al agregar el comentario");
                            SendWarningLog(log);
                        }
                        break;

                    case CommandConstants.ViewComents:
                        try
                        {
                            List <string> comments;
                            string        userName, photo;
                            GetCredentials(word, out userName, out photo);
                            Photo photoForComments = GetPhoto(userName, photo);
                            comments = photoForComments.Comments;

                            response     = new ListStringResponse();
                            responseData = comments;
                            int responseDataLength = ListStringDataTransform.ListLength(comments);
                            response.SendResponse(command, responseData, socket, responseDataLength);
                            SendSuccessfullLog(user, log);
                            break;
                        }catch (Exception)
                        {
                            Console.WriteLine("Error al mostrar comentarios de la foto");
                            response     = new StringResponse();
                            responseData = "Error al listar fotos";
                            response.SendResponse(Common.CommandConstants.ErrorListing, responseData, socket, responseData.Length);
                            SendWarningLog(log);
                            break;
                        }

                    default:
                        Console.WriteLine("Invalid command");
                        break;
                    }
                }
                else
                {
                    socket.Close();
                    _conectedClients.Remove(socket);
                    endConn = true;
                }
            }
        }