Exemplo n.º 1
0
 private static void ReceiveRecordParents(SharedNetwork.SharedNetworkInfo sharedInfo, RecordParentPack response)
 {
     foreach (var x in response.Parents)
     {
         CheckRecord(sharedInfo, x);
     }
 }
Exemplo n.º 2
0
        internal static bool SendVersions(SharedNetworkInfo sharedInfo, Stack<Objects.Version> versionsToSend)
        {
            try
            {
                int ackCount = 0;
                byte[] tempBuffer = new byte[16 * 1024 * 1024];
                if (versionsToSend.Count == 0)
                {
                    ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(sharedInfo.Stream, new NetCommand() { Type = NetCommandType.SynchronizeRecords }, ProtoBuf.PrefixStyle.Fixed32);
                    var dataResponse = ProtoBuf.Serializer.DeserializeWithLengthPrefix<NetCommand>(sharedInfo.Stream, ProtoBuf.PrefixStyle.Fixed32);
                    if (dataResponse.Type == NetCommandType.Synchronized)
                        return true;
                    return false;
                }
                Printer.PrintDiagnostics("Synchronizing {0} versions to server.", versionsToSend.Count);
                while (versionsToSend.Count > 0)
                {
                    List<Objects.Version> versionData = new List<Objects.Version>();
                    while (versionData.Count < 512 && versionsToSend.Count > 0)
                    {
                        versionData.Add(versionsToSend.Pop());
                    }
                    Printer.PrintDiagnostics("Sending version data pack...");
                    ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(sharedInfo.Stream, new NetCommand() { Type = NetCommandType.PushVersions }, ProtoBuf.PrefixStyle.Fixed32);
                    VersionPack pack = CreatePack(sharedInfo, versionData);
                    Utilities.SendEncrypted(sharedInfo, pack);
                    ackCount++;
                }
                while (ackCount-- > 0)
                {
                    NetCommand response = ProtoBuf.Serializer.DeserializeWithLengthPrefix<NetCommand>(sharedInfo.Stream, ProtoBuf.PrefixStyle.Fixed32);
                    if (response.Type != NetCommandType.Acknowledge)
                        return false;
                }
                ProtoBuf.Serializer.SerializeWithLengthPrefix<NetCommand>(sharedInfo.Stream, new NetCommand() { Type = NetCommandType.SynchronizeRecords }, ProtoBuf.PrefixStyle.Fixed32);
                while (true)
                {
                    var command = ProtoBuf.Serializer.DeserializeWithLengthPrefix<NetCommand>(sharedInfo.Stream, ProtoBuf.PrefixStyle.Fixed32);
                    if (command.Type == NetCommandType.RequestRecordParents)
                    {
                        Printer.PrintDiagnostics("Remote vault is asking for record metadata...");
                        var rrp = Utilities.ReceiveEncrypted<RequestRecordParents>(sharedInfo);
                        RecordParentPack rp = new RecordParentPack();
                        rp.Parents = rrp.RecordParents.Select(x => sharedInfo.Workspace.GetRecord(x)).ToArray();
                        Utilities.SendEncrypted<RecordParentPack>(sharedInfo, rp);
                    }
                    else if (command.Type == NetCommandType.RequestRecord)
                    {
                        var rrd = Utilities.ReceiveEncrypted<RequestRecordData>(sharedInfo);
                        Printer.InteractivePrinter printer = null;
                        SendStats sstats = null;
                        System.Diagnostics.Stopwatch sw = null;
                        if (sharedInfo.Client)
                        {
                            sstats = new SendStats();
                            sw = new System.Diagnostics.Stopwatch();
                            Printer.PrintMessage("Remote has requested #b#{0}## records...", rrd.Records.Length);
                            printer = Printer.CreateProgressBarPrinter("Sending data", string.Empty,
                                    (obj) =>
                                    {
                                        return string.Format("{0}/sec", Versionr.Utilities.Misc.FormatSizeFriendly((long)(sstats.BytesSent / sw.Elapsed.TotalSeconds)));
                                    },
                                    (obj) =>
                                    {
                                        return (100.0f * (int)obj) / (float)rrd.Records.Length;
                                    },
                                    (pct, obj) =>
                                    {
                                        return string.Format("{0}/{1}", (int)obj, rrd.Records.Length);
                                    },
                                    60);
                            sw.Start();
                        }
                        Func<byte[], int, bool, bool> sender = GetSender(sharedInfo, sstats);
                        int processed = 0;
                        foreach (var x in rrd.Records)
                        {
                            var record = sharedInfo.Workspace.GetRecord(x);
                            Printer.PrintDiagnostics("Sending data for: {0}", record.CanonicalName);
                            if (!sharedInfo.Workspace.TransmitRecordData(record, sender, tempBuffer, () =>
                            {
                                sender(BitConverter.GetBytes(x), 8, false);
                            }))
                            {
                                sender(BitConverter.GetBytes(-x), 8, false);
                            }
                            if (printer != null)
                                printer.Update(processed++);
                        }
                        if (!sender(new byte[0], 0, true))
                            return false;

                        if (printer != null)
                            printer.End(rrd.Records.Length);

                        var dataResponse = ProtoBuf.Serializer.DeserializeWithLengthPrefix<NetCommand>(sharedInfo.Stream, ProtoBuf.PrefixStyle.Fixed32);
                        if (dataResponse.Type != NetCommandType.Acknowledge)
                            return false;
                    }
                    else if (command.Type == NetCommandType.RequestRecordUnmapped)
                    {
                        SharedNetwork.SendRecordDataUnmapped(sharedInfo);
                    }
                    else if (command.Type == NetCommandType.Synchronized)
                    {
                        return true;
                    }
                    else
                    {
                        throw new Exception(string.Format("Invalid command: {0}", command.Type));
                    }
                }
            }
            catch (Exception e)
            {
                Printer.PrintError("Error: {0}", e);
                return false;
            }
        }