Beispiel #1
0
 public TransferPartial(OpTransfer transfer)
 {
     Created      = transfer.Created;
     Details      = transfer.Details;
     Target       = transfer.Target;
     BitCount     = transfer.LocalBitfield.Length;
     Bitfield     = transfer.LocalBitfield;
     InternalSize = transfer.InternalSize;
     ChunkSize    = transfer.ChunkSize;
 }
Beispiel #2
0
        public static TransferPing Decode(G2Header root)
        {
            TransferPing ping = new TransferPing();

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (child.Name == Packet_RequestInfo)
                {
                    ping.RequestInfo = true;
                }

                if (child.Name == Packet_RequestAlts)
                {
                    ping.RequestAlts = true;
                }

                if (child.Name == Packet_BitfieldUpdated)
                {
                    ping.BitfieldUpdated = true;
                }


                if (!G2Protocol.ReadPayload(child))
                {
                    continue;
                }

                switch (child.Name)
                {
                case Packet_Target:
                    ping.Target = BitConverter.ToUInt64(child.Data, child.PayloadPos);
                    break;

                case Packet_Details:
                    ping.Details = FileDetails.Decode(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize));
                    break;

                case Packet_Status:
                    ping.Status = (TransferStatus)BitConverter.ToInt32(child.Data, child.PayloadPos);
                    break;

                case Packet_MissingDepth:
                    ping.MissingDepth = CompactNum.ToInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(ping);
        }
Beispiel #3
0
        public static FileDetails Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            if (!G2Protocol.ReadPacket(root))
            {
                return(null);
            }

            if (root.Name != TransferPacket.Params)
            {
                return(null);
            }

            return(FileDetails.Decode(root));
        }
Beispiel #4
0
        public static TransferPartial Decode(G2Header root)
        {
            TransferPartial partial = new TransferPartial();

            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                {
                    continue;
                }

                switch (child.Name)
                {
                case Packet_Created:
                    partial.Created = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos));
                    break;

                case Packet_Details:
                    partial.Details = FileDetails.Decode(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize));
                    break;

                case Packet_Target:
                    partial.Target = BitConverter.ToUInt64(child.Data, child.PayloadPos);
                    break;

                case Packet_BitCount:
                    partial.BitCount = CompactNum.ToInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_Bitfield:
                    partial.Bitfield = Utilities.ToBitArray(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize), partial.BitCount);
                    break;

                case Packet_InternalSize:
                    partial.InternalSize = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_ChunkSize:
                    partial.ChunkSize = CompactNum.ToInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(partial);
        }
Beispiel #5
0
        public override bool Equals(object obj)
        {
            FileDetails compare = obj as FileDetails;

            if (obj == null)
            {
                return(false);
            }

            if (compare.Service == Service &&
                compare.DataType == DataType &&
                Utilities.MemCompare(compare.Hash, Hash) &&
                compare.Size == Size &&
                Utilities.MemCompare(compare.Extra, Extra))
            {
                return(true);
            }

            return(false);
        }
