コード例 #1
0
ファイル: SharedFile.cs プロジェクト: thaolt/BitChatClient
        internal static SharedFile PrepareDownloadFile(SharedFileMetaData metaData, BitChat chat, BitChat.Peer seeder, BitChatProfile profile, SynchronizationContext syncCxt)
        {
            //check if file already exists
            lock (_sharedFiles)
            {
                SharedFile sharedFile;

                if (_sharedFiles.ContainsKey(metaData.FileID))
                {
                    sharedFile = _sharedFiles[metaData.FileID];
                }
                else
                {
                    sharedFile          = new SharedFile(null, metaData, new FileBlockState[metaData.BlockHash.Length], 0, syncCxt);
                    sharedFile._profile = profile;

                    _sharedFiles.Add(metaData.FileID, sharedFile);
                }

                sharedFile.AddChat(chat);
                sharedFile.AddSeeder(seeder);

                return(sharedFile);
            }
        }
コード例 #2
0
ファイル: SharedFile.cs プロジェクト: thaolt/BitChatClient
            public FileBlockDownloadManager(SharedFile sharedFile, int blockNumber, int blockSize)
            {
                _sharedFile  = sharedFile;
                _blockNumber = blockNumber;
                _blockData   = new byte[blockSize];

                _lastResponse = DateTime.UtcNow;
            }
コード例 #3
0
ファイル: SharedFile.cs プロジェクト: thaolt/BitChatClient
        internal static SharedFile LoadFile(string filePath, SharedFileMetaData metaData, FileBlockState[] blockAvailable, bool isPaused, BitChat chat, SynchronizationContext syncCxt)
        {
            //check if file already shared
            lock (_sharedFiles)
            {
                SharedFile sharedFile;

                if (_sharedFiles.ContainsKey(metaData.FileID))
                {
                    sharedFile = _sharedFiles[metaData.FileID];
                }
                else
                {
                    int availableBlocksCount = 0;

                    for (int i = 0; i < blockAvailable.Length; i++)
                    {
                        if (blockAvailable[i] == FileBlockState.Available)
                        {
                            availableBlocksCount++;
                        }
                    }

                    FileStream fS;

                    if (blockAvailable.Length == availableBlocksCount)
                    {
                        fS = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    }
                    else
                    {
                        fS = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                    }

                    sharedFile        = new SharedFile(fS, metaData, blockAvailable, availableBlocksCount, syncCxt);
                    sharedFile._state = SharedFileState.Paused;

                    _sharedFiles.Add(metaData.FileID, sharedFile);

                    if (!isPaused)
                    {
                        sharedFile.Start();
                    }
                }

                sharedFile.AddChat(chat);

                return(sharedFile);
            }
        }
コード例 #4
0
        public SharedFileItem(SharedFile file, BitChat chat)
        {
            InitializeComponent();

            _file = file;
            _chat = chat;

            _fileSizeFormatted = WebUtilities.GetFormattedSize(_file.MetaData.FileSize);

            labFileName.Text = _file.MetaData.FileName;
            labSpeed.Text = "";

            if (_file.TotalPeers > 1)
                labInfo1.Text = _fileSizeFormatted + " (" + _file.TotalPeers + " peers)";
            else
                labInfo1.Text = _fileSizeFormatted + " (" + _file.TotalPeers + " peer)";

            _file.FileDownloadStarted += OnFileDownloadStarted;
            _file.FileDownloaded += OnFileDownloaded;
            _file.BlockDownloaded += OnBlockDownloaded;
            _file.PeerCountUpdate += OnPeerCountUpdate;
            _file.FileTransferSpeedUpdate += OnFileTransferSpeedUpdate;
            _file.FileRemoved += OnFileRemoved;

            if (_file.State == SharedFileState.Advertisement)
            {
                _type = SharedFileItemType.Advertisement;
                this.BackColor = _BackColorAvailable;

                pbFileProgress.Visible = false;
            }
            else if (_file.IsComplete)
            {
                _type = SharedFileItemType.Sharing;

                labSpeed.ForeColor = Color.Blue;
                pbFileProgress.Visible = false;
            }
            else
            {
                _type = SharedFileItemType.Downloading;

                pbFileProgress.Visible = true;
                pbFileProgress.Value = _file.PercentComplete;
            }
        }
コード例 #5
0
ファイル: BitChat.cs プロジェクト: spthaolt/BitChatClient
 private void SendFileAdvertisement(SharedFile sharedFile)
 {
     byte[] packetData = BitChatMessage.CreateFileAdvertisement(sharedFile.MetaData);
     _network.WritePacketBroadcast(packetData, 0, packetData.Length);
 }
