public UploadResponse SendFile(TCPProtocol protocol)
        {
            try
            {
                UploadResponse response;

                //Envio mensaje pidiendo subir archivo
                protocol.SendMessage(this);

                //Recibo respuesta
                response = (UploadResponse)protocol.RecieveMessage();

                if (response.Result != TransferResponseEnum.OK)
                {
                    return(response);
                }

                //Envio el archivo
                protocol.SendFile(this.File);

                //Recibo respuesta
                response = (UploadResponse)protocol.RecieveMessage();

                return(response);
            }
            catch (Exception ex)
            {
                throw new Exception("Error enviando mensaje " + ex.Message);
            }
        }
예제 #2
0
        public ListFilesResponse ListFiles(TCPProtocol protocol)
        {
            try
            {
                ListFilesResponse response;

                //Envio mensaje pidiendo descargar archivo
                protocol.SendMessage(this);

                //Recibo respuesta
                response = (ListFilesResponse)protocol.RecieveMessage();

                if (response.Result != TransferResponseEnum.OK)
                {
                    return(response);
                }

                //Envio request nuevamente
                protocol.SendMessage(this);


                return(response);
            }
            catch (Exception ex)
            {
                throw new Exception("Error enviando mensaje " + ex.Message);
            }
        }
        public DownloadResponse DownloadFile(TCPProtocol protocol, string repository)
        {
            try
            {
                DownloadResponse response;

                protocol.SendMessage(this);

                response = (DownloadResponse)protocol.RecieveMessage();

                if (response.Result != TransferResponseEnum.OK)
                {
                    return(response);
                }

                protocol.SendMessage(this);

                bool resp = protocol.RecieveFile(string.Format("{0}\\{1}", repository, this.FileName), Rest == 0, response.FileLength - this.Rest);

                response.Result = resp ? TransferResponseEnum.OK : TransferResponseEnum.Error;

                return(response);
            }
            catch (Exception ex)
            {
                throw new Exception("Error enviando mensaje " + ex.Message);
            }
        }
        public void Listen(TcpListener tcpListener)
        {
            while (!_shouldStop)
            {
                var protocol   = new TCPProtocol();
                var clientName = String.Empty;
                protocol.Listen(tcpListener);

                try
                {
                    var message = protocol.RecieveMessage();
                    clientName = message.ClientName;

                    //Le comunico al servidor de conciliacion que un cliente se conecto.
                    var remotingRequest = new ClientConnectionRequest()
                    {
                        ClientName = clientName
                    };
                    _remotingService.SendMessage(remotingRequest);

                    if (message.Header == MessageTypeEnum.REQ)
                    {
                        if (message.Cmd == CommandEnum.Download)
                        {
                            DownloadFile(protocol, (DownloadRequest)message);
                        }
                        else if (message.Cmd == CommandEnum.Upload)
                        {
                            UploadFile(protocol, (UploadRequest)message);
                        }
                        else if (message.Cmd == CommandEnum.ListFiles)
                        {
                            SendFileList(protocol, (ListFilesRequest)message);
                        }
                        else if (message.Cmd == CommandEnum.Backup)
                        {
                            SyncFiles(protocol, (ListFilesRequest)message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Ocurrio un error.", ex.Message);
                }
                finally
                {
                    protocol.Disconnect();
                    //Le comunico al servidor de conciliacion que un cliente se desconecto.
                    var remotingRequest = new ClientDisconnectionRequest()
                    {
                        ClientName = clientName
                    };
                    _remotingService.SendMessage(remotingRequest);
                }
            }
        }
예제 #5
0
        public void SyncRecieve(TcpListener tcpListener)
        {
            while (!_shouldStop)
            {
                var protocol = new TCPProtocol();
                protocol.Listen(tcpListener);

                try
                {
                    var mensaje = protocol.RecieveMessage();

                    if (mensaje.Header == MessageTypeEnum.REQ && mensaje.Cmd == CommandEnum.Backup)
                    {
                        //Recibo un req para listar archivos por este puerto, se que es un pedido de sync
                        var response = new ListFilesResponse();

                        try
                        {
                            response.Cmd    = CommandEnum.ListFiles;
                            response.Result = TransferResponseEnum.OK;

                            StringBuilder sb = new StringBuilder();

                            foreach (var file in ServerData.Instance.FileList)
                            {
                                sb.Append(file);
                                sb.Append(",");
                            }

                            response.Data       = sb.ToString();
                            response.FileLength = sb.Length;

                            //Envio respuesta
                            protocol.SendMessage(response);
                        }
                        catch (Exception ex)
                        {
                            response.Result = TransferResponseEnum.ConnectionError;

                            protocol.SendMessage(response);
                        }
                    }
                    else if (mensaje.Header == MessageTypeEnum.REQ && mensaje.Cmd == CommandEnum.Download)
                    {
                        this.DownloadFile(protocol, (DownloadRequest)mensaje);
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Error enviando datos {0}", ex.Message));
                }
            }
        }
예제 #6
0
        private void DownloadFile(TCPProtocol protocol, DownloadRequest request)
        {
            var response = new DownloadResponse();

            try
            {
                byte[] archivo = null;


                response.Result = ServerData.Instance.FileExists(request.FileName) ? TransferResponseEnum.OK : TransferResponseEnum.FileDoesntExist;


                if (response.Result == TransferResponseEnum.OK)
                {
                    archivo             = FileHelper.ReadFile(string.Format("{0}\\{1}", ServerData.Instance.FileRepository, request.FileName));
                    response.FileLength = archivo.LongLength;
                }

                protocol.SendMessage(response);

                if (response.Result == TransferResponseEnum.OK)
                {
                    request = (DownloadRequest)protocol.RecieveMessage();

                    byte[] archivoAEnviar = new byte[archivo.LongLength - request.Rest];
                    Array.Copy(archivo, request.Rest, archivoAEnviar, 0, archivoAEnviar.LongLength);

                    protocol.SendFile(archivoAEnviar);
                }
                else
                {
                    throw new Exception("Error enviando archivo");
                }
            }
            catch (Exception ex)
            {
                response.Result = TransferResponseEnum.ConnectionError;

                protocol.SendMessage(response);
            }
        }
        private void DownloadFile(TCPProtocol protocol, DownloadRequest request)
        {
            var response = new DownloadResponse();

            try
            {
                //Le comunico al servidor de conciliacion que un usuario pidio descargar un archivo.
                var remotingRequest = new ClientDownloadRequest()
                {
                    ClientName = request.ClientName,
                    FileName   = request.FileName
                };
                _remotingService.SendMessage(remotingRequest);


                byte[] archivo = null;


                response.Result = ServerData.Instance.FileExists(request.FileName) ? TransferResponseEnum.OK : TransferResponseEnum.FileDoesntExist;


                if (response.Result == TransferResponseEnum.OK)
                {
                    archivo             = FileHelper.ReadFile(string.Format("{0}\\{1}", ServerData.Instance.FileRepository, request.FileName));
                    response.FileLength = archivo.LongLength;
                }

                protocol.SendMessage(response);

                if (response.Result == TransferResponseEnum.OK)
                {
                    request = (DownloadRequest)protocol.RecieveMessage();

                    byte[] archivoAEnviar = new byte[archivo.LongLength - request.Rest];
                    Array.Copy(archivo, request.Rest, archivoAEnviar, 0, archivoAEnviar.LongLength);

                    protocol.SendFile(archivoAEnviar);
                }
                else
                {
                    throw new Exception("Error enviando archivo");
                }
            }
            catch (System.IO.FileNotFoundException e)
            {
                response.Result = TransferResponseEnum.FileDoesntExist;
                //Actualizo mi lista, por las dudas.
                ServerData.Instance.FileList = new List <string>();

                foreach (string pathArchivo in FileHelper.SearchFilesInLocation(ServerData.Instance.FileRepository))
                {
                    ServerData.Instance.FileList.Add(Path.GetFileName(pathArchivo));
                }

                //Envio respuesta.
                protocol.SendMessage(response);
            }
            catch (Exception ex)
            {
                response.Result = TransferResponseEnum.ConnectionError;

                protocol.SendMessage(response);
            }
        }