Exemple #1
0
        private static void StateToBytes(DualSourceHTTPDownloaderState state, BinaryWriter w)
        {
            w.Write(state.Id !);
            w.Write(state.TempDir ?? string.Empty);
            w.Write(state.FileSize);
            w.Write(state.LastModified.ToBinary());
            w.Write(state.SpeedLimit);
            w.Write(state.Init1);
            w.Write(state.Init2);
            w.Write(state.Url1.ToString());
            w.Write(state.Url2.ToString());
            var hasHeaders1 = state.Headers1 != null;

            w.Write(hasHeaders1);
            if (hasHeaders1)
            {
                XDM.Messaging.StreamHelper.WriteStateHeaders(state.Headers1, w);
            }
            var hasHeaders2 = state.Headers2 != null;

            w.Write(hasHeaders2);
            if (hasHeaders2)
            {
                XDM.Messaging.StreamHelper.WriteStateHeaders(state.Headers2, w);
            }
            var hasCookies1 = state.Cookies1 != null;

            w.Write(hasCookies1);
            if (hasCookies1)
            {
                XDM.Messaging.StreamHelper.WriteStateCookies(state.Cookies1, w);
            }
            var hasCookies2 = state.Cookies2 != null;

            w.Write(hasCookies2);
            if (hasCookies2)
            {
                XDM.Messaging.StreamHelper.WriteStateCookies(state.Cookies2, w);
            }
            var hasProxy = state.Proxy.HasValue;

            w.Write(hasProxy);
            if (hasProxy)
            {
                w.Write(state.Proxy !.Value.Host ?? string.Empty);
                w.Write(state.Proxy !.Value.Port);
                w.Write((int)state.Proxy !.Value.ProxyType);
                w.Write(state.Proxy !.Value.UserName ?? string.Empty);
                w.Write(state.Proxy !.Value.Password ?? string.Empty);
            }
        }
Exemple #2
0
        private static DualSourceHTTPDownloaderState DualSourceHTTPDownloaderStateFromBytes(BinaryReader r)
        {
            var state = new DualSourceHTTPDownloaderState
            {
                Id           = r.ReadString(),
                TempDir      = XDM.Messaging.StreamHelper.ReadString(r),
                FileSize     = r.ReadInt64(),
                LastModified = DateTime.FromBinary(r.ReadInt64()),
                SpeedLimit   = r.ReadInt32(),
                Init1        = r.ReadBoolean(),
                Init2        = r.ReadBoolean(),
                Url1         = new Uri(r.ReadString()),
                Url2         = new Uri(r.ReadString()),
            };

            if (r.ReadBoolean())
            {
                XDM.Messaging.StreamHelper.ReadStateHeaders(r, out Dictionary <string, List <string> > headers);
                state.Headers1 = headers;
            }
            if (r.ReadBoolean())
            {
                XDM.Messaging.StreamHelper.ReadStateHeaders(r, out Dictionary <string, List <string> > headers);
                state.Headers2 = headers;
            }
            if (r.ReadBoolean())
            {
                XDM.Messaging.StreamHelper.ReadStateCookies(r, out Dictionary <string, string> cookies);
                state.Cookies1 = cookies;
            }
            if (r.ReadBoolean())
            {
                XDM.Messaging.StreamHelper.ReadStateCookies(r, out Dictionary <string, string> cookies);
                state.Cookies2 = cookies;
            }
            if (r.ReadBoolean())
            {
                state.Proxy = new ProxyInfo
                {
                    Host      = XDM.Messaging.StreamHelper.ReadString(r),
                    Port      = r.ReadInt32(),
                    ProxyType = (ProxyType)r.ReadInt32(),
                    UserName  = XDM.Messaging.StreamHelper.ReadString(r),
                    Password  = XDM.Messaging.StreamHelper.ReadString(r),
                };
            }
            return(state);
        }
Exemple #3
0
 public static void Save(DualSourceHTTPDownloaderState state)
 {
     TransactedBinaryDataReader.Write($"{state.Id}.state", Config.DataDir, w => StateToBytes(state, w));
 }