예제 #1
0
        public FileCloud dbExecuteGetFileQuery(string query, string[] parametersList, string outputPath)
        {
            SQLiteCommand command = null;
            FileCloud     file    = new FileCloud();

            try
            {
                command = new SQLiteCommand(query, this.dbConnection);

                FileStream   stream;
                BinaryWriter writer;

                int    bufferSize   = 1024;
                byte[] outputBuffer = new byte[bufferSize];
                // The bytes returned from GetBytes.
                long retval;
                // The starting position in the BLOB output.
                long startIndex = 0;

                // The publisher id to use in the file name.
                int      idFile   = 0;
                string   path     = string.Empty;
                DateTime version  = DateTime.MinValue;
                int      valid    = 0;
                string   fileHash = string.Empty;
                Int64    length   = 0;

                SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

                // read data
                reader.Read();

                idFile   = reader.GetInt32(reader.GetOrdinal(parametersList[0]));
                path     = reader.GetString(reader.GetOrdinal(parametersList[1]));
                version  = reader.GetDateTime(reader.GetOrdinal(parametersList[2]));
                valid    = reader.GetInt32(reader.GetOrdinal(parametersList[3]));
                fileHash = reader.GetString(reader.GetOrdinal(parametersList[4]));
                length   = reader.GetInt64(reader.GetOrdinal(parametersList[5]));

                file.setIdFile(idFile);
                file.setClientPath(path);
                file.setVersion(version);
                file.setValid(valid);
                file.setFileHash(fileHash);
                file.setFileLength(length);

                // Create a file to hold the output.
                stream = new FileStream(outputPath, FileMode.Create, FileAccess.Write);
                writer = new BinaryWriter(stream);

                // Reset the starting byte for the new BLOB.
                startIndex = 0;

                // Read bytes into outByte[] and retain the number of bytes returned.
                retval = reader.GetBytes(reader.GetOrdinal(parametersList[6]), startIndex, outputBuffer, 0, bufferSize);

                // Continue while there are bytes beyond the size of the buffer.
                while (retval == bufferSize)
                {
                    writer.Write(outputBuffer);
                    writer.Flush();

                    // Reposition start index to end of last buffer and fill buffer.
                    startIndex += bufferSize;
                    retval      = reader.GetBytes(reader.GetOrdinal(parametersList[6]), startIndex, outputBuffer, 0, bufferSize);
                }

                // Write the remaining buffer.
                writer.Write(outputBuffer, 0, (int)retval);
                writer.Flush();

                // Close the output file.
                writer.Close();
                stream.Close();

                reader.Close();

                return(file);
            }
            catch (System.InvalidOperationException e)
            {
                Console.WriteLine("server:DBmanager:dbExecuteGetFileQuery:Exception >> " + e.Message);
                if (command != null)
                {
                    command.Dispose();
                }
                return(file);
            }
            catch (SQLiteException e)
            {
                Console.WriteLine("server:DBmanager:dbExecuteGetFileQuery:Exception >> " + e.Message);
                if (command != null)
                {
                    command.Dispose();
                }
                return(file);
            }
        }
예제 #2
0
        public FileCloud NetReadFile(NetworkStream networkStream, string directoryPath, int timeout)
        {
            try
            {
                try
                {
                    FileCloud newFile = new FileCloud();
                    networkStream.ReadTimeout = timeout;
                    Byte[] buffer = new Byte[FILE_BUFFER_SIZE];

                    // Read file length (Int64)
                    networkStream.Read(buffer, 0, 8);
                    Int64 fileLength = BitConverter.ToInt64(buffer, 0);
                    newFile.setFileLength(fileLength);
                    NetWriteTextMsg(networkStream, Program.ACK);

                    // Read file name
                    string fileName = NetReadTextMsg(networkStream);
                    newFile.setOriginalFileName(fileName);
                    //TODO string receivingTimestampStr = Program.Now().ToString();
                    int receivingTimestamp = Program.Now().Millisecond;
                    newFile.setReceivingTimestamp(receivingTimestamp);
                    string localPath = string.Empty;
                    if (directoryPath[directoryPath.Length - 1] == '\\')
                    {
                        localPath = directoryPath + receivingTimestamp + "_" + fileName;
                    }
                    else
                    {
                        localPath = directoryPath + @"\" + receivingTimestamp + "_" + fileName;
                    }
                    newFile.setLocalPath(localPath);
                    NetWriteTextMsg(networkStream, Program.ACK);

                    // Read file
                    Int64 receivedBytes = 0;
                    using (Stream stream = new FileStream(localPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        if (fileLength > 0)
                        {
                            int readLength;
                            do
                            {
                                readLength = networkStream.Read(buffer, 0, buffer.Length);
                                stream.Write(buffer, 0, readLength);
                                receivedBytes += readLength;
                            }while (receivedBytes < fileLength);
                        }
                        NetWriteTextMsg(networkStream, Program.ACK);
                    }

                    return(newFile);
                }
                catch (IOException)
                {
                    Console.WriteLine("server:{0}:NetworkManager >> Timeout. Nothing received in {1} seconds", Thread.CurrentThread.Name, Program.NETWORK_TIMEOUT / 1000);
                    throw;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("server:{0}:NetworkManager:Exception >> {1}", Thread.CurrentThread.Name, e.Message);
                throw new SocketException();
            }
        }