Exemple #1
0
        private bool CommandDownloadFile(Connection connection, Data dato)
        {
            // login + "|" + owner + "|"+hashfile;
            string[] payload  = dato.Payload.Message.Split(ParseConstants.SEPARATOR_PIPE);
            string   login    = payload[0];
            string   owner    = payload[1];
            string   hashfile = payload[2];

            FileInfo fi = FileOperationsSingleton.GetInstance().GetFile(hashfile, owner);

            //   Data retDato;
            if (fi == null)
            {
                /*
                 * retDato = new Data()
                 * {
                 *  Command = Command.RES,
                 *  OpCode = OpCodeConstants.RES_DOWNLOAD_FILE,
                 *  Payload = new Payload() { Message = "ERROR [wrong file or not found]" }
                 * };
                 * foreach (var item in retDato.GetBytes())
                 * {
                 *  Console.WriteLine("Envio :{0}", ConversionUtil.GetString(item));
                 *  connection.WriteToStream(item);
                 * }
                 * */
                return(false);
            }
            else
            {
                long size = fi.Length;

                log.Info("Espero antes de mandar el binario");
                //  Thread.Sleep(2000);
                FileStream fileStream = fi.OpenRead();
                const int  BUFF_SIZE  = 10000;
                byte[]     buffer     = new byte[BUFF_SIZE];
                long       sentData   = 0;
                bool       done       = false;
                try
                {
                    while (!done)
                    {
                        int countRead = fileStream.Read(buffer, 0, BUFF_SIZE);
                        //   Thread.Sleep(20);
                        log.DebugFormat("countRead={0}", countRead);
                        sentData += countRead;
                        if (countRead > 0)
                        {
                            if (sentData == size)
                            {
                                done = true;
                            }
                            connection.WriteToNetworkStream(buffer, 0, countRead);
                            log.DebugFormat("Enviando {2} - {0}: {3}  - {1}", countRead, size, fi.FullName, sentData);
                            //   Thread.Sleep(2000);
                        }
                        else
                        {//no leyo nada de la entrada (cantidad de bytes justa, en la siguiente lectura)
                            done = true;
                        }
                    }
                    connection.FlushNetworkStream();
                }
                catch (Exception e)
                {
                    log.Error("descarga", e);
                }
                fileStream.Close();
                log.DebugFormat("Archivo puesto todo en el stream , y crerrado el file local");
            }
            return(true); //terminar la conexion
        }