コード例 #6
0
ファイル: BitChat.cs プロジェクト: spthaolt/BitChatClient
 private void RaiseEventFileAdded(SharedFile file)
 {
     _syncCxt.Post(FileAddedCallback, file);
 }
コード例 #7
0
ファイル: BitChat.cs プロジェクト: spthaolt/BitChatClient
 internal void RemoveSharedFile(SharedFile file)
 {
     lock (_sharedFiles)
     {
         _sharedFiles.Remove(file.MetaData.FileID);
     }
 }
コード例 #8
0
ファイル: BitChat.cs プロジェクト: spthaolt/BitChatClient
 public SharedFile[] GetSharedFileList()
 {
     lock (_sharedFiles)
     {
         SharedFile[] sharedFilesList = new SharedFile[_sharedFiles.Count];
         _sharedFiles.Values.CopyTo(sharedFilesList, 0);
         return sharedFilesList;
     }
 }
コード例 #9
0
        private void _chat_FileAdded(BitChat sender, SharedFile sharedFile)
        {
            SharedFileItem item = new SharedFileItem(sharedFile, _chat);
            item.FileRemoved += OnFileRemoved;

            lstFiles.AddItem(item);
        }
コード例 #10
0
ファイル: SharedFile.cs プロジェクト: thaolt/BitChatClient
        internal static SharedFile ShareFile(string filePath, string hashAlgo, BitChat chat, SynchronizationContext syncCxt)
        {
            FileStream fS = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            HashAlgorithm hash     = HashAlgorithm.Create(hashAlgo);
            int           hashSize = hash.HashSize / 8;

            //calculate block size
            int blockSize;
            {
                //header size = ChatMessage header + FileAdvertisement header
                int packetHeaderSize    = (20 + 1 + 2) + (1 + 1 + 255 + 1 + 255 + 8 + 8 + 4 + 1 + 10 + 1);
                int packetDataSize      = 65536 - packetHeaderSize;
                int totalBlocksPossible = packetDataSize / hashSize;
                blockSize = Convert.ToInt32(fS.Length / totalBlocksPossible);

                if (blockSize <= short.MaxValue)
                {
                    blockSize = short.MaxValue + 1;
                }
                else
                {
                    //align to 16 bytes
                    int remainder = blockSize % 16;
                    if (remainder > 0)
                    {
                        blockSize = blockSize - remainder + 16;
                    }
                }
            }

            //compute block hashes and file info hash
            int totalBlocks = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((double)fS.Length / blockSize)));

            byte[][]         blockHash      = new byte[totalBlocks][];
            FileBlockState[] blockAvailable = new FileBlockState[totalBlocks];

            //init
            for (int i = 0; i < totalBlocks; i++)
            {
                long offset = i * blockSize;
                long length = blockSize;

                if ((offset + length) > fS.Length)
                {
                    length = fS.Length - offset;
                }

                blockHash[i]      = hash.ComputeHash(new OffsetStream(fS, offset, length));
                blockAvailable[i] = FileBlockState.Available;
            }

            //get file meta data
            SharedFileMetaData metaData = new SharedFileMetaData(Path.GetFileName(fS.Name), WebUtilities.GetContentType(fS.Name), File.GetLastWriteTimeUtc(fS.Name), fS.Length, blockSize, hashAlgo, blockHash);

            //check if file already shared
            lock (_sharedFiles)
            {
                SharedFile sharedFile;

                if (_sharedFiles.ContainsKey(metaData.FileID))
                {
                    sharedFile = _sharedFiles[metaData.FileID];

                    if (sharedFile._isComplete)
                    {
                        fS.Dispose();
                    }
                    else
                    {
                        sharedFile.Remove(chat);

                        sharedFile = new SharedFile(fS, metaData, blockAvailable, blockAvailable.Length, syncCxt);
                        sharedFile.StartSharing();

                        _sharedFiles.Add(metaData.FileID, sharedFile);
                    }
                }
                else
                {
                    sharedFile = new SharedFile(fS, metaData, blockAvailable, blockAvailable.Length, syncCxt);
                    sharedFile.StartSharing();

                    _sharedFiles.Add(metaData.FileID, sharedFile);
                }

                sharedFile.AddChat(chat);

                return(sharedFile);
            }
        }
