Inheritance: G2Packet
Esempio n. 1
0
        public void ProcessDownloads()
        {
            if (Downloads.Count == 0)
            {
                return;
            }

            var removeDownloads = new List <Download>();

            foreach (var download in Downloads)
            {
                if (download.Connection.State != TcpState.Connected)
                {
                    removeDownloads.Add(download);
                    continue;
                }

                // while connection has 8kb in buffer free
                while (download.Connection.SendReady) // read 8k, 200b overflow buffer
                {
                    // read 8k of file
                    long readSize = XRay.DatSize - download.FilePos;
                    if (readSize > DownloadChunkSize)
                    {
                        readSize = DownloadChunkSize;
                    }

                    var chunk = new DatPacket(download.FilePos, download.Stream.Read((int)readSize));

                    Log("Sending dat pos: {0}, length: {1}", chunk.Pos, chunk.Data.Length); //todo delete

                    // send
                    if (chunk.Data.Length > 0)
                    {
                        int bytesSent = download.Connection.SendPacket(chunk);
                        if (bytesSent < 0)
                        {
                            break;
                        }
                    }

                    download.FilePos += chunk.Data.Length;

                    // remove when complete
                    if (download.FilePos >= XRay.DatSize)
                    {
                        removeDownloads.Add(download);
                        break;
                    }
                }
            }

            foreach (var download in removeDownloads)
            {
                download.Stream.Close();
                Downloads.Remove(download);
            }
        }
Esempio n. 2
0
        public static DatPacket Decode(G2Header root)
        {
            var dat = new DatPacket();

            foreach (var child in G2Protocol.EnumerateChildren(root))
            {
                switch (child.Name)
                {
                case Packet_Pos:
                    dat.Pos = BitConverter.ToInt64(child.Data, child.PayloadPos);
                    break;

                case Packet_Data:
                    dat.Data = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(dat);
        }
Esempio n. 3
0
        void Receive_DatPacket(XConnection connection, G2ReceivedPacket packet)
        {
            // received by client from server
            var chunk = DatPacket.Decode(packet.Root);

            // write to tmp file
            if (LocalTempFile == null)
            {
                LocalTempFile = File.Create(LocalDatTempPath);
                LocalTempFile.SetLength(0);
            }

            Log("Received dat pos: {0}, length: {1}", chunk.Pos, chunk.Data.Length); //todo delete

            LocalTempFile.Write(chunk.Data);

            var percentComplete = LocalTempFile.Length * 100 / RemoteDatSize;

            RemoteStatus = string.Format("Downloading Dat File - {0}% Complete", percentComplete);

            // hash when complete
            if (LocalTempFile.Length >= RemoteDatSize)
            {
                LocalTempFile.Close();
                LocalTempFile = null;

                var checkHash = Utilities.MD5HashFile(LocalDatTempPath);

                if (checkHash == RemoteDatHash)
                {
                    File.Move(LocalDatTempPath, LocalDatPath);
                    Send_StartSync(connection);
                }
                else
                {
                    RemoteStatus = string.Format("Dat integrity check failed - Expecting {0}, got {1}", RemoteDatHash, checkHash);
                }
            }
        }
Esempio n. 4
0
        public static DatPacket Decode(G2Header root)
        {
            var dat = new DatPacket();

            foreach(var child in G2Protocol.EnumerateChildren(root))
            {
                switch (child.Name)
                {
                    case Packet_Pos:
                        dat.Pos = BitConverter.ToInt64(child.Data, child.PayloadPos);
                        break;

                    case Packet_Data:
                        dat.Data = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return dat;
        }
Esempio n. 5
0
        public void ProcessDownloads()
        {
            if (Downloads.Count == 0)
                return;

            var removeDownloads = new List<Download>();

            foreach (var download in Downloads)
            {
                if (download.Connection.State != TcpState.Connected)
                {
                    removeDownloads.Add(download);
                    continue;
                }

                // while connection has 8kb in buffer free
                while (download.Connection.SendReady) // read 8k, 200b overflow buffer
                {
                    // read 8k of file
                    long readSize = XRay.DatSize - download.FilePos;
                    if (readSize > DownloadChunkSize)
                        readSize = DownloadChunkSize;

                    var chunk = new DatPacket(download.FilePos, download.Stream.Read((int)readSize));

                    Log("Sending dat pos: {0}, length: {1}", chunk.Pos, chunk.Data.Length); //todo delete

                    // send
                    if(chunk.Data.Length > 0)
                    {
                        int bytesSent = download.Connection.SendPacket(chunk);
                        if(bytesSent < 0)
                            break;
                    }

                    download.FilePos += chunk.Data.Length;

                    // remove when complete
                    if (download.FilePos >= XRay.DatSize)
                    {
                        removeDownloads.Add(download);
                        break;
                    }
                }
            }

            foreach (var download in removeDownloads)
            {
                download.Stream.Close();
                Downloads.Remove(download);
            }
        }