Beispiel #6
0
        public static FileDetails Decode(G2Header root)
        {
            FileDetails packet = new FileDetails();
            G2Header    child  = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                {
                    continue;
                }

                switch (child.Name)
                {
                case Packet_Service:
                    packet.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_DataType:
                    packet.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_Hash:
                    packet.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_Size:
                    packet.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_Extra:
                    packet.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(packet);
        }
Beispiel #7
0
        public static FileDetails Decode(G2Header root)
        {
            FileDetails packet = new FileDetails();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Service:
                        packet.Service = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_DataType:
                        packet.DataType = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Hash:
                        packet.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Size:
                        packet.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Extra:
                        packet.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return packet;
        }
Beispiel #8
0
 public TransferPartial(OpTransfer transfer)
 {
     Created = transfer.Created;
     Details = transfer.Details;
     Target = transfer.Target;
     BitCount = transfer.LocalBitfield.Length;
     Bitfield = transfer.LocalBitfield;
     InternalSize = transfer.InternalSize;
     ChunkSize = transfer.ChunkSize;
 }
Beispiel #9
0
        string Transfers_PublicRequest(ulong key, FileDetails details)
        {
            ShareCollection collection;
            if (!Collections.SafeTryGetValue(key, out collection))
                return null;

            if (collection.Size != details.Size || !Utilities.MemCompare(collection.Hash, details.Hash))
                return null;

            return GetPublicPath(collection);
        }
Beispiel #10
0
        bool Transfers_FileSearch(ulong key, FileDetails details)
        {
            if (details.Extra == null || details.Extra.Length < 8)
                return false;

            OpPost post = GetPost(key, PostUID.FromBytes(details.Extra, 0));

            if (post != null && details.Size == post.Header.FileSize && Utilities.MemCompare(details.Hash, post.Header.FileHash))
                return true;

            return false;
        }
Beispiel #11
0
        void DownloadPost(SignedData signed, PostHeader header)
        {
            if (!Utilities.CheckSignedData(header.Source, signed.Data, signed.Signature))
                return;

            FileDetails details = new FileDetails(ServiceID, 0, header.FileHash, header.FileSize, new PostUID(header).ToBytes());

            Core.Transfers.StartDownload(header.TargetID, details, GetPostPath(header), new EndDownloadHandler(EndDownload), new object[] { signed, header });
        }
Beispiel #12
0
        public void DownloadFile(ulong id, StorageFile file)
        {
            // called from hash thread
            if (Core.InvokeRequired)
            {
                Core.RunInCoreAsync(delegate() { DownloadFile(id, file); });
                return;
            }

            // if file still processing return
            if (file.Hash == null)
                return;

            FileDetails details = new FileDetails(ServiceID, FileTypeData, file.Hash, file.Size, null);

            Core.Transfers.StartDownload(id, details, GetFilePath(file.HashID), new EndDownloadHandler(EndDownloadFile), new object[] { file });
        }
Beispiel #13
0
        string Transfers_FileRequest(ulong key, FileDetails details)
        {
            if (details.Extra == null || details.Extra.Length < 8)
                return null;

            OpPost post = GetPost(key, PostUID.FromBytes(details.Extra, 0));

            if (post != null && details.Size == post.Header.FileSize && Utilities.MemCompare(details.Hash, post.Header.FileHash))
                return GetPostPath(post.Header);

            return null;
        }
Beispiel #14
0
        string Transfers_FileRequest(ulong key, FileDetails details)
        {
            OpVersionedFile vfile = GetFile(key);

            if (vfile != null)
                if (details.Size == vfile.Header.FileSize && Utilities.MemCompare(details.Hash, vfile.Header.FileHash))
                    return GetFilePath(vfile.Header);

            return null;
        }
Beispiel #15
0
        private void ReceivePublicDetails(RudpSession session, ShareCollection file)
        {
            ShareCollection collection;
            if (!Collections.SafeTryGetValue(session.UserID, out collection))
                return;

            collection.Key = file.Key;
            collection.Size = file.Size;
            collection.Hash = file.Hash;

            foreach (DhtClient done in collection.ToRequest.Where(t => t.ClientID == session.ClientID).ToArray())
                collection.ToRequest.Remove(done);

            FileDetails details = new FileDetails(ServiceID, DataTypePublic, file.Hash, file.Size, null);
            object[] args = new object[] { collection, (object) session.ClientID };

            DhtClient client = new DhtClient(session.UserID, session.ClientID);
            OpTransfer transfer = Core.Transfers.StartDownload(client.UserID, details, GetPublicPath(collection), new EndDownloadHandler(CollectionDownloadFinished), args);
            transfer.AddPeer(client);
            transfer.DoSearch = false;

            collection.Status = "Starting List Download";
        }
Beispiel #16
0
        bool Transfers_FileSearch(ulong key, FileDetails details)
        {
            UpdateInfo signed = Core.Context.SignedUpdate;

            return (signed != null && signed.Loaded &&
                    signed.Size == details.Size && Utilities.MemCompare(signed.Hash, details.Hash));
        }
Beispiel #17
0
        private void StartDownload(ulong target)
        {
            FileDetails details = new FileDetails(ServiceID, 0, Core.Context.SignedUpdate.Hash, Core.Context.SignedUpdate.Size, null);

            Core.Transfers.StartDownload(target, details, LookupConfig.UpdatePath, new EndDownloadHandler(DownloadFinished), null);
        }
Beispiel #18
0
        bool Transfers_FileSearch(ulong key, FileDetails details)
        {
            bool found = false;

            Local.Files.LockReading(() =>
            {
                if (Local.Files.Any(f => f.Size == details.Size && Utilities.MemCompare(f.Hash, details.Hash)))
                    found = true;
            });

            return found;
        }
Beispiel #19
0
        string Transfers_FileRequest(ulong key, FileDetails details)
        {
            SharedFile share = null;

            Local.Files.LockReading(() =>
            {
                share = Local.Files.Where(f => f.Size == details.Size && Utilities.MemCompare(f.Hash, details.Hash)).FirstOrDefault();
            });

            if (share != null && share.Completed)
                return GetFilePath(share);

            return null;
        }
Beispiel #20
0
        private OpTransfer StartTransfer(DhtClient client, SharedFile file)
        {
            FileDetails details = new FileDetails(ServiceID, DataTypeShare, file.Hash, file.Size, null);
            object[] args = new object[] { file };

            OpTransfer transfer = Core.Transfers.StartDownload(client.UserID, details, GetFilePath(file), new EndDownloadHandler(FileDownloadFinished), args);

            transfer.AddPeer(client);

            file.TransferStatus =  "Starting download from " + Core.GetName(client.UserID);

            return transfer;
        }
Beispiel #21
0
        string Transfers_FileRequest(ulong key, FileDetails details)
        {
            UpdateInfo signed = Core.Context.SignedUpdate;

            if (signed != null && signed.Loaded &&
                signed.Size == details.Size && Utilities.MemCompare(signed.Hash, details.Hash))
            {
                string sharePath = Core.User.TempPath + Path.DirectorySeparatorChar + signed.TempName;

                // cannot share from deops root directory because when new update.dat is downloaded the old one
                // cant be replaced if it is locked up by the transfer service

                if (!File.Exists(sharePath))
                    File.Copy(LookupConfig.UpdatePath, sharePath, true);

                return sharePath;
            }

            return null;
        }
Beispiel #22
0
        bool Transfers_PublicSearch(ulong key, FileDetails details)
        {
            // shouldnt be called, transfer starts directly
            Debug.Assert(false);

            return false;
        }
Beispiel #23
0
        private void Download(SignedData signed, VersionedFileHeader header)
        {
            if (!Utilities.CheckSignedData(header.Key, signed.Data, signed.Signature))
            {
                Debug.Assert(false);
                return;
            }

            FileDetails details = new FileDetails(Service, DataType, header.FileHash, header.FileSize, null);

            Core.Transfers.StartDownload(header.KeyID, details, GetFilePath(header), new EndDownloadHandler(EndDownload), new object[] { signed, header } );
        }
Beispiel #24
0
        string Transfers_DataFileRequest(ulong key, FileDetails details)
        {
            ulong hashID = BitConverter.ToUInt64(details.Hash, 0);

            OpFile file = null;

            if (FileMap.SafeTryGetValue(hashID, out file))
                if (details.Size == file.Size && Utilities.MemCompare(details.Hash, file.Hash))
                    return GetFilePath(hashID);

            return null;
        }
Beispiel #25
0
        bool Transfers_FileSearch(ulong key, FileDetails details)
        {
            OpVersionedFile vfile = GetFile(key);

            if (vfile != null)
                if (details.Size == vfile.Header.FileSize && Utilities.MemCompare(details.Hash, vfile.Header.FileHash))
                    return true;

            return false;
        }
Beispiel #26
0
        bool Transfers_DataFileSearch(ulong key, FileDetails details)
        {
            ulong hashID = BitConverter.ToUInt64(details.Hash, 0);

            OpFile file = null;

            if (FileMap.SafeTryGetValue(hashID, out file))
                if (details.Size == file.Size && Utilities.MemCompare(details.Hash, file.Hash))
                    return true;

            return false;
        }