コード例 #11
0
        internal static SharedFile ShareFile(string filePath, string hashAlgo, BitChat chat, SynchronizationContext syncCxt)
        {
            FileStream fS = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            HashAlgorithm hash = HashAlgorithm.Create(hashAlgo);
            int hashSize = hash.HashSize / 8;

            //calculate block size
            int blockSize;
            {
                //header size = ChatMessage header + FileAdvertisement header
                int packetHeaderSize = (20 + 1 + 2) + (1 + 1 + 255 + 1 + 255 + 8 + 8 + 4 + 1 + 10 + 1);
                int packetDataSize = 65536 - packetHeaderSize;
                int totalBlocksPossible = packetDataSize / hashSize;
                blockSize = Convert.ToInt32(fS.Length / totalBlocksPossible);

                if (blockSize <= short.MaxValue)
                    blockSize = short.MaxValue + 1;
                else
                {
                    //align to 16 bytes
                    int remainder = blockSize % 16;
                    if (remainder > 0)
                        blockSize = blockSize - remainder + 16;
                }
            }

            //compute block hashes and file info hash
            int totalBlocks = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((double)fS.Length / blockSize)));
            byte[][] blockHash = new byte[totalBlocks][];
            FileBlockState[] blockAvailable = new FileBlockState[totalBlocks];

            //init
            for (int i = 0; i < totalBlocks; i++)
            {
                long offset = i * blockSize;
                long length = blockSize;

                if ((offset + length) > fS.Length)
                    length = fS.Length - offset;

                blockHash[i] = hash.ComputeHash(new OffsetStream(fS, offset, length));
                blockAvailable[i] = FileBlockState.Available;
            }

            //get file meta data
            SharedFileMetaData metaData = new SharedFileMetaData(Path.GetFileName(fS.Name), WebUtilities.GetContentType(fS.Name), File.GetLastWriteTimeUtc(fS.Name), fS.Length, blockSize, hashAlgo, blockHash);

            //check if file already shared
            lock (_sharedFiles)
            {
                SharedFile sharedFile;

                if (_sharedFiles.ContainsKey(metaData.FileID))
                {
                    sharedFile = _sharedFiles[metaData.FileID];

                    if (sharedFile._isComplete)
                    {
                        fS.Dispose();
                    }
                    else
                    {
                        sharedFile.Remove(chat);

                        sharedFile = new SharedFile(fS, metaData, blockAvailable, blockAvailable.Length, syncCxt);
                        sharedFile.StartSharing();

                        _sharedFiles.Add(metaData.FileID, sharedFile);
                    }
                }
                else
                {
                    sharedFile = new SharedFile(fS, metaData, blockAvailable, blockAvailable.Length, syncCxt);
                    sharedFile.StartSharing();

                    _sharedFiles.Add(metaData.FileID, sharedFile);
                }

                sharedFile.AddChat(chat);

                return sharedFile;
            }
        }
コード例 #12
0
        internal static SharedFile PrepareDownloadFile(SharedFileMetaData metaData, BitChat chat, BitChat.Peer seeder, BitChatProfile profile, SynchronizationContext syncCxt)
        {
            //check if file already exists
            lock (_sharedFiles)
            {
                SharedFile sharedFile;

                if (_sharedFiles.ContainsKey(metaData.FileID))
                {
                    sharedFile = _sharedFiles[metaData.FileID];
                }
                else
                {
                    sharedFile = new SharedFile(null, metaData, new FileBlockState[metaData.BlockHash.Length], 0, syncCxt);
                    sharedFile._profile = profile;

                    _sharedFiles.Add(metaData.FileID, sharedFile);
                }

                sharedFile.AddChat(chat);
                sharedFile.AddSeeder(seeder);

                return sharedFile;
            }
        }
コード例 #13
0
        internal static SharedFile LoadFile(string filePath, SharedFileMetaData metaData, FileBlockState[] blockAvailable, bool isPaused, BitChat chat, SynchronizationContext syncCxt)
        {
            //check if file already shared
            lock (_sharedFiles)
            {
                SharedFile sharedFile;

                if (_sharedFiles.ContainsKey(metaData.FileID))
                {
                    sharedFile = _sharedFiles[metaData.FileID];
                }
                else
                {
                    int availableBlocksCount = 0;

                    for (int i = 0; i < blockAvailable.Length; i++)
                    {
                        if (blockAvailable[i] == FileBlockState.Available)
                            availableBlocksCount++;
                    }

                    FileStream fS;

                    if (blockAvailable.Length == availableBlocksCount)
                        fS = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    else
                        fS = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);

                    sharedFile = new SharedFile(fS, metaData, blockAvailable, availableBlocksCount, syncCxt);
                    sharedFile._state = SharedFileState.Paused;

                    _sharedFiles.Add(metaData.FileID, sharedFile);

                    if (!isPaused)
                        sharedFile.Start();
                }

                sharedFile.AddChat(chat);

                return sharedFile;
            }
        }
コード例 #14
0
            public FileBlockDownloadManager(SharedFile sharedFile, int blockNumber, int blockSize)
            {
                _sharedFile = sharedFile;
                _blockNumber = blockNumber;
                _blockData = new byte[blockSize];

                _lastResponse = DateTime.UtcNow;
            }